Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

GrabCut output shown in greyscale

Hi all ,

I am practicing JavaCV grabcut example given here.

The example given here is in Scala but I have managed to write in Java. On running the example two windows open up , first one containing the input image and second one containing output image in greyscale mode. The problem is would want the output image in color mode with transparent background. I am using OpenCV 2.4.10 and JavaCV 0.10. Below is the code in using and the images shown in output.

import opencv2_cookbook.OpenCVUtils;
import org.bytedeco.javacpp.opencv_core.Mat;
import org.bytedeco.javacpp.opencv_core.Rect;
import static org.bytedeco.javacpp.opencv_highgui.CV_LOAD_IMAGE_COLOR;
import org.bytedeco.javacpp.opencv_imgproc;
import static org.bytedeco.javacpp.opencv_imgproc.*;
public class GrabCut
    public static void main(String[] arg)
        Mat  image = OpenCVUtils.loadMatAndShowOrExit(new File("F:\\images\\set2.jpg"), CV_LOAD_IMAGE_COLOR);

        Rect rectangle = new Rect(0, 0 , 224 , 224);

        Mat result = new Mat();
        int iterCount = 5;
        int mode = GC_INIT_WITH_RECT;

        Mat bgdModel = new Mat();
        Mat fgdModel = new Mat();

        opencv_imgproc.grabCut(image, result, rectangle, bgdModel, fgdModel, iterCount, mode);
        //opencv_core.cvCmpS(result, GC_PR_FGD, result, CV_CMP_EQ);
        opencv_imgproc.threshold(result, result, GC_PR_FGD - 0.5, GC_PR_FGD + 0.5, THRESH_BINARY); , true), "Result foreground mask");

image description

Kindly need ur help.Thanks.