我正在尝试使用 OpenCV 的 Java 包装器实现分水岭分割。
这就是我正在做的事情:
public void watershedSegmentation(Mat image)
{
Mat target = new Mat(image.rows(), image.cols(), CvType.CV_8UC3);
Imgproc.cvtColor(image, target, Imgproc.COLOR_BGRA2RGB);
//Conversion to 8UC1 grayscale image
Mat grayScale = new Mat(image.rows(), image.cols(), CvType.CV_64FC1);
Imgproc.cvtColor(image, grayScale, Imgproc.COLOR_BGR2GRAY);
//Otsu's Threshold, applied to the grayscale image
Imgproc.threshold(grayScale, grayScale, 0, 255, Imgproc.THRESH_OTSU);
//constructing a 3x3 kernel for morphological opening
Mat openingKernel = Mat.ones(3,3, CvType.CV_8U);
Imgproc.morphologyEx(grayScale, grayScale, Imgproc.MORPH_OPEN, openingKernel, new Point(-1,-1), 3);
//dilation operation for extracting the background
Imgproc.dilate(grayScale, grayScale, openingKernel, new Point(-1,-1), 1);
Imgproc.watershed(target, grayScale);
}
就在我调用“分水岭”时,我看到一个错误,内容如下:
OpenCV Error: Unsupported format or combination of formats (Only 32-bit, 1-channel output images are supported) in cvWatershed, file ..\..\..\..\opencv\modules\imgproc\src\segmentation.cpp, line 151
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: ..\..\..\..\opencv\modules\imgproc\src\segmentation.cpp:151: error: (-210) Only 32-bit, 1-channel output images are supported in function cvWatershed
]
at org.opencv.imgproc.Imgproc.watershed_0(Native Method)
at org.opencv.imgproc.Imgproc.watershed(Imgproc.java:9732)
at segmentation.Segmentation.watershedSegmentation(Segmentation.java:60)
at segmentation.Segmentation.main(Segmentation.java:29)
我明白了,OpenCV 正在寻找一个 32 位 1 通道文件作为输出。
我尝试了所有可能的组合:
CvType.CV_32FC1,
CvType.CV_32F,
CvType.CV_32S,
CvType.CV_32SC1,
CvType.CV_8UC1,
CVType.CV_16UC1
... 他们全部。
错误是有弹性的。它拒绝离开。
请帮忙。
提前致谢。