Ask Your Question
0

Weird problem with Mahalanobis distance function

asked Mar 2 '14

updated Mar 3 '14

berak gravatar image

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.

Preview: (hide)

2 answers

Sort by » oldest newest most voted
1

answered Mar 2 '14

Guanta gravatar image

The two input-vectors (in your case 'pixel' and 'mn') need to have the same format. So both Vec3f or both Mat1f(1,3) or both Mat1f(3,1) (and of course your inverse covariance matrix 'i_cvm' should have the type Mat1f(3,3)).

Preview: (hide)

Comments

I kinda solved it by myself. One of the vectors was defined as unsigned int, and I changed that to double.

ilchiodo gravatar imageilchiodo (Mar 2 '14)edit
0

answered Dec 24 '17

refa gravatar image

Hi and merry Christmas I have started OpenCV recently and I have a same project about color segmentation. I don’t have any experience about mahalanobis distance algorithm. I saw you partial code but I don’t understand how you have defined mn, i-cvm or threshold, .. .in your code I will be appreciated to send me whole of your code if it is possible for you Thanks Refa

Preview: (hide)

Question Tools

Stats

Asked: Mar 2 '14

Seen: 906 times

Last updated: Mar 02 '14