# Histogram Chi-Square Distance Metric (Bug #2199)

**Description**

The Chi-Sqaure formula looks to be incorrect. I believe it should be SUM - H2)^2 / (H1 + H2) ).

Add: "+ H2"

http://docs.opencv.org/modules/imgproc/doc/histograms.html?highlight=histogram#double cvCompareHist(const CvHistogram* hist1, const CvHistogram* hist2, int method)

**Related issues**
(Add)

### History

#### Updated by Robert Witkowski about 2 years ago

Nathaniel Quillin wrote:

The Chi-Sqaure formula looks to be incorrect. I believe it should be SUM - H2)^2 / (H1 + H2) ).

Add: "+ H2"

http://docs.opencv.org/modules/imgproc/doc/histograms.html?highlight=histogram#double cvCompareHist(const CvHistogram* hist1, const CvHistogram* hist2, int method)

in some papers as this one (shape context, hist-comparison) there is also a factor 0.5 included, see URL:

http://www.eecs.berkeley.edu/Research/Projects/CS/vision/shape/belongie-pami02.pdf

#### Updated by Robert Witkowski about 2 years ago

Robert Witkowski wrote:

Nathaniel Quillin wrote:

The Chi-Sqaure formula looks to be incorrect. I believe it should be SUM - H2)^2 / (H1 + H2) ).

Add: "+ H2"

http://docs.opencv.org/modules/imgproc/doc/histograms.html?highlight=histogram#double cvCompareHist(const CvHistogram* hist1, const CvHistogram* hist2, int method)

in some papers as this one (shape context, hist-comparison) there is also a factor 0.5 included, see URL:

http://www.eecs.berkeley.edu/Research/Projects/CS/vision/shape/belongie-pami02.pdf

ps: i've just checked that the '+ H2' is correctly implemented, already (see cvhistogram.cpp, google). ONLY THE DOCUMENTACION IS WRONG. in cvhistogram.cpp you can see that the variable b is calculated using in your notation b = H1 + H2 (here b = ptr1[i] + ptr2[i]):

case CV_COMP_CHISQR:

for( i = 0; i < total; i++ ) {

double a = ptr1[i] - ptr2[i];

double b = ptr1[i] + ptr2[i];

if( b != 0 )

result += a*a/b;

}

break;

#### Updated by Vadim Pisarevsky about 2 years ago

thanks for the report!

looks like there are different opinions on how the formula should look like. We now use the formula from Wikipedia.

See also #1263

**Status**changed from*Open*to*Cancelled***Assignee**set to*Vadim Pisarevsky*

#### Updated by Ming-Ming Cheng 5 months ago

Both documentation and implementation is wrong currently. The Chi-Sqaure distance, as documented in the famous paper ( http://www.eecs.berkeley.edu/Research/Projects/CS/vision/shape/belongie-pami02.pdf ), should be

Could someone help to update the code in Histogram.cpp from

@

if( method == CV_COMP_CHISQR )
{

for( j = 0; j < len; j++ )
{

double a = h1[j] - h2[j];

double b = h1[j];

if( fabs(b) > DBL_EPSILON )

result += a*a/b;

}

}

@

to

@

if( method == CV_COMP_CHISQR )
{

for( j = 0; j < len; j++ )
{

double a = h1[j] - h2[j];

double b = h1[j] + h2[j];

if( fabs(b) > DBL_EPSILON )

result += 0.5*a*a/b;

}

}

@

I didn't find the one mentioned in "... the formula from Wikipedia.". Could someone post a link and we should correct that one as well.

**Target version**changed from*2.4.3*to*3.0***Status**changed from*Cancelled*to*Incomplete***Assignee**deleted ()*Vadim Pisarevsky*

#### Updated by Jewel James 2 months ago

hey,

i have been using this method. id like to help. il refer with my professors,come to a conclusion and add back. im new to the procedure -> git and contributions. Should i get assigned or something?