简体   繁体   中英

How do you lightness thresh hold with HSL on OpenCV?

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

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:

在此处输入图片说明

Result:

在此处输入图片说明

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.

COLOR_BGR2HLS -> channels: H, L, S

Lchannel = imgHLS[:,:,1], #You will get S instead of L

Lchannel = imgHLS[:,1,:]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM