adaptivethreshold给出一个错误(opencv,java)



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

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

对于自适应阈值源应该是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()

相关内容

  • 没有找到相关文章