Python-OpenCV for detecting colored object

I always wondered how “Line following robots” worked. How do machines see something? Read news about famous people warning against implications of Artificial intelligence but always wondered how can intelligence be artificial? We, as humans, ever since we are born start collecting data from all around us and form our intelligence on the empirical findings.Nature gave us sensory organs for that.

Machines too got sensory organs. The one that really fascinates me is “Vision”. OpenCV, or Open Computer Vision is such a program library that provides computers the ability to see through a camera and analyse the images.

This is my first program using OpenCV and Python (its easy to use Python for prototyping) which allows the computer to use its camera and filter out only one color from the frames it captures.

import cv2
import numpy as np

def getthresholdedimg(hsv):
threshImg = cv2.inRange(hsv,np.array((cv2.getTrackbarPos('Hue_Low','Trackbars'),cv2.getTrackbarPos('Saturation_Low','Trackbars'),cv2.getTrackbarPos('Value_Low','Trackbars'))),np.array((cv2.getTrackbarPos('Hue_High','Trackbars'),cv2.getTrackbarPos('Saturation_High','Trackbars'),cv2.getTrackbarPos('Value_High','Trackbars'))))
return threshImg

def getTrackValue(value):
return value
c = cv2.VideoCapture(0)
width,height = c.get(3),c.get(4)
print "frame width and height : ", width, height

cv2.namedWindow('Trackbars', cv2.WINDOW_NORMAL)
cv2.createTrackbar('Hue_Low','Trackbars',0,255, getTrackValue)
cv2.createTrackbar('Saturation_Low','Trackbars',0,255, getTrackValue)
cv2.createTrackbar('Value_Low','Trackbars',0,255, getTrackValue)

cv2.createTrackbar('Hue_High','Trackbars',0,255, getTrackValue)
cv2.createTrackbar('Saturation_High','Trackbars',0,255, getTrackValue)
cv2.createTrackbar('Value_High','Trackbars',0,255, getTrackValue)
cv2.createTrackbar('Caliberate','Trackbars',0,1, getTrackValue)

_,f =
f = cv2.flip(f,1)
blur = cv2.medianBlur(f,5)
hsv = cv2.cvtColor(f,cv2.COLOR_BGR2HSV)
thrImg = getthresholdedimg(hsv)
erode = cv2.erode(thrImg,None,iterations = 3)
dilate = cv2.dilate(erode,None,iterations = 10)

contours,hierarchy = cv2.findContours(dilate,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

for cnt in contours:
x,y,w,h = cv2.boundingRect(cnt)
cx,cy = x+w/2, y+h/2


if(cv2.getTrackbarPos('Caliberate','Trackbars') == 1):

if cv2.waitKey(10) & 0xFF == ord('q'):



The code is written in Python 2.7 and OpenCV 2.4.10. It shows one console, and two preview boxes. One shows the image while the other has sliders on it.

OpenCV Dialogs
Open CV Program Windows

The OpenCV library does its job of identifying and spotting the object of specific color in front of camera. The only tricky part I found was to get the exact “Hue Saturation Value” range for a particular color. The code when run, opens a window with sliders on it. Just turn on the calibrate slider and try adjusting the other LOW and HIGH values sliders for HSV value to see the object under observation is the only one visible now (it can be the only white on black background or black on white)

Once calibration is done, switch back to image by turning off the calibrate switch. That’s it !!! the program will start identifying the object and draw a red box outside it.

(I struggled a lot for getting the correct set of HSV values, hope this blog helps someone)

Edit Jan 2017 – The code is available on github

Python-OpenCV for detecting colored object