Comparing histograms

Histograms can be compared with each other in other to get some insight into the content of an image. OpenCV allows histogram comparison using a method called compareHist, which requires the comparison method to be set first. The following example code depicts how this function can be used to calculate the result of comparison between two histograms calculated using previous calls to the calcHist function:

HistCompMethods method = HISTCMP_CORREL; 
double result = compareHist(histogram1, histogram2, method); 

histogram1 and histogram2, in the preceding example, are simply histograms of two different images, or different channels of an image. method, on the other hand, which must contain a valid entry from the HistCompMethods enum, defines the comparison algorithm used by the compareHist function and it can be any one of the following methods:

  • HISTCMP_CORREL, for the Correlation method
  • HISTCMP_CHISQR, for the Chi-square method
  • HISTCMP_INTERSECT, for the Intersection method
  • HISTCMP_BHATTACHARYYA, for the Bhattacharyya distance method
  • HISTCMP_HELLINGER, same as HISTCMP_BHATTACHARYYA
  • HISTCMP_CHISQR_ALT, for the Alternative Chi-square method
  • HISTCMP_KL_DIV, for the Kullback-Leibler divergence method

You can refer to the latest OpenCV documentation to get more information about the mathematical details of each method, and how and what properties of histograms are used by them. The same can be said about the interpretation of the results of any method. Let's see what this means with an example. Using the following sample code, we can output the result of all histogram-comparison methods:

cout << "HISTCMP_CORREL: " << 
  compareHist(histogram1, histogram2, HISTCMP_CORREL) 
    << endl; 
cout << "HISTCMP_CHISQR: " << 
  compareHist(histogram1, histogram2, HISTCMP_CHISQR) 
    << endl; 
cout << "HISTCMP_INTERSECT: " << 
  compareHist(histogram1, histogram2, HISTCMP_INTERSECT) 
    << endl; 
cout << "HISTCMP_BHATTACHARYYA: " << 
  compareHist(histogram1, histogram2, HISTCMP_BHATTACHARYYA) 
    << endl; 
cout << "HISTCMP_HELLINGER: " << 
  compareHist(histogram1, histogram2, HISTCMP_HELLINGER) 
    << endl; 
cout << "HISTCMP_CHISQR_ALT: " << 
  compareHist(histogram1, histogram2, HISTCMP_CHISQR_ALT) 
    << endl; 
cout << "HISTCMP_KL_DIV: " << 
  compareHist(histogram1, histogram2, HISTCMP_KL_DIV) 
    << endl; 

We use the same example image we used throughout this chapter to calculate both histogram1 and histogram2, or, in other words, if we compare one histogram with an equal histogram, here's what we would get:

HISTCMP_CORREL: 1 
HISTCMP_CHISQR: 0 
HISTCMP_INTERSECT: 426400 
HISTCMP_BHATTACHARYYA: 0 
HISTCMP_HELLINGER: 0 
HISTCMP_CHISQR_ALT: 0 
HISTCMP_KL_DIV: 0

Notice how distance- and divergence-based methods return a value of zero, while correlation returns a value of one, for exact correlation. All of the results in the preceding output mean equal histograms. Let's shed more light on this by calculating the histograms from the following two images:

The following results would be created if the image on the left is used to create histogram1 and the image on the right is used to create histogram2, or, in other words, an arbitrary bright image is compared with an arbitrary dark image:

HISTCMP_CORREL: -0.0449654 
HISTCMP_CHISQR: 412918 
HISTCMP_INTERSECT: 64149 
HISTCMP_BHATTACHARYYA: 0.825928 
HISTCMP_HELLINGER: 0.825928 
HISTCMP_CHISQR_ALT: 1.32827e+06 
HISTCMP_KL_DIV: 3.26815e+06 

It's important to note that the order of the histograms being passed to the compareHist function matters in some cases, such as when HISTCMP_CHISQR is used as the method. Here are the results with histogram1 and histogram2 passed in reverse order to the compareHist function:

HISTCMP_CORREL: -0.0449654 
HISTCMP_CHISQR: 3.26926e+06 
HISTCMP_INTERSECT: 64149 
HISTCMP_BHATTACHARYYA: 0.825928 
HISTCMP_HELLINGER: 0.825928 
HISTCMP_CHISQR_ALT: 1.32827e+06 
HISTCMP_KL_DIV: 1.15856e+07 

Comparing histograms is extremely useful, especially when we need to get a better and more meaningful impression of changes across various images. For instance, comparing histograms of consecutive frames from a camera can give us an idea of the intensity of change between those consecutive frames.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.123.73