OpenCV4Android - Otsu's Method: Error [closed]

asked 2015-03-17 07:48:33 -0600

Mytheral gravatar image

updated 2015-03-17 08:26:07 -0600

What I'm trying to do is this.

Imgproc.threshold(image, image, 0, 255, Imgproc.THRESH_BINARY+Imgproc.THRESH_OTSU);

I have been looking around and I see various recommendations on what to put in for the threshold and max value. I can do a standard Binary Threshold so I'm assuming it's something specific with Otsu's method that I've misunderstood.

Error

OpenCV Error: Assertion failed (src.type() == CV_8UC1) in double cv::threshold(cv::InputArray, cv::OutputArray, double, double, int), file /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/imgproc/src/thresh.cpp, line 719
imgproc::threshold_10() caught cv::Exception: /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/imgproc/src/thresh.cpp:719: error: (-215) src.type() == CV_8UC1 in function double cv::threshold(cv::InputArray, cv::OutputArray, double, double, int)

EDIT: So I checked my image type and it's coming back with type 24. I attempt to convert it to 8UC1 and I still get the same error.

New Code.

if (image.type() == CvType.CV_8UC1)
        {
            Imgproc.threshold(image, image, 0, 255, Imgproc.THRESH_BINARY+Imgproc.THRESH_OTSU);

            return image;
        }
        else
        {
            Log.v(TAG, "Cannot Threshold: Wrong Image Type: " + image.type() + " Converting to CV_8UC1");

            Mat convertedTo8UC1 = new Mat(); 

            image.convertTo(convertedTo8UC1, CvType.CV_8UC1);

            Imgproc.threshold(convertedTo8UC1, convertedTo8UC1, 0, 255, Imgproc.THRESH_BINARY+Imgproc.THRESH_OTSU);

            return convertedTo8UC1;
        }
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by berak
close date 2015-03-18 02:49:05.459488

Comments

2

So, have you checked if src.type() == CV_8UC1 (i.e. one channel unsigned char)?

Guanta gravatar imageGuanta ( 2015-03-17 07:55:18 -0600 )edit

Good catch. Oddly even when I'm attempting to convert it to 8UC1 it is converting it to 8UC4

Mytheral gravatar imageMytheral ( 2015-03-17 08:39:17 -0600 )edit
2

Mat convertedTo8UC1 = new Mat(); image.convertTo(convertedTo8UC1, CvType.CV_8UC1); <-- maybe this doesn't work as expected if you have a multi-channel image. Have you tried cvtColor() to convert to gray-scale?

Guanta gravatar imageGuanta ( 2015-03-17 08:44:53 -0600 )edit

Sweet that did the trick.

Mytheral gravatar imageMytheral ( 2015-03-17 09:20:59 -0600 )edit