Convert RGB to black OR white

后端 未结 8 1153
我在风中等你
我在风中等你 2020-11-30 23:20

How would I take an RGB image in Python and convert it to black OR white? Not grayscale, I want each pixel to be either fully black (0, 0, 0) or fully white (255, 255, 255).

8条回答
  •  日久生厌
    2020-11-30 23:29

    If you don't want to use cv methods for the segmentation and understand what you are doing, treat the RGB image as matrix.

    image = mpimg.imread('image_example.png') # your image
    R,G,B = image[:,:,0], image[:,:,1], image[:,:,2] # the 3 RGB channels
    thresh = [100, 200, 50] # example of triple threshold
    
    # First, create an array of 0's as default value
    binary_output = np.zeros_like(R)
    # then screen all pixels and change the array based on RGB threshold.
    binary_output[(R < thresh[0]) & (G > thresh[1]) & (B < thresh[2])] = 255
    

    The result is an array of 0's and 255's based on a triple condition.

提交回复
热议问题