Add adaptive threshold algorithm using mean method by miralshah365 · Pull Request #341 · boostorg/gil

@miralshah365
I've been playing with the algorithm against this little funny sample of 16x16 pixel greyscale gradient background with two 'barely visible rectangles.
You can download it here 16x16rectangles.

image

OpenCV

The 16x16 sample above passed through the adaptive mean threshold by OpenCV:

import cv2
import numpy as np
import matplotlib.pyplot as plt
path = 'D:\\workshop\\opencv\\'
path_original = path + "rectangles.png"

img = cv2.imread(path_original, 0)

ret, th1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
th2 = cv2.adaptiveThreshold(
    img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 0)
th3 = cv2.adaptiveThreshold(
    img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 0)

titles = ['Input', 'Global Thresholding (t=127)',
          'Adaptive Mean', 'Adaptive Gaussian']
images = [img, th1, th2, th3]
for i in range(4):
    plt.subplot(2, 2, i+1), plt.imshow(images[i], 'gray')
    plt.title(titles[i])
    plt.xticks([]), plt.yticks([])
plt.show()

identifies both rectangles:

image

GIL

Using threshold_adaptive:

std::string image_dir = "d:\\workshop\\opencv\\";
{
    gray8_image_t img;
    read_image(image_dir + "rectangles.png", img, png_tag{});
    gray8_image_t img_out(img.dimensions());

    boost::gil::threshold_adaptive(const_view(img), view(img_out), 11);
    write_view(image_dir + "out-threshold-adaptive.png", view(img_out), png_tag{});
}

identifies only one, the upper rectangle:

image

Here is dump of the convoluted view generated with this

write_view("d:\\workshop\\opencv\\out-threshold-adaptive-convoluted-view.png", temp_view, png_tag{});

image

Unfortunately, I have no way to generate equivalent for OpenCV.

Conclusion

There still may be some fine tuning of the algorithm necessary.
At least, it may be worth to test it a bit more, also with your impl. of convolution.