Converting an OpenCV Image to Black and White

后端 未结 7 1527
逝去的感伤
逝去的感伤 2020-11-29 17:38

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

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-29 18:06

    Approach 1

    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.

    1. Convert your color (RGB) image to gray scale.
    2. Obtain the median of the gray scale image.
    3. Choose a threshold value either 33% above the median

    Why 33%?

    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.

    Approach 2

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

提交回复
热议问题