How do you convert a grayscale OpenCV image to black and white? I see a similar question has already been asked, but I\'m using OpenCV 2.3, and the proposed solution no long
While converting a gray scale image to a binary image, we usually use cv2.threshold()
and set a threshold value manually. Sometimes to get a decent result we opt for Otsu's binarization.
I have a small hack I came across while reading some blog posts.
This is because 33% works for most of the images/data-set.
You can also work out the same approach by replacing median
with the mean
.
Another approach would be to take an x
number of standard deviations (std
) from the mean, either on the positive or negative side; and set a threshold. So it could be one of the following:
th1 = mean - (x * std)
th2 = mean + (x * std)
Note: Before applying threshold it is advisable to enhance the contrast of the gray scale image locally (See CLAHE).