I am trying to do adaptiveThresholding on an image but it gives me this error:

OpenCV Error: Assertion failed (src.type() == CV_8UC1) in adaptiveThreshold

I can't seem to understand why, here is my code:

         Mat source = Highgui.imread("camera.jpg", 
         Highgui.CV_LOAD_IMAGE_COLOR);

         Mat destination = new Mat(source.rows(),source.cols(),source.type());

         Imgproc.cvtColor(source, destination, Imgproc.COLOR_RGB2GRAY);

         Highgui.imwrite("grayscale.jpg", destination);

         Mat source2 = Highgui.imread("grayscale.jpg", 
         Highgui.CV_LOAD_IMAGE_COLOR);

         Mat destination2 = new Mat(source.rows(),source.cols(),source.type());

         Imgproc.adaptiveThreshold(source2, destination2, 255,
         Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY_INV, 15, 4);
有帮助吗?

解决方案

For adaptiveThreshold source should be 8-bit single-channel image, but you are loading source2 as colour,

So, Change the line

 Mat source2 = Highgui.imread("grayscale.jpg", Highgui.CV_LOAD_IMAGE_COLOR);

to

 Mat source2 = Highgui.imread("grayscale.jpg", Highgui.CV_LOAD_IMAGE_GRAYSCALE);

Also why to save and load destination image before adaptiveThreshold, pass it directly to adaptiveThreshold()

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top