For this case you need an adaptive threshold. ADAPTIVE_THRESH_GAUSSIAN_C
should give the best results. But you should perform experiments with the blocksize. I think your value 11 is too small. The larger the blocksize, the smoother your T(x,y) threshold will be, and the less noisy the output.
for block_size in range(15, 40, 6):
print(f'Attempt {block_size=}')
binarized_image = cv2.adaptiveThreshold(image, 255,
cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, block_size, 2)
save_my_image(binarized_image,f'myimage{block_size}.png')
You can also experiment with the last parameter, C=2
. This is added to the threshold (or subtracted?) so it represents the binary cut-off. Using larger C
will reduce the noise, but it may also remove details from the script.
After you find the best block_size
, then run another experiment to find the best C
value.