3
votes

There is a project that im working on which required the color white detection, after some research i decided to use covert RGB image to HSL image and thresh hold the lightness to get the color white, im working with openCV so wonder if there is a way to do it. enter image description here

1
in opencv is HLS and normally images are loaded as BGR in OpenCV. take a look to cvtColor.api55
Yes i was able to covert the BGR to HLS but what i was stuck at how to filt out the pixel that have lightness attribute too low, so that the image will be left with the shade of color whiteMinh Cht
use split to separate the channels (or using numpy slicing), then use threshold in the L one (second channel) and you get a mask with it, then apply the mask to your original image. If you post an example image I can try to give a complete answerapi55

1 Answers

7
votes

You can do it with 4 easy steps:

Convert HLS

img = cv2.imread("HLS.png")
imgHLS = cv2.cvtColor(img, cv2.COLOR_BGR2HLS)

Get the L channel

Lchannel = imgHLS[:,:,1]

Create the mask

#change 250 to lower numbers to include more values as "white"
mask = cv2.inRange(Lchannel, 250, 255)

Apply Mask to original image

res = cv2.bitwise_and(img,img, mask= mask)

This also depends on what is white for you, and you may change the values :) I used inRange in the L channel but you can save one step and do

mask = cv2.inRange(imgHLS, np.array([0,250,0]), np.array([255,255,255]))

instead of the lines:

Lchannel = imgHLS[:,:,1]
mask = cv2.inRange(Lchannel, 250, 255)

It is shorter, but I did it the other way first to make it more explicit and to show what I was doing.

Image:

enter image description here

Result:

enter image description here

The result looks almost as the mask (almost binary), but depending on your lowerbound (I chose 250) you may get even some almost white colors.