Grabcut algorithim - mask mode in Android

asked 2014-08-26 04:14:32 -0600

updated 2020-09-23 14:30:55 -0600

Hi ALL, I am using grabcut mask mode algorithm in Android and for some reason iteration through pixels is not working.. Could you please help me. Please see the code below:

for(int i=0; i<maskImg.cols; i++)    
    for(int j=0; j<maskImg.rows; j++)    
    {               
        //if it's red, make it black
        if ((int)maskImg.at<cv::Vec3b>(j,i)[0]==0 && (int)maskImg.at<cv::Vec3b>(j,i)[1] == 0 && (int)maskImg.at<cv::Vec3b>(j,i)[2] == 255) {

            //the whole mask is black so this is redundant
            mask.at<uchar>(j,i)= GC_BGD;  //GC_BGD := 0 := black 

        }           

        //if it's green, make it white
        if ((int)maskImg.at<cv::Vec3b>(j,i)[0]==0 && (int)maskImg.at<cv::Vec3b>(j,i)[1] == 255 && (int)maskImg.at<cv::Vec3b>(j,i)[2] == 0) {    

                mask.at<uchar>(j,i) = GC_FGD; //GC_FGD:= 1 := white 

        }
    }
edit retag flag offensive close merge delete

Comments

I think what you are doing here is quite weird

int)maskImg.at&lt;cv::Vec3b&gt;(j,i)[0]

This should mean that the type of the image pixel is of Vec3b. That is impossible. I am guessing this should be at least made uchar. I think something like this in your if loop should fix it.

if ((int)maskImg.at&lt;uchar&gt;(j,i)[0]==0 &amp;&amp; (int)maskImg.at&lt;uchar&gt;(j,i)[1] == 0 &amp;&amp; (int)maskImg.at&lt;uchar&gt;(j,i)[2] == 255)

StevenPuttemans gravatar imageStevenPuttemans ( 2014-08-26 07:17:34 -0600 )edit
1

what if it's neither plain green or plain red ?

(also, please explain: "isn't working" ..)

berak gravatar imageberak ( 2014-08-26 08:01:11 -0600 )edit