I like to think of myself as a hacker
, not in today’s sense of the word [person who breaks into secured computer areas] but as a hacker in the sense of first two definitions found here. I like to experiment with things especially related to computers and Artificial Intelligence in particular. MATLAB happens to be a virtual toolbox for the trade pun intended :-), using some of its toolboxes we will see how we can solve a ubiquitous problem that faces most persons with a nice camera and a voracious appetite for taking pictures.
Now, I don’t have an expensive Nikon, but I do have loads of pictures; and one day i was trying to find this particular picture when it occurred to me that if i could not remember the name or date when i took the picture it would require me to search blindly every picture I had in order to find that special one. Now what if i had some way of finding the picture based on what i could remember of it? ie. environment in which it was taken, colours and objects along with some other visual specifications, wouldn’t that be cool.
So I went for the proverbial toolbox MATLAB, which tools will I need?
- Neural Network -> selforgmap, lvqnet, vec2ind, ind2vec
- Image Processing -> imhist, imresize, rgb2gray, imread
Other:
- mspaint
Note: For this demonstration I will be using:
- MATLAB R2011b on Windows 7
- Pictures found at C:\Users\Public\Pictures\Sample Pictures
Ok lets do it, start up MATLAB, copy and paste all the pics from the above directory to MATLAB’s current directory. {Chrysanthemum, Desert, Hydrangeas, Jellyfish, Koala, Lighthouse, Penguins, Tulips}.
- In the new edit window copy and paste the code as given below. Save file as scan.m
function scan(img) files = dir('*.jpg'); hist = []; for n = 1 : length(files) filename = files(n).name; file = imread(filename); hist = [hist, imhist(rgb2gray(imresize(file,[ 50 50])))]; %#ok end som = selforgmap([10 10]); som = train(som, hist); t = som(hist); %extract class data net = lvqnet(10); net = train(net, hist, t); like(img, hist, files, net) endLinks to the functions used were provided above therefore i will not be going into the details of how they work, however there is a narrative with regard to the workings of the code:
The code starts by searching the current MATLAB directory for all files with a .jpg extension. On each iteration of the loop an image is loaded and resized to 50 x 50, it is then converted to greyscale and a histogram measurement is taken of its pixels [feature vector]; the results are then appended to a 256 x n matrix with n been the number of images scanned.
A self organizing map network is then used to identify classes into which the images fall. The feature matrix and class data is used to train a Learning Vector Quantization neural network, that will be used for classification of images presented to it.
- Next we will create a function to display all matching images for an image we submit to the LVQ network.
function like(im, hist, files , net) hs = imhist(rgb2gray(imresize(im,[50 50]))); cls = vec2ind(net(hs)); [~, n] = size(hist); for i = 1 : n if(cls == vec2ind(net(hist(:, i)))) figure('name', files(i).name); imshow(imresize(imread(files(i).name), [100 100])) end end end - Download a picture of a koala and save it outside your MATLAB path as koalatest.jpg
- At the MATLAB command prompt type scan(imread(‘[replace with path to koalatest]\koalatest.jpg’)
- After a minute or two the networks should have been trained and a figure displaying the matching koala.jpg image shown to you.
NOTE: As explained above this is hacking, not production code I wrote this up in about 20 minutes as a demonstration for classification of images, with imagination this could be extended to classify things like sound for example using a feature map crated from humming a tune to find a song with a similar melody.

This is brilliant, thanks for sharing your idea and code. So many times I have been in that same spot of looking for that particular photo among 15000+ …
Thank you Matteo, im glad you like it.
i need to do image classification using neural network in matlab using image dn values and texture information .. can any one tell me hoe to train and classify image using back propagation neural network please reply to bala.c89@ gmail.com
how to train Neural network and classify image in matlab
I need help in train neural network for classification for iris recognition pls…. help me…..
My id:anjupriya23@gmail.com
i am doing m.tech project on image classification using CBIR technique with ANN using gabor filter. can you please help me out with the coding of it…
See my profile on LinkedIn we can go over some ideas.
I am working on SOM and k-NN for chracter recognition
what i want to do is
1. i have a set of input images for characters of gujarati script
2. want to creat a som network and to give all images in input. than some unseen data samples I want to give to input and want to check that it can be recognized or not.
wants to creat clusters in output latice. and their mean values will be stored
than I am going to use k-NN for a input data it will search nearest cluster mean value and than withing that cluster one of best matching sample will be recognized. for that can you help me for matlab code. I am new to matlab
See my LinkedIn profile for contact details.
Hi there, I must say you have awesome ideas and thanks for the coding! Anyway, I’ve tried the codes, and guess what, on the first attempt, it shows the image of penguins.jpg; second attempt, penguin again; third attempt, I tried using a penguin image as penguintest.jpg, and it shows tulips. LOL!
Hi Erika, i must say that this code is by no means production ready, also depending on the color and similarity of the images you may have false positives. Thanks for the complement though, feel free to modify and improve on the code as you see fit.
Hey bro how to include an notification when the pic is not in the data base
add an else statement which displays a message or a not found picture.
hey .. i would like some help… i want to classify sounds.wav and i want some help if you have an idea about the features i need to use. i tried 3 features: weighted mean, fundamental frequency, fundamental frequency amplitude. my result is about 60%. can i improve it?
thank you!!
I am doing work in lvq network.how to train & test lvq network.How to make confutsion matrix & classification.If you give the code it would be helpful.Thanks in advance.
Hey, I’m a beginner to matlab. Thanks for this code. I did everything exactly as outlined in your instructions but I am getting the following errors :
Error using *
Inner matrix dimensions must agree.
Error in learnsomb>apply (line 122)
a2 = neighborhood * a + a;
Error in trainbu>train_network (line 208)
[dw,IWLS{i,j}] = fcn.apply(net.IW{i,j}, …
Error in trainbu (line 103)
[net,tr] = train_network(net,tr,data,fcns,param);
Error in network/train (line 106)
[net,tr] = feval(net.trainFcn,net,X,T,Xi,Ai,EW,net.trainParam);
Error in scan1 (line 12)
som = train(som, hist);
Any idea what could be causing these errors? Thanks!