分水岭算法没有提供我期望的结果Java | OpenCV的

时间:2017-06-12 22:05:08

标签: java opencv image-processing watershed

我正在尝试在此图像中找到凹坑中心。

enter image description here

我使用了分水岭算法,我认为它用于在图像上找到凹坑,但结果图像对我没什么用。我是否正确使用了这个算法?我应该用另一种方式吗?这是我的代码:

public static void main (String args[]){

        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        Mat image = new Mat();
        image=Imgcodecs.imread("cellar.jpg");

        if ( image == null) System.out.println("not working");
        else System.out.println("ok");

        Mat binaryImage = new Mat();
        Imgproc.cvtColor(image, binaryImage, Imgproc.COLOR_BGR2GRAY);
        Imgproc.threshold(binaryImage,binaryImage,0,255,Imgproc.THRESH_BINARY | Imgproc.THRESH_OTSU);

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

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

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

         if(markers==null) System.out.println("ojojoj");
         else System.out.println("perfect");

         WatershedSegmenter segmenter = new WatershedSegmenter();
         segmenter.setMarkers(markers);
         Mat result = segmenter.process(image);

         Imgcodecs.imwrite("resultMat.png", result);
}        


public class WatershedSegmenter{
    public Mat markers;

    public void setMarkers(Mat markerImage)
    {
        markers = new Mat();
        markerImage.convertTo(markers, CvType.CV_32S);
    }

    public Mat process(Mat image)
    {
        Imgproc.watershed(image, markers);
        markers.convertTo(markers,CvType.CV_8U);
        return markers;
    }
}

这是结果图片: enter image description here

感谢您的帮助:)

0 个答案:

没有答案