我正在开发一个使用 OpenCV-2.4.8 android sdk
的 Android 应用程序。
以下示例代码尝试使用 OpenCV
库的 Watershed 分割算法
检测对象。
//bmp is the source bitmap that I am reading from a Drawable resource.
Mat mBackgroundMat = new Mat(new Size(bmp.getWidth(), bmp.getHeight()), CvType.CV_8UC3);
Utils.bitmapToMat(bmp, mBackgroundMat);
//Initialize the Mat Objects
Mat backgroundMat = new Mat();
Mat greyMatImg = new Mat();
Mat thresholdImg = new Mat();
Mat markerImg = new Mat();
//Erode and dillate
Imgproc.erode(mBackgroundMat, backgroundMat, new Mat(), new Point(-1, -1), 12);
Imgproc.dilate(backgroundMat, backgroundMat, new Mat(), new Point(-1, -1), 12);
// Imgproc.cvtColor(backgroundMat, backgroundMat, Imgproc.COLOR_RGB2BGR);
Imgproc.cvtColor(backgroundMat, greyMatImg, Imgproc.COLOR_BGR2GRAY);
Imgproc.threshold(greyMatImg, thresholdImg, 0, 255, Imgproc.THRESH_BINARY_INV);
Imgproc.distanceTransform(thresholdImg, markerImg, Imgproc.CV_DIST_L2, Imgproc.CV_DIST_MASK_5);
Imgproc.cvtColor(thresholdImg, thresholdImg, Imgproc.COLOR_GRAY2BGR, 3);
Mat tempMat = new Mat(markerImg.rows(), markerImg.cols(), CvType.CV_32SC1);
Imgproc.cvtColor(markerImg, tempMat, CvType.CV_32SC1, 0);
Imgproc.watershed(thresholdImg, tempMat);
//Release unused mats.
tempMat.release();
backgroundMat.release();
greyMatImg.release();
markerImg.release();
//Output thresholdImg
我收到以下错误:
cv::error()(2566): OpenCV Error: Assertion failed (scn == 3 || scn == 4) in void cv::cvtColor(InputArray, OutputArray, int, int), file /home/reports/ci/slave_desktop/50-SDK/opencv/modules/imgproc/src/color.cpp, line 3648
请帮我弄清楚我在这里做错了什么。
最佳答案
Imgproc.cvtColor(markerImg, tempMat, CvType.CV_32SC1, 0);
我认为是这一行 - opencv 提示 markerImg 不是 3 或 4 channel 图像。看起来这次调用中的 Mats 具有相同数量的 channel ,因此您可能只需要在数据类型之间进行转换; distanceTransform 的结果似乎给出了 CV_32FC1 所以你可以尝试像这样转换:
markerImg.convertTo(tempMat, CvType.CV_32SC1);
关于java - OpenCV 错误 : Assertion failed (scn == 3 || scn == 4) while using Watershed in OpenCV android sdk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21315537/