Ask Your Question
0

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in void cv::cvtColor(InputArray, OutputArray, int, int) while using watershed

asked 2014-01-23 12:29:25 -0600

vivek_rk gravatar image

updated 2014-01-23 12:39:07 -0600

berak gravatar image

Hi,

I am working on an Android application that used the OpenCV-2.4.8 android sdk. The following sample code tries to detect an object using the Watershed segmenter algorithm of the OpenCV library.

//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

I am getting the following error: 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

Please help me in figuring out what I am doing wrong here.

edit retag flag offensive close merge delete

Comments

1

from the bitmapToMat docs: @Param mat is a valid output Mat object, it will be reallocated if needed, so Mat may be empty.

so please check, if yout bitmap is really bgr. if it is not, the mat will be reallocated to 1chan , which would explain the error you get.

berak gravatar imageberak ( 2014-01-23 12:48:19 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-02-25 16:37:10 -0600

Rui Marques gravatar image

updated 2014-02-25 16:37:48 -0600

What are you doing here?

Imgproc.cvtColor(markerImg, tempMat, CvType.CV_32SC1, 0);

The third parameter should be something like Imgproc.COLOR_*
The 4th should be the number of channels (shouldn't be zero) but you can omit it if you got the 3rd right.

Check cvtColor documentation here:

http://docs.opencv.org/java/org/opencv/imgproc/Imgproc.html

edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-01-23 12:29:25 -0600

Seen: 5,210 times

Last updated: Feb 25 '14