[JAVA] Bureau: 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

Bureau: OpenCV Watershed
Bureau: seuil OpenCV
Bureau: OpenCV BilateralFilterBlur
Bureau: OpenCV Expand
Bureau: OpenCV Affine
Bureau: OpenCV Emboss
Bureau: OpenCV CLAHE
Bureau: OpenCV Ellipse2Poly
Bureau: OpenCV HDR
Bureau: Polylignes OpenCV
Bureau: OpenCV Denoise
Bureau: netteté OpenCV
Bureau: OpenCV Concat
Bureau: OpenCV OpenCV_SalonUseBlurAddWeighted
Bureau: OpenCV Mosaic
Bureau: OpenCV Erode
Bureau: OpenCV Denoise
Bureau: Rectangle OpenCV
Bureau: Texte OpenCV
Bureau: OpenCV Inpaint
Bureau: OpenCV NormalizeBlur
Bureau: OpenCV StereoSGBM
Bureau: OpenCV Spot
Bureau: OpenCV Canny
Bureau: OpenCV Denoise3
Bureau: histogramme OpenCV
Bureau: OpenCV Dft
Bureau: OpenCV Decolor
Bureau: OpenCV FaceDetector
Bureau: OpenCV Denoise2
Bureau: OpenCV StereoBM
Bureau: Filtre OpenCV Kirsch
Bureau: Filtre laplacien OpenCV 2
Bureau: Changement d'éclairage OpenCV
Bureau: OpenCV Add WaterMark
Bureau: OpenCV Fill ConvexPoly
Bureau: OpenCV Grab Cut
Bureau: OpenCV Sharpness Gui
Bureau: Changement de couleur OpenCV
Bureau: seuil adaptatif OpenCV
Bureau: OpenCV Draw Circle
Bureau: OpenCV Fill Poly
Bureau: OpenCV Mean Filter
Bureau: référentiel Java OpenCV
Bureau: OpenCV pyrMeanShift Filter
Bureau: OpenCV OpticalFlow PyrLK
Bureau: Piano virtuel OpenCV
Bureau: image de fusion OpenCV
Bureau: Filtre OpenCV Scharr
Bureau: Filtre laplacien OpenCV
Bureau: Flou médian OpenCV
Bureau: OpenCV Add Broad
Bureau: OpenCV Robinson Filter
Bureau: enregistrement vidéo OpenCV
Bureau: Aplatissement de la texture OpenCV
Bureau: OpenCV Flood Fill
Bureau: espace colorimétrique OpenCV
Bureau: Filtre OpenCV SqrBox
Bureau: redimensionner l'image OpenCV
Bureau: Croquis au crayon OpenCV
Bureau: carte des couleurs OpenCV