Image Classification Using MATLAB SOM/LVQ

Posted on Updated on

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


  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
    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)

    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]))
  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:

35 thoughts on “Image Classification Using MATLAB SOM/LVQ

    Matteo said:
    January 22, 2013 at 4:04 am

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

      Romaine Carter responded:
      January 22, 2013 at 7:25 am

      Thank you Matteo, im glad you like it.

    bala said:
    January 28, 2013 at 1:43 am

    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@

    bala said:
    January 28, 2013 at 1:45 am

    how to train Neural network and classify image in matlab

    Priya said:
    February 20, 2013 at 7:26 am

    I need help in train neural network for classification for iris recognition pls…. help me…..

    vidhya said:
    March 2, 2013 at 8:57 am

    i am doing project on image classification using CBIR technique with ANN using gabor filter. can you please help me out with the coding of it… :(

      Romaine Carter responded:
      March 3, 2013 at 7:57 pm

      See my profile on LinkedIn we can go over some ideas.

    brij said:
    March 7, 2013 at 6:19 am

    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

      Romaine Carter responded:
      March 7, 2013 at 6:37 am

      See my LinkedIn profile for contact details.

    Erika Lim said:
    March 12, 2013 at 3:42 am

    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!

    Romaine Carter responded:
    March 12, 2013 at 6:14 am

    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. :-)

    Sed said:
    March 19, 2013 at 12:26 am

    Hey bro how to include an notification when the pic is not in the data base

      Romaine Carter responded:
      March 20, 2013 at 6:23 am

      add an else statement which displays a message or a not found picture.

    Gkatidis Konstantinos said:
    March 25, 2013 at 10:14 am

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

    poornima said:
    April 2, 2013 at 4:40 am

    I am doing work in lvq to train & test lvq network.How to make confutsion matrix & classification.If you give the code it would be helpful.Thanks in advance.

    devikad said:
    April 16, 2013 at 11:25 pm

    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!

      Arghya said:
      June 8, 2014 at 7:09 am

      I am also having the same problem….Anyone please HEEEEEEELLLLLLLLLLLLLLLPPPPPPPPPPPPPPPPP

      Rony said:
      June 8, 2014 at 7:11 am

      SAME PROBLEM !!!!!!!!!!!!!!!!!!! …..

      I am Using MATLAB R2013b

    sail said:
    May 25, 2013 at 12:28 pm

    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.

    saf said:
    June 13, 2013 at 3:58 am

    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

      Sander Ali said:
      August 30, 2013 at 1:19 pm

      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

    saurabh said:
    November 8, 2013 at 3:31 am

    The code is very simple and very useful.
    Thanks for sharing.
    Really Appreciable effort.

    suarez said:
    January 6, 2014 at 5:07 am

    can I get this project ?

    Romaine Carter responded:
    January 6, 2014 at 5:10 am

    Please explain your use of “get”

    hailu alemu said:
    April 6, 2014 at 6:19 am

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

    Arghya said:
    June 9, 2014 at 12:14 am

    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!

    abcd said:
    August 11, 2014 at 9:07 am

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

    filran said:
    September 19, 2014 at 1:05 pm

    Hi, Romaine Carter! I would like talk about your code. Please, contact me:

    […] Image Classification Using MATLAB SOM/LVQ | Script Bucket – Sep 21, 2012 · 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 …… […]

    vinayraj m khunti said:
    December 3, 2014 at 12:56 pm

    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???

      Romaine Carter responded:
      December 3, 2014 at 10:25 pm

      You should try adjusting the sensitivity of the nn and also train with a larger image dataset.

    shaikmeera said:
    February 27, 2015 at 8:00 am

    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.

    Deepak said:
    May 15, 2015 at 6:06 am

    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] =

    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.

    Bheki said:
    December 8, 2015 at 7:50 pm

    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

      Romaine Carter responded:
      December 9, 2015 at 8:50 pm

      What version of MATLAB are you using?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s