Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

GrabCut Implementation in JAVA

Hello guys I'm new working with OpenCV and I'm trying to implement the grabcut algorithm, well I'm stuck, I've been trying too figured it out how to select the foreground and background with the fine touchups but Im dont know too much python so I cant undestand very well the example. Can anyone here guide me in this process? or at least tell me the steps with the datatype I need?

Here is what i got so far:

    Rect selection = new Rect(x1,y1,x2,y2);
    Mat mask = new Mat();//(imagen.rows(),imagen.cols(),CvType.CV_8UC1);


    Mat cmp = new Mat(1, 1, CvType.CV_8U, new Scalar(3));
    Mat foreground;
    Mat source = new Mat();
    Mat bkground = new Mat();
    Mat frground = new Mat();

    Imgproc.grabCut(imagen, mask,selection, bkground, frground,5,Imgproc.GC_INIT_WITH_RECT);
    Core.compare(mask, cmp,cmp, Core.CMP_EQ);
    foreground = new Mat(imagen.size(),CvType.CV_8UC3,new Scalar(150,255,0));
    imagen.copyTo(foreground,mask);

Can anyone tell me if I'm in the right way? Also I have to tell you that I've already can select with the touchup the part I want as a foreground, and the one as background, but the point is that I dont know how to apply again the grabcut using the new selection....

Thanks in advance

GrabCut Implementation in JAVA

Hello guys I'm Thanks Sudesh, could you explain a little bit the code please?, Im new working with OpenCV and I'm using OPENCV, also Do you know how to apply the grabcut but instead using INIT_WITH_RECT, use INTI_WITH_MASK? so far what I got is this, probably with Images you can see my real problem:

First you can select the part you want as your ROI in this case the part selected is mark with a gray rectangle :

image description

Applying grabcut with INIT_WITH_RECT the algorithm show this:

image description

As you can see the grabcut select a part that is not wanted so Im trying to implement the grabcut algorithm, well I'm stuck, I've been trying too figured enhance the selection with doing some touchups in the parts you want as foreground and as a background, in this example foreground is mark with blue and foregroun with red as below:

image description

Here comes the problem, how to apply again the grabcut algorithm but now indicating that the blue lines are foreground and the red lines are background? I have read about it out how to select the foreground and background with the fine touchups but Im dont and saw the python examples but in python the use of arrays is far different from java. What I know too much python so I cant undestand very well the example. Can anyone here guide me in this process? or at least tell me the steps with the datatype I need?

Here is what i got so far:

    Rect selection = new Rect(x1,y1,x2,y2);
    Mat mask = new Mat();//(imagen.rows(),imagen.cols(),CvType.CV_8UC1);


    Mat cmp = new Mat(1, 1, CvType.CV_8U, new Scalar(3));
    Mat foreground;
    Mat source = new Mat();
    Mat bkground = new Mat();
    Mat frground = new Mat();

    Imgproc.grabCut(imagen, mask,selection, bkground, frground,5,Imgproc.GC_INIT_WITH_RECT);
    Core.compare(mask, cmp,cmp, Core.CMP_EQ);
    foreground = new Mat(imagen.size(),CvType.CV_8UC3,new Scalar(150,255,0));
    imagen.copyTo(foreground,mask);

Can anyone tell and correct me if I'm in the right way? Also wrong, i have to modify the mask and reapply the grabcut algorithm, but when I try this I have to tell you that I've already can select with the touchup the part I want as a foreground, and the one as background, but the point is that I dont know how to apply again the grabcut using the new selection....found this error:

image description

Could you give me a hand with this?

Thanks in advance

GrabCut Implementation in JAVA

Thanks Sudesh, could you explain a little bit the code please?, Hi everyone, Im new using OPENCV, also Do you know how to apply the grabcut but instead using INIT_WITH_RECT, use INTI_WITH_MASK? so far what I got is this, probably with Images you can see my real problem:

First you can select the part you want as your ROI in this case the part selected is mark with a gray rectangle :

image description

Applying grabcut with INIT_WITH_RECT the algorithm show this:

image description

As you can see the grabcut select a part that is not wanted so Im trying to enhance the selection with doing some touchups in the parts you want as foreground and as a background, in this example foreground is mark with blue and foregroun with red as below:

image description

Here comes the problem, how to apply again the grabcut algorithm but now indicating that the blue lines are foreground and the red lines are background? I have read about it and saw the python examples but in python the use of arrays is far different from java. What I know and correct me if I'm wrong, i have to modify the mask and reapply the grabcut algorithm, but when I try this I found this error:

image description

Error:

OpenCV Error: Assertion failed (!bgdSamples.empty() && !fgdSamples.empty()) in initGMMs, file C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\imgproc\src\grabcut.cpp, line 379 Exception in thread "AWT-EventQueue-0" CvException [org.opencv.core.CvException: cv::Exception: C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\imgproc\src\grabcut.cpp:379: error: (-215) !bgdSamples.empty() && !fgdSamples.empty() in function initGMMs ]

Could you give me a hand with this?

Thanks in advance

GrabCut Implementation in JAVA

Hi everyone, Im new using OPENCV, also Do you know how to apply the grabcut but instead using INIT_WITH_RECT, use INTI_WITH_MASK? so far what I got is this, probably with Images you can see my real problem:

First you can select the part you want as your ROI in this case the part selected is mark with a gray rectangle :

image description

Applying grabcut with INIT_WITH_RECT the algorithm show this:

image description

As you can see the grabcut select a part that is not wanted so Im trying to enhance the selection with doing some touchups in the parts you want as foreground and as a background, in this example foreground is mark with blue and foregroun background with red as below:

image description

Here comes the problem, how to apply again the grabcut algorithm but now indicating that the blue lines are foreground and the red lines are background? I have read about it and saw the python examples but in python the use of arrays is far different from java. What I know and correct me if I'm wrong, i have to modify the mask and reapply the grabcut algorithm, but when I try this I found this error:

image description

Error:

OpenCV Error: Assertion failed (!bgdSamples.empty() && !fgdSamples.empty()) in initGMMs, file C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\imgproc\src\grabcut.cpp, line 379 Exception in thread "AWT-EventQueue-0" CvException [org.opencv.core.CvException: cv::Exception: C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\imgproc\src\grabcut.cpp:379: error: (-215) !bgdSamples.empty() && !fgdSamples.empty() in function initGMMs ]

Could you give me a hand with this?

Thanks in advance