Sean M.
Sean M.

Reputation: 593

Calculate mahalanobis distance

From a bunch of images I, a mean color C_m evolves. Now I want to obtain a distance image, using mahalanobis distance, in which each pixels mahalanobis distance to the C_m gets calculated. I can't get OpenCV's Mahalanobis() function to work. I calculate the calcCovarMatrix with all pixel colors of I, invert it and pass it to Mahalanobis(). Next I'm looping over the new image to calculate the distance for every single pixel:

  Mat covar, incovar, mean;
  calcCovarMatrix(...);
  invert(covar,incovar,DECOMP_SVD);

  for (int row = 0; row < image.rows; ++row) {
        for (int col = 0; col < image.cols; ++col) {
            Scalar color = image.at<Vec3b>(row, col);
        double m_dist = Mahalanobis(color, mean, incovar);
        }
  }

Resulting in: OpenCV Error: Assertion failed (type == v2.type() && type == icovar.type() && sz == v2.size() && len == icovar.rows && len == icovar.cols) in Mahalanobis, file /tmp/opencv-8GA996/opencv-2.4.9/modules/core/src/matmul.cpp,

What's my mistake here? Thanks in advance!

Upvotes: 1

Views: 2809

Answers (1)

berak
berak

Reputation: 39816

Mahalanobis is not working on single pixels, but on whole images. so instead try :

double dist = Mahalanobis( image1, image2, invcovar );

Upvotes: 1

Related Questions