Adaptive threshold of blurry image

前端 未结 3 655
旧巷少年郎
旧巷少年郎 2020-12-13 01:23

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

相关标签:
3条回答
  • 2020-12-13 01:34

    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

    0 讨论(0)
  • 2020-12-13 01:38

    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.

    0 讨论(0)
  • 2020-12-13 01:43

    Always apply gaussian for better results.

    cvAdaptiveThreshold(original_image, thresh_image, 255,
                CV_ADAPTIVE_THRESH_GAUSSIAN_C, CV_THRESH_BINARY, 11, 2);
    
    0 讨论(0)
提交回复
热议问题