Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adaptive threshold of blurry image

I have a fairly blurry 432x432 image of a Sudoku puzzle that doesn't adaptively threshold well (take the mean over a block size of 5x5 pixels, then subtract 2):

enter image description here

As you can see, the digits are slightly distorted, there are a lot of breakages in them, and a few 5s have fused into 6s and 6s into 8s. Also, there's a ton of noise. To fix the noise, I have to make the image even blurrier using a Gaussian blur. However, even a fairly large Gaussian kernel and adaptive threshold blockSize (21x21, subtract 2) fails to remove all the breakages and fuses the digits together even more:

enter image description here

I've also tried dilating the image after thresholding, which has a similar effect to increasing the blockSize; and sharpening the image, which doesn't do much one way or the other. What else should I try?

like image 813
1'' Avatar asked Nov 15 '12 03:11

1''


People also ask

What is adaptive thresholding in image processing?

This method allows us to handle cases where there may be dramatic ranges of pixel intensities and the optimal value of T may change for different parts of the image. In adaptive thresholding, sometimes called local thresholding, our goal is to statistically examine the pixel intensity values in the neighborhood of a given pixel, p.

What is adaptive threshold in OpenCV?

OpenCV - Adaptive Threshold. In simple thresholding, the threshold value is global, i.e., it is same for all the pixels in the image. Adaptive thresholding is the method where the threshold value is calculated for smaller regions and therefore, there will be different threshold values for different regions.

How to threshold a gray-level image with a localized threshold?

A method of thresholding a gray-level image with a locally determined threshold for each individual pixel is described. The method is compared with others based on operators such as the Mexican Hat and the second derivative in the gradient direction. Otsu method is one of the most successful methods for image thresholding.

What is the difference between adaptive thresholding and constant thresholding?

So, a constant threshold value may not be good in all the conditions where an image has different lighting conditions in different areas. In adaptive thresholding, the threshold value is calculated for smaller regions and, therefore, there will be different threshold values for different regions.


2 Answers

A pretty good solution is to use morphological closing to make the brightness uniform and then use a regular (non-adaptive) Otsu threshold:

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

Result:

enter image description here

like image 185
1'' Avatar answered Oct 11 '22 12:10

1''


Take a look at Smoothing Images OpenCV tutorial. Except GaussianBlur there are also medianBlur and bilateralFilter which you can also use to reduce noise. I've got this image from your source image (top right):

result image

Update: And the following image I got after removing small contours:

enter image description here

Update: also you can sharpen image (for example, using Laplacian). Look at this discussion.

like image 45
ArtemStorozhuk Avatar answered Oct 11 '22 13:10

ArtemStorozhuk