Tracking white color using python opencv

Tracking white color using python opencv

Lets take a look at HSV color space:


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:


I wrote this for tracking white color :

import cv2
import numpy as np

cap = cv2.VideoCapture(0)


    _, frame =
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    # define range of white color in HSV
    # change it according to your need !
    lower_white = np.array([0,0,0], dtype=np.uint8)
    upper_white = np.array([0,0,255], dtype=np.uint8)

    # Threshold the HSV image to get only white colors
    mask = cv2.inRange(hsv, lower_white, upper_white)
    # Bitwise-AND mask and original image
    res = cv2.bitwise_and(frame,frame, mask= mask)


    k = cv2.waitKey(5) & 0xFF
    if k == 27:


I tried to track the white screen of my phone and got this :


You can try changing the HSV values
You might also try HSL color space as Legat said, it would be more accurate

Tracking white color using python opencv

Heres an HSV color thresholder script to determine the lower and upper bounds using sliders



Using this sample image

With these lower/upper thresholds

lower_white = np.array([0,0,168])
upper_white = np.array([172,111,255])

We get isolated white pixels (left) and the binary mask (right)

Heres the script, remember to change the input image path

import cv2
import sys
import numpy as np

def nothing(x):

# Load in image
image = cv2.imread(1.jpg)

# Create a window

# create trackbars for color change
cv2.createTrackbar(HMin,image,0,179,nothing) # Hue is from 0-179 for Opencv

# Set default value for MAX HSV trackbars.
cv2.setTrackbarPos(HMax, image, 179)
cv2.setTrackbarPos(SMax, image, 255)
cv2.setTrackbarPos(VMax, image, 255)

# Initialize to check if HSV min/max value changes
hMin = sMin = vMin = hMax = sMax = vMax = 0
phMin = psMin = pvMin = phMax = psMax = pvMax = 0

output = image
wait_time = 33


    # get current positions of all trackbars
    hMin = cv2.getTrackbarPos(HMin,image)
    sMin = cv2.getTrackbarPos(SMin,image)
    vMin = cv2.getTrackbarPos(VMin,image)

    hMax = cv2.getTrackbarPos(HMax,image)
    sMax = cv2.getTrackbarPos(SMax,image)
    vMax = cv2.getTrackbarPos(VMax,image)

    # Set minimum and max HSV values to display
    lower = np.array([hMin, sMin, vMin])
    upper = np.array([hMax, sMax, vMax])

    # Create HSV Image and threshold into a range.
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(hsv, lower, upper)
    output = cv2.bitwise_and(image,image, mask= mask)

    # Print if there is a change in HSV value
    if( (phMin != hMin) | (psMin != sMin) | (pvMin != vMin) | (phMax != hMax) | (psMax != sMax) | (pvMax != vMax) ):
        print((hMin = %d , sMin = %d, vMin = %d), (hMax = %d , sMax = %d, vMax = %d) % (hMin , sMin , vMin, hMax, sMax , vMax))
        phMin = hMin
        psMin = sMin
        pvMin = vMin
        phMax = hMax
        psMax = sMax
        pvMax = vMax

    # Display output image

    # Wait longer to prevent freeze for videos.
    if cv2.waitKey(wait_time) & 0xFF == ord(q):


Leave a Reply

Your email address will not be published. Required fields are marked *