Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

how to find euclidean distance for an image

Asked by FIR on 20 Dec 2011

I have 100 images and i have to find the euclidean distance for it,and i have to take a query image and find the euclidean distance and retrieve the image ,i have extracted an feature of an image and have stored it in .mat file,please help

3 Comments

Naz on 22 Dec 2011

Amazing. You have such diverse questions. I wonder how you manage to work on so many different things at a time.

Joel on 13 Mar 2013

Dear FIR, could you please send me the files to this project so I can have a better look and see if I might be able to help.

Thanks

Image Analyst on 13 Mar 2013

Joel, did you notice that FIR posted this 15 months ago? I doubt he still needs your help on it. Besides, he accepted an answer already.

FIR

5 Answers

Answer by Junaid on 22 Dec 2011
Accepted answer

Dear FIR, Sorry FIR I can't overview your code you sent to me. To compute the Euclidean distance between images or image features, your vector length or matrix should have same dimensions. Let say your first image has 1 x 460 vector then your query should be of same length. If that is the case then you can easily find Euclidean distance by the code I have written below. You just have to ensure that the dimensions are the same. I give you example of Histogram feature of two images.

I = imread('myimage.jpg'); 
I = rgb2gray(I);
h = imhist(I); % this will have default bins 256
% now second image 
J = imread('myimage1.jpg'); 
J = rgb2gray(J);
h1 = imhist(J); % this will have default bins 256 
E_distance = sqrt(sum((h-h1).^2));

You can do it for 1000 images as well. Let say now your 1000 images histogram are concatenated into h1. where each column is one histogram. Then your query image histogram is h. Then distance can be computed as follow.

h_new = repmat(h,1,size(h1,2));
E_distance = sqrt(sum((h_new-h1).^2));

2 Comments

Aziz on 3 Oct 2012

nice share Junaid... can i ask question similar to this one... i also have problem to compare 2 image at onetime, example of 2 simple image... http://i50.tinypic.com/259e5hh.jpg http://i47.tinypic.com/21m6q9c.jpg

so it is possible to get the x and y axis value for this difference?

jenifer on 29 Oct 2012

I also have one doubt related to this..I want to classify normal and abnormal images..I m having histogram as a features for normal and abnormal images.. How can i compute the feature vectors for these histograms...

Junaid
Answer by Junaid on 21 Dec 2011

Dear FIR,

Similar question was asked by one fellow. The solution you can see from following URL. I hope it might help you.

http://mathworks.com/matlabcentral/answers/22844-how-to-find-euclidean-distance-in-matlab

2 Comments

FIR on 21 Dec 2011

Juniard i have gone through it,but final question was not answered,which i need,I have one query image ,and other images in .mat file which are concatanated,now i have to find the euclidean distance for image retrieval,please help

Junaid on 22 Dec 2011

Dear FIR,

Sorry FIR I can't overview your code you sent to me. To compute the Euclidean distance between images or image features, your vector length or matrix should have same dimensions. Let say your first image has 1 x 460 vector then your query should be of same length. If that is the case then you can easily find Euclidean distance by the code I have written below. You just have to ensure that the dimensions are the same. I give you example of Histogram feature of image.

I = imread('myimage.jpg');
I = rgb2gray(I);

h = imhist(I); % this will have default bins 256

% now second image
J = imread('myimage1.jpg');
J = rgb2gray(J);

h1 = imhist(J); % this will have default bins 256
E_distance = sqrt(sum((h-h1).^2));

You can do it for 1000 images as well.

Junaid
Answer by Junaid on 21 Dec 2011

Dear Fir,

You have Query image Q, you want to compute euclidean distance of Q with all images in database. Is that you want ? If yes then Let say query Image Q is grayscale image so you can present it as feature vector

Q = Q(:); % this is one [size(Q,1) x size(Q,2) by 1]

all the images in database should have same dimensions. Let say every image and query image should have same number of pixels.

Now you load your database

D = load('Database.mat');

we assume that each column is one image and your number of columns should be size of Database. or if you want to present each row as image then simply take the transpose.

Q= repmat(Q,1,size(D,2));
E_distance = sqrt(sum((Q-D).^2));

Now E_distance have euclidean distance of Q with all images in database D.

Do let me know if It solved your problem.

3 Comments

Image Analyst on 21 Dec 2011

He said "I have extracted features of 100 images and stored in .matfile.i have to find euclidean distance for those" so he wants to compare feature vectors, not the images themselves. Anyway trying to compare images on a pixel by pixel basis is only useful for certain kinds of situations, like characterizing compression/decompression algorithms, not, say for retrieving all images from a huge database that have faces in them.

FIR on 21 Dec 2011

Juniard i have uploaded my files,plz look it and suggest ideas

I have attached my program files plz see it and give suggestion

http://www.sendspace.com/file/t58trj

FIR on 22 Dec 2011

Juniard i get error wen executing ur code

Undefined function or method 'minus' for input arguments of type 'struct'.

Error in ==> features at 22
E_distance = sqrt(sum((Q-D).^2))

Junaid
Answer by Sean de Wolski on 20 Dec 2011
doc bwdist
doc graydist

might be some places to start.

1 Comment

FIR on 21 Dec 2011

Sean can u tell how to process for image retrieval using query image by euclidean distance

Sean de Wolski
Answer by Image Analyst on 21 Dec 2011

The Euclidean distance is another image. What do you mean "query image by Euclidean distance"? I don't even know what that means. Please explain.

6 Comments

Image Analyst on 21 Dec 2011

I don't understand that. What is that? Is your feature vector actually a cell array where the first cell has a 487 element row vector, same for the second cell, the third cell has a 359 element row vector, etc. Do you have 100 cells in your cell array? Feature vectors virtually never have thousands of features in them like that. I think you've chosen the wrong features. What does each feature represent? They should be things like the mean, standard deviation (for each color), perhaps the area fraction of edges or of "skin" pixels, maybe the presence of certain shapes, etc. Here's a nice database comparison that gets color feature vectors and retrieves images with those colors you select in it:

http://labs.ideeinc.com/multicolr/

FIR on 21 Dec 2011

I have attached my program files plz see it and give suggestion

http://www.sendspace.com/file/t58trj

Image Analyst on 21 Dec 2011

I probably won't get to it. I'm leaving on 9 day vacation to Florida in a couple of hours.

Image Analyst

Contact us