OpenCV4Android - Otsu's Method: Error [closed]
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;
}
So, have you checked if src.type() == CV_8UC1 (i.e. one channel unsigned char)?
Good catch. Oddly even when I'm attempting to convert it to 8UC1 it is converting it to 8UC4
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?Sweet that did the trick.