OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Sun, 23 Dec 2012 08:34:48 -0600Question About Histogram Comparisonhttp://answers.opencv.org/question/5426/question-about-histogram-comparison/Hello,
I need to compare two histograms for my project. I am working on normalized histograms by using the following function call:
//Assuming that histogram is a cv::Mat and is valid
cv::normalize(histogram, histogram, 1.0);
I am using cv::compareHist() to compare the histograms. I have tried the all four methods for comparison with OpenCV macros: CV_COMP_CORREL, CV_COMP_CHISQR, CV_COMP_INTERSECT, CV_COMP_BHATTACHARYYA.
The problem is that: When analyzing the comparison results, CV_COMP_INTERSECT comparison method produce values bigger than 1 although two histograms are normalized. It is stated on Learning OpenCV: Computer Vision with the OpenCV library (page 202):
> If both histograms are normalized to 1, then a perfect match is 1 and a
total mismatch is 0.
What you think about this?
Best wishes.Sat, 22 Dec 2012 10:47:06 -0600http://answers.opencv.org/question/5426/question-about-histogram-comparison/Answer by Michael Burdinov for <p>Hello,
I need to compare two histograms for my project. I am working on normalized histograms by using the following function call:</p>
<pre><code>//Assuming that histogram is a cv::Mat and is valid
cv::normalize(histogram, histogram, 1.0);
</code></pre>
<p>I am using cv::compareHist() to compare the histograms. I have tried the all four methods for comparison with OpenCV macros: CV_COMP_CORREL, CV_COMP_CHISQR, CV_COMP_INTERSECT, CV_COMP_BHATTACHARYYA.</p>
<p>The problem is that: When analyzing the comparison results, CV_COMP_INTERSECT comparison method produce values bigger than 1 although two histograms are normalized. It is stated on Learning OpenCV: Computer Vision with the OpenCV library (page 202):</p>
<blockquote>
<p>If both histograms are normalized to 1, then a perfect match is 1 and a
total mismatch is 0.</p>
</blockquote>
<p>What you think about this?</p>
<p>Best wishes.</p>
http://answers.opencv.org/question/5426/question-about-histogram-comparison/?answer=5442#post-id-5442When you use [CV_COMP_INTERSECT](http://docs.opencv.org/modules/imgproc/doc/histograms.html?highlight=comparehist#comparehist) comparison method on two histograms that are perfectly match, your result will be equal to sum of values of a histogram. i.e. your result will be equal to L1 norm of histogram.
When you used [normalize()](http://docs.opencv.org/modules/core/doc/operations_on_arrays.html?highlight=normalize#normalize) function, you set only 3 first parameters. And so other parameters were used with their default values. By default it uses L2 norm. i.e. after using normalize(), sum of squares of values of histogram was set to 1.
To fix your problem you should use:
cv::normalize(histogram, histogram, 1.0, 0.0, NORM_L1);Sun, 23 Dec 2012 07:26:56 -0600http://answers.opencv.org/question/5426/question-about-histogram-comparison/?answer=5442#post-id-5442Comment by enienws for <p>When you use <a href="http://docs.opencv.org/modules/imgproc/doc/histograms.html?highlight=comparehist#comparehist">CV_COMP_INTERSECT</a> comparison method on two histograms that are perfectly match, your result will be equal to sum of values of a histogram. i.e. your result will be equal to L1 norm of histogram.</p>
<p>When you used <a href="http://docs.opencv.org/modules/core/doc/operations_on_arrays.html?highlight=normalize#normalize">normalize()</a> function, you set only 3 first parameters. And so other parameters were used with their default values. By default it uses L2 norm. i.e. after using normalize(), sum of squares of values of histogram was set to 1.</p>
<p>To fix your problem you should use:</p>
<pre><code>cv::normalize(histogram, histogram, 1.0, 0.0, NORM_L1);
</code></pre>
http://answers.opencv.org/question/5426/question-about-histogram-comparison/?comment=5445#post-id-5445Thank you so much. I have understood where the mistake was.Sun, 23 Dec 2012 08:34:48 -0600http://answers.opencv.org/question/5426/question-about-histogram-comparison/?comment=5445#post-id-5445