Ask Your Question

Revision history [back]

Weird problem with Mahalanobis distance function

Hello there! I'm trying to do an image segmentation based on Mahalanobis distance, but the implementation in Visual Studio with OpenCV 2.4.8 throws a weird exception I don't know how to solve.

Here is the snippet:

// cam_frame is a Mat object, while pixel is a std::vector made of 3 elements.

// mn is a 1x3 Mat representing the mean vector, and i_covar has been successfully computed

for (int i=0; i<cam_frame.rows; i++){   //for every pixel,
    for (int j=0; j<cam_frame.cols; j++)
    {
        pixel[0]=cam_frame.at<Vec3b>(i, j)[0];  //B component
        pixel[1]=cam_frame.at<Vec3b>(i, j)[1];  //G component
        pixel[2]=cam_frame.at<Vec3b>(i, j)[2];  //R component

        if (Mahalanobis(mn, pixel, i_cvm)<threshold)    
            mask.at<unsigned char>(i,j)=255;
        else mask.at<unsigned char>(i,j)=0;
    }
}

The Mahalanobis function throws an exception regarding the size of the arguments, but I can't really figure out.

click to hide/show revision 2
retagged

updated 2014-03-03 11:49:16 -0600

berak gravatar image

Weird problem with Mahalanobis distance function

Hello there! I'm trying to do an image segmentation based on Mahalanobis distance, but the implementation in Visual Studio with OpenCV 2.4.8 throws a weird exception I don't know how to solve.

Here is the snippet:

// cam_frame is a Mat object, while pixel is a std::vector made of 3 elements.

// mn is a 1x3 Mat representing the mean vector, and i_covar has been successfully computed

for (int i=0; i<cam_frame.rows; i++){   //for every pixel,
    for (int j=0; j<cam_frame.cols; j++)
    {
        pixel[0]=cam_frame.at<Vec3b>(i, j)[0];  //B component
        pixel[1]=cam_frame.at<Vec3b>(i, j)[1];  //G component
        pixel[2]=cam_frame.at<Vec3b>(i, j)[2];  //R component

        if (Mahalanobis(mn, pixel, i_cvm)<threshold)    
            mask.at<unsigned char>(i,j)=255;
        else mask.at<unsigned char>(i,j)=0;
    }
}

The Mahalanobis function throws an exception regarding the size of the arguments, but I can't really figure out.