Image Classification Using MATLAB SOM/LVQ

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?

  1. Neural Network    -> selforgmap, lvqnet, vec2ind, ind2vec
  2. Image Processing -> imhist, imresize, rgb2gray, imread

Other:

  1. 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}.

  1. 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)
    end
                   

    Links 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.

  2. 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
    
  3. Download a picture of a koala and save it outside your MATLAB path as koalatest.jpg
  4. At the MATLAB command prompt type scan(imread(‘[replace with path to koalatest]\koalatest.jpg’)
  5. 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.

LVQ Been trained:

40 thoughts on “Image Classification Using MATLAB SOM/LVQ

  1. 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+ …

  2. 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

  3. I need help in train neural network for classification for iris recognition pls…. help me…..
    My id:anjupriya23@gmail.com

  4. 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… 😦

  5. 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

  6. 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!

  7. 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. 🙂

  8. 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!!

  9. 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.

  10. 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!

  11. Surface Texture classification using MATLAB TOOLBOX
    I am trying with waveltet and SVM

    Extraction using WAVELET
    Classification using SUPPORT VECTOR MACHINE (SVM)

    (1) which toolbox should I choose.

    (2) Which method should I choose for Ferture extraction using wavelet and how to implement it.

    (3)how to load extracted features in SVM and what settings should i USE.

    (4) which method should I choose for claasification using SVM and how to implement it .

    (5) Please suggest in steps.
    sail133a@yahoo.com

  12. Hello,
    I used RGB (N x M x 3) and tried to run these functions and I got the below error, can you help me?

    ??? Error using ==> rgb2gray>parse_inputs at 82
    MAP must be a m x 3 array.

    Error in ==> rgb2gray at 35
    X = parse_inputs(varargin{:});

    Error in ==> scan at 16
    hist = [hist, imhist(rgb2gray(imresize(file,[ 50 50])))]; %#ok

    1. Saf,
      the directory which you chose must contains grayscale images, this code only works on color images so filter out the grayscale images in that folder and then try the code

  13. I need to know how to extract features of each objects in both color and grayscale image using matlab for my thesis

  14. Anyone please help me…I am getting the code..I am using R2013b Matlab and Windows7 os

    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!

  15. WOW, it works perfectly! (After reseting my PC three times because of frozen Matlab and unsufficient memory.) Koala founded! Excellent idea.

  16. I have tried a lot and finally it ran but you know what I have tried to find a Jellyfish and it showed me a Penguin..! How to solve this???

  17. Im doing same image classification using the feature vector of the each image and i want to show minimum 10 matching images with the given qurey image like image matching 100%,90% and so on…

    in here you have used if loop where 100% matching image alone will be displayed can you tell me how to find the percentage matching so that i ll make my own code to display required no of matching images.

  18. Sir thanx for sharing your ideas. But sir i am not able to get the output after completing all the steps as you said . I found some error that is:

    Maximum variable size allowed by the program is exceeded.

    Error in nnMex2.codeHints (line 117)
    hints.TEMP = zeros(1,ceil(tempSize/8),’double’);

    Error in nncalc.setup2 (line 13)
    calcHints = calcMode.codeHints(calcHints);

    Error in network/sim (line 321)
    [calcLib,calcNet] =
    nncalc.setup2(calcMode,calcNet,calcData,calcHints,isParallel);

    Error in network/subsref (line 17)
    otherwise, v = sim(vin,subs{:});

    Error in scan (line 13)
    t = som(hist); %extract class data

    Please help me . I need your suggestion to go ahead.

  19. Hi,
    I am trying out this code but it comes up with an error which reads
    ” function scan(img)

    Error: Function definitions are not permitted in this context.
    “.
    Any idea on how I could fix this error ?

    Thank you in advance

  20. Hello, please help me I did all the steps but I am getting this error

    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 scan (line 12)
    som = train(som, hist);

  21. Hy, i need to do cigarette obejct detection using neural network in matlab using image color segementation and texture information .. can any one tell me hoe to train and classify image using lvq neural network please reply to gedebukal@gmail.com

  22. Hello Sir,
    I want to implement SOM for finding unique basis for images. I have set of images from which I have extracted features. So, I have all point data for each image. Now I want to train net using SOM and then group similar images at each SOM node. Please guide me how do I make data set for this. Thanks.

Leave a reply to Romaine Carter Cancel reply