Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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