{"id":355,"date":"2013-12-06T13:41:36","date_gmt":"2013-12-06T18:41:36","guid":{"rendered":"http:\/\/homepages.uc.edu\/~yaozo\/wordpress\/?p=355"},"modified":"2013-12-06T13:41:36","modified_gmt":"2013-12-06T18:41:36","slug":"image-classification-using-matlab-somlvq","status":"publish","type":"post","link":"https:\/\/zhuoyao.net\/index.php\/2013\/12\/06\/image-classification-using-matlab-somlvq\/","title":{"rendered":"Image Classification Using MATLAB SOM\/LVQ"},"content":{"rendered":"<p>I like to think of myself as a hacker :-), not in today\u2019s sense of the word [person who breaks into secured computer areas] but as a hacker in the sense of first two definitions found\u00a0<a title=\"hacker\" href=\"http:\/\/duartes.org\/gustavo\/blog\/post\/first-recorded-usage-of-hacker\">here<\/a>. 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 \u00a0:-), 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\u00a0appetite\u00a0for taking pictures.<\/p>\n<p>Now, I don\u2019t have an expensive Nikon, but I do have loads of\u00a0pictures; and one day i was trying to find this particular picture when it\u00a0occurred\u00a0to me that if i could not remember the name or date when i took the picture \u00a0it 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,\u00a0colours\u00a0and objects along with some other visual specifications,\u00a0wouldn\u2019t\u00a0that be cool.<\/p>\n<p>So I went for the proverbial toolbox\u00a0<em>MATLAB,\u00a0<\/em>which tools will I need?<\/p>\n<ol>\n<li>Neural Network \u00a0 \u00a0-&gt;\u00a0<a title=\"self organizing map\" href=\"http:\/\/www.mathworks.com\/help\/nnet\/ref\/selforgmap.html\">selforgmap<\/a>,\u00a0<a title=\"Learning vector quantization\" href=\"http:\/\/www.mathworks.com\/help\/nnet\/ref\/lvqnet.html\">lvqnet<\/a>,\u00a0<a href=\"http:\/\/www.mathworks.com\/help\/nnet\/ref\/vec2ind.html\">vec2ind<\/a>,\u00a0<a href=\"http:\/\/www.mathworks.com\/help\/nnet\/ref\/ind2vec.html\">ind2vec<\/a><\/li>\n<li>Image Processing -&gt;\u00a0<a href=\"http:\/\/www.mathworks.com\/help\/images\/ref\/imhist.html\">imhist<\/a>,\u00a0<a href=\"http:\/\/www.mathworks.com\/help\/images\/ref\/imresize.html\">imresize<\/a>,\u00a0<a href=\"http:\/\/www.mathworks.com\/help\/images\/ref\/rgb2gray.html\">rgb2gray<\/a>,\u00a0<a href=\"http:\/\/www.mathworks.com\/help\/matlab\/ref\/imread.html\">imread<\/a><\/li>\n<\/ol>\n<p>Other:<\/p>\n<ol>\n<li>mspaint<\/li>\n<\/ol>\n<p><strong>Note<\/strong>: For this demonstration I will be using:<\/p>\n<ul>\n<li>MATLAB R2011b on Windows 7<\/li>\n<li>Pictures found at \u00a0C:\\Users\\Public\\Pictures\\Sample Pictures<\/li>\n<\/ul>\n<p>Ok lets do it, \u00a0start up MATLAB, copy and paste all the pics from the above directory to MATLAB\u2019s current directory. {Chrysanthemum,\u00a0Desert,\u00a0Hydrangeas,\u00a0Jellyfish,\u00a0Koala,\u00a0Lighthouse,\u00a0Penguins,\u00a0Tulips}.<\/p>\n<ol>\n<li>In the new edit window copy and paste the code as given below. Save file as scan.m\n<div>\n<div id=\"highlighter_63271\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<div>2<\/div>\n<div>3<\/div>\n<div>4<\/div>\n<div>5<\/div>\n<div>6<\/div>\n<div>7<\/div>\n<div>8<\/div>\n<div>9<\/div>\n<div>10<\/div>\n<div>11<\/div>\n<div>12<\/div>\n<div>13<\/div>\n<div>14<\/div>\n<div>15<\/div>\n<div>16<\/div>\n<div>17<\/div>\n<div>18<\/div>\n<div>19<\/div>\n<\/td>\n<td>\n<div>\n<div><code>function<\/code> <code>scan(img)<\/code><\/div>\n<div><code>files = dir(<\/code><code>'*.jpg'<\/code><code>);<\/code><\/div>\n<div><code>hist = [];<\/code><\/div>\n<div><code>for<\/code> <code>n = 1 : length(files)<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>filename = files(n).name;<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>file = imread(filename);<\/code><\/div>\n<div><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>hist = [hist, imhist(rgb2gray(imresize(file,[ 50 50])))]; <\/code><code>%#ok<\/code><\/div>\n<div><code>end<\/code><\/div>\n<div><\/div>\n<div><code>som = selforgmap([10 10]);<\/code><\/div>\n<div><code>som = train(som, hist);<\/code><\/div>\n<div><code>t\u00a0\u00a0 = som(hist); <\/code><code>%extract class data<\/code><\/div>\n<div><\/div>\n<div><code>net = lvqnet(10);<\/code><\/div>\n<div><code>net = train(net, hist, t);<\/code><\/div>\n<div><\/div>\n<div><code>like(img, hist, files, net)<\/code><\/div>\n<div><code>end<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>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:<\/p>\n<p>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.<\/p>\n<p>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.<\/li>\n<li>Next we will create a function to display all matching images for an image we submit to the LVQ network.\n<div>\n<div id=\"highlighter_65329\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>20<\/div>\n<div>21<\/div>\n<div>22<\/div>\n<div>23<\/div>\n<div>24<\/div>\n<div>25<\/div>\n<div>26<\/div>\n<div>27<\/div>\n<div>28<\/div>\n<div>29<\/div>\n<div>30<\/div>\n<div>31<\/div>\n<\/td>\n<td>\n<div>\n<div><code>function<\/code> <code>like(im, hist, files , net)<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>hs = imhist(rgb2gray(imresize(im,[50 50])));<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>cls = vec2ind(net(hs));<\/code><\/div>\n<div><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>[~, n] = size(hist);<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>for<\/code> <code>i = 1 : n<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>if<\/code><code>(cls == vec2ind(net(hist(:, i))))<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>figure(<\/code><code>'name'<\/code><code>, files(i).name);<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>imshow(imresize(imread(files(i).name), [100 100]))<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>end<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>end<\/code><\/div>\n<div><code>end<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/li>\n<li>Download a picture of a koala and save it outside your MATLAB path as\u00a0<em>koalatest.jpg<\/em><\/li>\n<li>At the MATLAB command prompt type\u00a0<em>scan(imread(\u2018[replace with path to koalatest]\\koalatest.jpg\u2019)<\/em><\/li>\n<li>After a minute or two the networks should have been trained and a figure displaying the matching koala.jpg image shown to you.<\/li>\n<\/ol>\n<p><strong>NOTE:\u00a0<\/strong>As\u00a0explained\u00a0above 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.<\/p>\n<p>LVQ Been trained:<br \/>\n<a href=\"http:\/\/scriptbucket.files.wordpress.com\/2012\/09\/lvq-train.png\"><img decoding=\"async\" title=\"LVQ Train\" alt=\"\" src=\"http:\/\/scriptbucket.files.wordpress.com\/2012\/09\/lvq-train.png?w=640\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I like to think of myself as a hacker :-), not in today\u2019s sense of the word [person who breaks into secured computer areas] but&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-355","post","type-post","status-publish","format-standard","hentry","category-image-processing"],"_links":{"self":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/355","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/comments?post=355"}],"version-history":[{"count":0,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/355\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/media?parent=355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/categories?post=355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/tags?post=355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}