1

I want to calculate the Euclidean distance between two images in Matlab. I find some examples and I've try them but they are not correct.
The result of this Euclidean distance should be between 0 and 1 but with two different ways I reached to different solutions.
The first algorithm gives me a 4 digit number such as 2000 and other digits like this and by the other way I reached numbers such as 0.007
What is wrong with it?

This is one of those algorithms I mentioned:

Im1 = imread('1.jpeg');
Im2 = imread('2.jpeg');

Im1 = rgb2gray(Im1);
Im2 = rgb2gray(Im2);

hn1 = imhist(Im1)./numel(Im1);
hn2 = imhist(Im2)./numel(Im2);

% Calculate the Euclidean distance
f = sum((hn1 - hn2).^2)
Schorsch
  • 7,761
  • 6
  • 39
  • 65
deansam
  • 68
  • 1
  • 2
  • 8
  • Very similar question can be find here: http://stackoverflow.com/questions/5475815/comparing-two-image-using-histogram?rq=1 – NKN Jul 09 '13 at 18:14

2 Answers2

4

the final line of code needs a sqrt command:

f = sum(sqrt(hn1-hn2).^2);

check this link

You can also use the norm command

f = norm(hn1-hn2);

These post1 and post2 can be useful.

Community
  • 1
  • 1
NKN
  • 6,482
  • 6
  • 36
  • 55
  • i don't have enough reputation to vote you,and i'm sorry:(. thanks a milion for your great help – deansam Jul 09 '13 at 19:10
  • In some specific case, the result of the euclidean distance between 2 arrays can be an array ? or it should be always scalar ? – Christina Dec 24 '13 at 21:52
1

Oh, I'm not sure where to begin but here are some things that you should think about:

1: You're normalising your histograms incorrectly. You want them to have unit L1-norm:

hn1 = imhist(Im1);
hn2 = imhist(Im2);
hn1 = hn1/numel(hn1);
hn2 = hn2/numel(hn2);

2: Taking L2-distance between histograms doesn't really make sense (what is an euclidian distance between two distributions really?). You should rather take a look at something like a L1 or Chi-2 distance, or use an intersection kernel. L1 would be

f=norm(hn1-hn2,1);

3: If you really do want it to be L2 euclidian distance, the last line should be

f=norm(hn1-hn2); 

but then you should rather L2-normalize the histogram:

hn1 = imhist(Im1);
hn2 = imhist(Im2);
hn1 = hn1/norm(hn1);
hn2 = hn2/norm(hn2);

4: Please try to be clearer in the formulation of your questions - it was a bit hard to decode :). If your would have mentioned the application - I could have given some additional pointers. :)

kamjagin
  • 3,614
  • 1
  • 22
  • 24
  • thanks. i want to write the vsumm algorithm. i must compute the pairwise distance of consecutive frames in the extracted sample, according to Euclidean distance. while there is a threshold 0.5 ,when ever the Euclidean distance go above 0.5 , k should be increased. – deansam Jul 09 '13 at 19:58
  • I see. In that case I would vote for sticking with Euclidian distance, (as it is justified by the k-means clustering step) even if it is not the most suitable distance measure for comparing histograms. – kamjagin Jul 09 '13 at 20:54
  • thank you. do you have any idea for this algorithm? i'm writing it and it's a little hard. do you have any guidance? – deansam Jul 09 '13 at 23:47
  • dear kamjagin
    can you please answer to my other question which i asked in this link:http://stackoverflow.com/questions/17563980/calculate-the-normalized-histogram-of-image-in-hsv-space-in-matlab
    – deansam Jul 10 '13 at 07:47