Ask Your Question
1

Mahalanobis function returns squared mahalanobis distance?

asked 2020-06-25 04:10:09 -0600

ffolch gravatar image

updated 2020-06-25 04:26:49 -0600

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 flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2020-06-25 08:35:37 -0600

Eduardo gravatar image

There is no issue.

This is done here.

edit flag offensive delete link more

Comments

nice catch !

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

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

Question Tools

1 follower

Stats

Asked: 2020-06-25 04:10:09 -0600

Seen: 264 times

Last updated: Jun 25 '20