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)
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. :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.