繁体   English   中英

OpenCV错误:在OpenCV android sdk中使用分水岭时断言失败(scn == 3 || scn == 4)

[英]OpenCV Error: Assertion failed (scn == 3 || scn == 4) while using Watershed in OpenCV android sdk

我正在使用OpenCV-2.4.8 android sdk的Android应用程序上工作。 以下示例代码尝试使用OpenCV库的Watershed segmenter algorithm检测对象。

//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通道图像。 看起来此调用中的Mats具有相同数量的通道,因此也许您只需要在数据类型之间进行转换即可; distanceTransform的结果似乎给出了CV_32FC1,因此您可以尝试像这样进行转换:

    markerImg.convertTo(tempMat, CvType.CV_32SC1);

我有一个类似的错误...

就我而言,该错误是因为Mat没有数据。

尝试执行此验证:

`if(!src.data) //where src is the Mat with the image
{
Log.d("Error","Adios");
    exit(0);
}`

如果src中没有数据,则不会将其转换为Bitmap

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM