HSV Range for sunny/shaded areas

I have the following problem: when detecting a while line under various lighting conditions, a mask (based on HSV) results in good performance in only one scenario (very bright or very shaded areas).

My code is as follows, I am using HSV. The threshold for upper and lower is a constant value (+x/-x); the same constant value for H, S, V

    hsv = cv2.cvtColor(crop_img, cv2.COLOR_BGR2HSV)

    lower_white = np.array([0,0,154], dtype=np.uint8) 
    upper_white = np.array([170,135,255], dtype=np.uint8) 

    # Threshold the HSV image to get only yellow colors
    mask = cv2.inRange(hsv, lower_white, upper_white)

The results that I get usually work in one case but not the other case. For instance, when the video is in the shaded region it is fine, the white line is the only color seen. Once the video reaches the sunny area, the mask shows everything in white! Could I get help setting the ranges, should I be adjusting H, S, V independently?


your issue is due to the color temperature of the lighting of your scene. you want to perform “white balance”.

1 Like

As a matter of fact, the white balance in the shadows is different from the sunlight.

For this image I would set H=0…180; S=0…50; V=0…110.
You can display your HSV image (it will appear in strange colors) and use the mouse cursor to get some value samples from the image for your inRange function.

Thank you for the reply, tried the suggested values but it sees the lighter browns (in the sun) from the ground as white as the line in the mask.

It might be the way that I’m visualizing it but I don’t get the cursor with the values. But I can screenshot and get the values.

You’re giving me hope that I can use the single mask without white balance, do you think it could work??