# 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;
}

edit retag close merge delete

Sort by ยป oldest newest most voted

There is no issue.

This is done here.

more

nice catch !

could you add this here , so the resp. issue can be closed ?

( 2020-06-25 13:51:34 -0600 )edit

Official site

GitHub

Wiki

Documentation

## Stats

Seen: 140 times

Last updated: Jun 25 '20