MATLAB Answers

A chi-square distance or Euclidean distance?

13 views (last 30 days)
Hi, I want to know what does this code measure? Is it a chi-square distance or Euclidean distance between two histograms?
i1=imread('blue_4.gif');i1=i1(:,:,1);
[c1,n]=imhist(i1);
c1=c1/size(i1,1)/size(i1,2);
i2=imread('blue_5.jpeg');i2=i2(:,:,1);
[c2,n2]=imhist(i2);
c2=c2/size(i2,1)/size(i2,2);
d=pdist2(c1',c2');

Accepted Answer

Image Analyst
Image Analyst on 1 Aug 2021
It tells you, for every gray level, the number of pixels that are different in the two images. In other words, the difference in the counts between every possible pair of gray levels. Let's say that you had 3 gray levels : 0, 1, and 2. And let's say image 1 had 10 pixels of gray level 0, 20 pixels of gray level 1, and 30 pixels of gray level 2. Let's say image 2 had 35 pixels of gray level 0, 5 pixels of gray level 1, and 20 pixels of gray level 2.
So the difference between image 1's 0 count and the others will be abs(10-35), abs(10-5) and abs(10-20) or [25, 5, 10]
So the difference between image 1's 1 count and the others will be abs(20-35), abs(20-5) and abs(20-20) or [15, 15, 0]
And the difference between image 1's 2 count and the others will be abs(30-35), abs(30-5) and abs(30-20) or [5, 25, 10]
So your d would be
c1 = [10, 20, 30]
c2 = [35, 5, 20]
d = pdist2(c1(:), c2(:))
d =
25 5 10
15 15 0
5 25 10
Now if the histograms would have been identical, the differences would all be 0. So d is sort of an overall metric for how different the histograms are. You could take the mean of d. Or you might just take the mean of the differences on a bin-by-bin basis : mean(abs(c1-c2)) = "average count difference"
md = mean2(d)
mc = mean(abs(c2-c1))
md =
12.222
mc =
16.667
They're just different histogram difference metrics, and there are others you could use. I don't know that one is necessarily any better than the other. Use whatever meets your needs the best. I think the mc mean, average count difference, is a lot easier to understand intuitively than the md one because you're comparing each gray level to itself in the other image rather than comparing each gray level to all other possible gray levels. But basically for both, the metric is zero for a complete histogram match and get bigger the more mismatch in histogram shapes there is.
Note that histograms, or histogram difference metrics, do not necessarily tell you how different the images are. I could take an image and completely randomly scramble/rearrange the pixels so that it looks like basically white noise, or even sort the pixels so that the image looks like a smooth ramp. The image will have the same identical histogram as the original image, but the images would look completely different. (Demo upon request).
  3 Comments

Sign in to comment.

More Answers (1)

DGM
DGM on 1 Aug 2021
Edited: DGM on 1 Aug 2021
By default, the distance metric used by pdist2() is the Euclidean distance.
If you want to use chi-square distance, there's a function in the answer here:

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by