Open In App
Related Articles

Detecting objects of similar color in Python using OpenCV

Improve Article
Save Article
Like Article

OpenCV is a library of programming functions mainly aimed at real-time computer vision.

In this article, we will see how to get the objects of the same color in an image. We can select a color by slide bar which is created by the cv2 command cv2.createTrackbar.

Libraries needed:



First of all, we need to read the image which is in our local folder using cv2.imread( ). For filtering a specific color we need to convert image into HSV format which is hue, saturation, and value and mask the image using cv2.inRange( ) by providing lower and upper bounds of RGB values we wanted to filter which gives us a black and white image where the images with the color of our interests are in white and remaining are in black. we can get back the images with the specified color which we gave it by trackbar by doing cv2 bitwise_and operation.



# import required library
import cv2
import numpy as np
import matplotlib.pyplot as plt
# create a video object
# for capture the frames.
# for Webcamera we pass 0 
# as an argument 
cap = cv2.VideoCapture(0)
# define a empty function 
def nothing(x):
# set window name
# Creates a trackbar and attaches 
# it to the specified window 
# with nothing function
cv2.createTrackbar("LH", "Tracking",
                   0, 255, nothing)
cv2.createTrackbar("LS", "Tracking"
                   0, 255, nothing)
cv2.createTrackbar("LV", "Tracking"
                   0, 255, nothing)
cv2.createTrackbar("HH", "Tracking"
                   0, 255, nothing)
cv2.createTrackbar("HS", "Tracking"
                   0, 255, nothing)
cv2.createTrackbar("HV", "Tracking",
                   0, 255, nothing)
# This drives the program 
# into an infinite loop. 
while True:
    # Captures the live stream frame-by-frame
    _, frame =
    # Converts images from BGR to HSV 
    hsv = cv2.cvtColor(frame, 
    # find LH trackbar position
    l_h = cv2.getTrackbarPos("LH",
    # find LS trackbar position
    l_s = cv2.getTrackbarPos("LS",
    # find LV trackbar position
    l_v = cv2.getTrackbarPos("LV"
    # find HH trackbar position
    h_h = cv2.getTrackbarPos("HH"
    # find HS trackbar position
    h_s = cv2.getTrackbarPos("HS",
    # find HV trackbar position
    h_v = cv2.getTrackbarPos("HV",
    # create a given numpy array
    l_b = np.array([l_h, l_s,
    # create a given numpy array
    u_b = np.array([h_h, h_s,
    # create a mask
    mask = cv2.inRange(hsv, l_b,
    # applying bitwise_and operation
    res = cv2.bitwise_and(frame, 
                          frame, mask = mask)
    # display frame, mask
    # and res window
    cv2.imshow('frame', frame)
    cv2.imshow('mask', mask)
    cv2.imshow('res', res)
    # wait for 1 sec
    k =  cv2.waitKey(1)
    # break out of while loop
    # if k value is 27
    if k == 27:
# release the captured frames 
# Destroys all windows. 


detect objects of same color

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 03 Jan, 2023
Like Article
Save Article
Similar Reads
Complete Tutorials