一个很好的解决方案是使用形态学闭合使亮度均匀,然后使用常规的(非自适应)Otsu阈值:
// Divide the image by its morphologically closed counterpart
Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(19,19));
Mat closed = new Mat();
Imgproc.morphologyEx(image, closed, Imgproc.MORPH_CLOSE, kernel);
image.convertTo(image, CvType.CV_32F); // divide requires floating-point
Core.divide(image, closed, image, 1, CvType.CV_32F);
Core.normalize(image, image, 0, 255, Core.NORM_MINMAX);
image.convertTo(image, CvType.CV_8UC1); // convert back to unsigned int
// Threshold each block (3x3 grid) of the image separately to
// correct for minor differences in contrast across the image.
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
Mat block = image.rowRange(144*i, 144*(i+1)).colRange(144*j, 144*(j+1));
Imgproc.threshold(block, block, -1, 255, Imgproc.THRESH_BINARY_INV+Imgproc.THRESH_OTSU);
}
}
结果:
0
我有一个Sudoku拼图的相当模糊的432x432图像,不能很好地自适应阈值(取5x5像素块大小的平均值,然后减去2):
如您所见,数字稍微失真,其中有很多破损,有几个5s融合成6s,6s融合成8s。另外,还有很多噪音。为了解决噪点,我必须使用高斯模糊使图像更加模糊。但是,即使是相当大的高斯内核和自适应阈值blockSize(21x21,减2)都无法消除所有破损,并且将数字融合得更多:
我还尝试了在阈值化之后对图像进行放大,这与增加blockSize的效果类似;并锐化图像 ,这两种方法都不会有太大作用。我还应该尝试什么?