1

我正在尝试对图像进行自适应阈值处理,但它给了我这个错误:

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

我似乎无法理解为什么,这是我的代码:

         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);
4

1 回答 1

3

对于AdaptiveThreshold源应该是 8 位单通道图像,但您正在加载source2为颜色,

所以,换行

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

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

还有为什么要在adaptiveThreshold之前保存和加载destination图片,直接传给 adaptiveThreshold()

于 2014-04-16T12:30:13.683 回答