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
12345678910111213141516171819
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.
- Next we will create a function to display all matching images for an image we submit to the LVQ network.
202122232425262728293031
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.