Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tracking white color using python opencv

I would like to track white color using webcam and python opencv. I already have the code to track blue color.

_, frame = cap.read() hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)  # define range of blue color in HSV lower_blue = np.array([110,100,100]) upper_blue = np.array([130,255,255])  #How to define this range for white color   # Threshold the HSV image to get only blue colors mask = cv2.inRange(hsv, lower_blue, upper_blue) # Bitwise-AND mask and original image res = cv2.bitwise_and(frame,frame, mask= mask)  cv2.imshow('frame',frame) cv2.imshow('mask',mask) cv2.imshow('res',res) 

what values should I give as lower bound and upper bound to track white color!!?? I tried changing values and I got other colors but no luck with the white color!!!

is that HSV values or BGR values specified as lower and upper bounds???

PS : I must get the last result as a binary image for further processing!!

Please help me !!!

like image 243
user3429616 Avatar asked Mar 23 '14 07:03

user3429616


People also ask

What is the HSV value of white?

4) White has an HSV value of 0-255, 0-255, 255.

How can I tell if a photo is white in color?

All Answers (8) A short answer to your question you need to transform your image into Hue, Saturation, Intensity (HSI) representation. Then you need to look for the Saturation channel (S) to detect the variation in White color.

Can OpenCV detect colors?

We can use the inRange() function of OpenCV to create a mask of color, or in other words, we can detect a color using the range of that color. The colors are stored in an RGB triplet value format inside a color image. To create its mask, we have to use the RGB triplet value of that color's light and dark version.


1 Answers

Let's take a look at HSV color space:

enter image description here

You need white, which is close to the center and rather high. Start with

sensitivity = 15 lower_white = np.array([0,0,255-sensitivity]) upper_white = np.array([255,sensitivity,255]) 

and then adjust the threshold to your needs.

You might also consider using HSL color space, which stands for Hue, Saturation, Lightness. Then you would only have to look at lightness for detecting white and recognizing other colors would stay easy. Both HSV and HSL keep similar colors close. Also HSL would probably prove more accurate for detecting white - here is why:

enter image description here

like image 166
Legat Avatar answered Oct 10 '22 21:10

Legat