Ask Your Question
0

GrabCut Implementation in JAVA

asked 2015-10-08 18:54:35 -0600

EduardoGG gravatar image

updated 2015-10-11 14:23:40 -0600

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

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-10-11 01:04:46 -0600

iamprivate gravatar image

I am not sure whether the code I am adding is the best way to do it but this is what I use. You can change one or two things (some lines are unnecessary) Hope it helps

    Mat local_image = color_chair.clone();
    Imgproc.grabCut(local_image, mask , rect, new Mat(), new Mat(), 5,choice);
    Mat fg_mask = mask.clone();
    Mat pfg_mask =mask.clone();
    Mat source = new Mat(1, 1, CvType.CV_8U, new Scalar(3.0));
    Core.compare(mask,source,pfg_mask,Core.CMP_EQ);
    source = new Mat(1, 1, CvType.CV_8U, new Scalar(1.0));
    Core.compare(mask,source,fg_mask,Core.CMP_EQ);
    Mat fg_foreground = new Mat(color_chair.size(),color_chair.type(),new Scalar(0,0,0));
    Mat pfg_foreground = new Mat(color_chair.size(),color_chair.type(),new Scalar(0,0,0));
    //display.show(pfg_mask);
    //display.show(fg_mask);
    Core.bitwise_or(pfg_mask, fg_mask, finalMask);
    //display.show(finalMask);
    color_chair.copyTo(fg_foreground,fg_mask);
    color_chair.copyTo(pfg_foreground,pfg_mask);
    Mat foreground = new Mat(fg_foreground.size(),CvType.CV_8UC3);
    Core.bitwise_or(fg_foreground, pfg_foreground, foreground);
edit flag offensive delete link more

Comments

Hello Sudesh, thanks for the reply, could you explian a little bit your code please, Im new using OpenCV and Im getting used to the library, thanks.

EduardoGG gravatar imageEduardoGG ( 2015-10-11 14:16:34 -0600 )edit

Hi EduardoGG , Can u share code please?

kavie gravatar imagekavie ( 2017-06-29 05:46:09 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-10-08 18:54:35 -0600

Seen: 3,685 times

Last updated: Oct 11 '15