[JAVA] Desktop: OpenCV Watershed

Goal
Test OpenCV Watershed.

OpenCV_Watershed.java


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

public class OpenCV_Watershed {
    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);
            Mat gray=new Mat(source.rows(),source.cols(),CvType.CV_8UC1);
            Imgproc.cvtColor(source, gray, Imgproc.COLOR_BGR2GRAY);
            Mat binary=Mat.zeros(gray.rows(),gray.cols(),CvType.CV_8UC1);
            Imgproc.threshold(gray, binary, 100, 255, Imgproc.THRESH_BINARY);


            Mat fg=new Mat(source.size(),CvType.CV_8U);
            Imgproc.erode(binary, fg, new Mat(),new Point(-1,-1),2);

            Mat bg=new Mat(source.size(),CvType.CV_8U);
            Imgproc.dilate(binary, bg, new Mat(),new Point(-1,-1),3);
            Imgproc.threshold(bg,bg, 1, 128, Imgproc.THRESH_BINARY_INV);

            Mat markers=new Mat(binary.size(),CvType.CV_8U,new Scalar(0));
            Core.add(fg, bg, markers);


            Mat tmp=new Mat(binary.size(),CvType.CV_8U,new Scalar(0));
            Mat tmper=new Mat(binary.size(),CvType.CV_8U,new Scalar(0));
            Mat tmper1=new Mat(binary.size(),CvType.CV_8U,new Scalar(0));


            //Watershed
            markers.convertTo(tmper, CvType.CV_32S);
            Imgproc.watershed(source, tmper);
            tmper.convertTo(tmp, CvType.CV_8U);
            //markers.convertTo(tmp, CvType.CV_8U);
            tmper.convertTo(tmper1, CvType.CV_8U,255,255);
            Imgcodecs.imwrite("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\lena_watered.jpg ", markers);
            Imgcodecs.imwrite("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\lena_watered1.jpg ",tmp);
            Imgcodecs.imwrite("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\lena_watered2.jpg ",tmper);
            Imgcodecs.imwrite("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\lena_watered3.jpg ",tmper1);

        }catch (Exception e) {
            System.out.println("error: " + e.getMessage());
        }
    }
}
Result
![lena_watered.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/276243/95a429f8-217c-d761-b0b4-9d350af7970d.jpeg) ![lena_watered1.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/276243/8ada148b-fb7c-d381-299f-c87e23361a43.jpeg) ![lena_watered2.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/276243/91dd711f-a31c-c220-4fd1-bceddf38e67b.jpeg) ![lena_watered3.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/276243/fafb4dbd-b54b-5f77-e38c-3081e21f55a0.jpeg)

Recommended Posts

Desktop: OpenCV Watershed
Desktop: OpenCV Threshold
Desktop: OpenCV BilateralFilterBlur
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 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 Fill ConvexPoly
Desktop: OpenCV Grab Cut
Desktop: OpenCV Sharpness Gui
Desktop: OpenCV Color Change
Desktop: OpenCV Adaptive Threshold
Desktop: OpenCV Draw Circle
Desktop: OpenCV Fill Poly
Desktop: OpenCV Mean Filter
Desktop: OpenCV Java Repository
Desktop: OpenCV pyrMeanShift Filter
Desktop: OpenCV OpticalFlow PyrLK
Desktop: OpenCV Virtual Piano
Desktop: OpenCV merge Picture
Desktop: OpenCV Scharr Filter
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
Desktop: OpenCV Pencil Sketch
Desktop: OpenCV Color Map