[JAVA] Desktop: OpenCV Grab Cut

Goal
Test OpenCV grab cut.

OpenCV_GrabCut.java


import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

public class OpenCV_GrabCut {

    public static void main( String[] args )
    {
        try{
            System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
            Mat source = Imgcodecs.imread("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\lena.jpg ",
                    Imgcodecs.CV_LOAD_IMAGE_COLOR);
            bgSubtracting(source);
        }catch (Exception e) {
            System.out.println("error: " + e.getMessage());
        }
    }

    private static void bgSubtracting(Mat img) {
        Mat firstMask = new Mat();
        Mat bgModel = new Mat();//grabCut 內 part math use
        Mat fgModel = new Mat();//grabCut 內 part math use
        Mat mask;
        Mat source = new Mat(1, 1, CvType.CV_8U, new Scalar(3.0));
        //Definition rectangle
        Rect rect = new Rect(0, 0,150,200);

        ///Execution grab Cut(Split)line
        Imgproc.grabCut(img, firstMask, rect, bgModel, fgModel, 1, 0 );


        //Foreground
        Core.compare(firstMask, source, firstMask, Core.CMP_EQ);
        //Generated export image
        Mat foreground = new Mat(img.size(), CvType.CV_8UC3, new Scalar(255,
                255, 255));
        //Duplicate foreground data,Consistent area
        img.copyTo(foreground, firstMask);

        mask = new Mat(foreground.size(), CvType.CV_8UC1, new Scalar(255, 255, 255));

        Imgproc.cvtColor(foreground, mask, Imgproc.COLOR_BGR2GRAY);
        Imgproc.threshold(mask, mask, 254, 255, Imgproc.THRESH_BINARY_INV);
        //Post-expulsion replacement color
        Mat vals = new Mat(1, 1, CvType.CV_8UC3, new Scalar(255.0));

        img.setTo(vals, mask);
        Imgcodecs.imwrite("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\grab_cut.jpg ", img);
    }

}
Result
![grab_cut.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/276243/f6806cf7-5ca5-8ba5-ac3e-f2c8d2a90382.jpeg)

Recommended Posts

Desktop: OpenCV Grab Cut
Desktop: OpenCV BilateralFilterBlur
Desktop: OpenCV Dilate
Desktop: OpenCV Expand
Desktop: OpenCV Affine
Desktop: OpenCV Emboss
Desktop: OpenCV CLAHE
Desktop: OpenCV Ellipse2Poly
Desktop: OpenCV HDR
Desktop: OpenCV Polylines
Desktop: OpenCV Denoise
Desktop: OpenCV Sharpness
Desktop: OpenCV Concat
Desktop: OpenCV OpenCV_SalonUseBlurAddWeighted
Desktop: OpenCV Mosaic
Desktop: OpenCV Erode
Desktop: OpenCV Denoise
Desktop: OpenCV Rectangle
Desktop: OpenCV Watershed
Desktop: OpenCV Text
Desktop: OpenCV Inpaint
Desktop: OpenCV NormalizeBlur
Desktop: OpenCV StereoSGBM
Desktop: OpenCV Spot
Desktop: OpenCV Canny
Desktop: OpenCV Denoise3
Desktop: OpenCV Histogram
Desktop: OpenCV Dft
Desktop: OpenCV Decolor
Desktop: OpenCV FaceDetector
Desktop: OpenCV Denoise2
Desktop: OpenCV StereoBM
Desktop: OpenCV Kirsch Filter
Desktop: OpenCV Laplacian Filter 2
Desktop: OpenCV Illumination Change
Desktop: OpenCV Add WaterMark
Desktop: OpenCV Sharpness Gui
Desktop: OpenCV Color Change
Desktop: OpenCV Freichennel Filter
Desktop: OpenCV Adaptive Threshold
Desktop: OpenCV Draw Circle
Desktop: OpenCV Fill Poly
Desktop: OpenCV Mean Filter
Desktop: OpenCV Java Repository
Desktop: OpenCV Sobel Filter2
Desktop: OpenCV pyrMeanShift Filter
Desktop: OpenCV OpticalFlow PyrLK
Desktop: OpenCV Virtual Piano
Desktop: OpenCV merge Picture
Desktop: OpenCV Scharr Filter
Desktop: Opencv webcam preview
Desktop: OpenCV Laplacian Filter
Desktop: OpenCV Median Blur
Desktop: OpenCV Add Broad
Desktop: OpenCV Robinson Filter
Desktop: OpenCV record video
Desktop: OpenCV Texture Flatting
Desktop: OpenCV Flood Fill
Desktop: OpenCV color space
Desktop: OpenCV SqrBox Filter
Desktop: OpenCV resize image