Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Mahalanobis function returns squared mahalanobis distance?

According to documentation, Mahalanobis distance returns the weighted distance between 2 vectors sqrt(sum_of_weighted_squared_diferences_with_mean) But, in the code, it doesn't seem to calculate the sqrt. It seems to return the square of the Mahalanobis distance. Is is possible?

double MahalanobisImpl(const Mat& v1, const Mat& v2, const Mat& icovar, double diff_buffer /[len]/, int len /=v1.total()*/) { CV_INSTRUMENT_REGION();

Size sz = v1.size(); double result = 0;

sz.width *= v1.channels(); if (v1.isContinuous() && v2.isContinuous()) { sz.width *= sz.height; sz.height = 1; }

{ const T* src1 = v1.ptr<t>(); const T* src2 = v2.ptr<t>(); size_t step1 = v1.step/sizeof(src1[0]); size_t step2 = v2.step/sizeof(src2[0]); double* diff = diff_buffer; const T* mat = icovar.ptr<t>(); size_t matstep = icovar.step/sizeof(mat[0]);

for (; sz.height--; src1 += step1, src2 += step2, diff += sz.width)
{
    for (int i = 0; i < sz.width; i++)
        diff[i] = src1[i] - src2[i];
}

diff = diff_buffer;
for (int i = 0; i < len; i++, mat += matstep)
{
    double row_sum = 0;
    int j = 0;

if CV_ENABLE_UNROLLED

for(; j <= len - 4; j += 4 ) row_sum += diff[j]mat[j] + diff[j+1]mat[j+1] + diff[j+2]mat[j+2] + diff[j+3]mat[j+3];

endif

for (; j < len; j++) row_sum += diff[j]*mat[j]; result += row_sum * diff[i]; } } return result; }

Mahalanobis function returns squared mahalanobis distance?

According to documentation, Mahalanobis distance returns the weighted distance between 2 vectors sqrt(sum_of_weighted_squared_diferences_with_mean) But, in the code, it doesn't seem to calculate the sqrt. It seems to return the square of the Mahalanobis distance. Is is possible?

double MahalanobisImpl(const Mat& v1, const Mat& v2, const Mat& icovar, double diff_buffer /[len]/, int len /=v1.total()*/)
{
CV_INSTRUMENT_REGION();

CV_INSTRUMENT_REGION(); Size sz = v1.size(); double result = 0;

0; sz.width *= v1.channels(); if (v1.isContinuous() && v2.isContinuous()) { sz.width *= sz.height; sz.height = 1; }

} { const T* src1 = v1.ptr<t>(); v1.ptr<T>(); const T* src2 = v2.ptr<t>(); v2.ptr<T>(); size_t step1 = v1.step/sizeof(src1[0]); size_t step2 = v2.step/sizeof(src2[0]); double* diff = diff_buffer; const T* mat = icovar.ptr<t>(); icovar.ptr<T>(); size_t matstep = icovar.step/sizeof(mat[0]);

icovar.step/sizeof(mat[0]);
for (; sz.height--; src1 += step1, src2 += step2, diff += sz.width)
{
 {
 for (int i = 0; i < sz.width; i++)
 diff[i] = src1[i] - src2[i];
}
 }
diff = diff_buffer;
 for (int i = 0; i < len; i++, mat += matstep)
{
 {
 double row_sum = 0;
 int j = 0;

if CV_ENABLE_UNROLLED

#if CV_ENABLE_UNROLLED for(; j <= len - 4; j += 4 ) row_sum += diff[j]mat[j] diff[j]*mat[j] + diff[j+1]mat[j+1] diff[j+1]*mat[j+1] + diff[j+2]mat[j+2] diff[j+2]*mat[j+2] + diff[j+3]mat[j+3];

endif

diff[j+3]*mat[j+3]; #endif for (; j < len; j++) row_sum += diff[j]*mat[j]; result += row_sum * diff[i]; } } return result; }

}