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


  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:

About these ads

28 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@

  3. 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… :(

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

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

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

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

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

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

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

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

    • 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

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

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

  14. Pingback: Fix Som Quantization Error Windows XP, Vista, 7, 8 [Solved]
  15. 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???

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