我正在尝试对图像执行adaptiveThresholding,但它给了我这个错误:
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);
答案 0 :(得分: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()