Image background removal problem

Hello,

I’m working on the background removal problem using OpenCV I’m my course work. I’ve already tried GrabCut algorithm, but I’m not satisfied with the results:

    Rect rectangle = new Rect(20, 50, 397, 467);
    Mat result = Mat.zeros(image.size(),CvType.CV_8U);
    Mat bgdModel = Mat.zeros(1,65,CvType.CV_64F);
    Mat fgdModel = Mat.zeros(1,65,CvType.CV_64F);
    Imgproc.grabCut(image, result, rectangle, bgdModel, fgdModel, 2, Imgproc.GC_INIT_WITH_RECT);

    Mat source = new Mat(1, 1, CvType.CV_8U, new Scalar(3));
    Core.compare(result, source, result, Core.CMP_EQ);
    Mat foreground = new Mat(image.size(), CvType.CV_8UC3, new Scalar(255, 255, 255));
    image.copyTo(foreground, result);

In fact, it works correctly only with solid backgrounds.

What other methods could you recommend to me? I’m going to try semantic segmentation method, but maybe there are some more?

@sivay1801 You can use semantic segmentation such as Deeplab for this task. Anyway, I found U2Net works better for background removal tasks. Please check the link below

opencv comes with sample code for grabcut: samples/python/grabcut.py. simply run it. I suspect that your rectangle intialization is to blame. you’re giving it a static area that may or may not be entirely inside the foreground object.