Ask Your Question
0

Weird problem with Mahalanobis distance function

asked 2014-03-02 07:27:38 -0600

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

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.

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted
1

answered 2014-03-02 08:18:00 -0600

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)).

edit flag offensive delete link more

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 ( 2014-03-02 10:52:01 -0600 )edit
0

answered 2017-12-24 00:44:38 -0600

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

edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-03-02 07:27:38 -0600

Seen: 865 times

Last updated: Mar 02 '14