You are on page 1of 3

Aquib Javed Khan

Real Time Multiple color tracking

In this project this project basically the fundamentals of computer vision is used to track three
differnet colours Red, Blue and Yellow. When we run the code a window will open an using the
webcam (We can use other camera also) and when there's any color from Red, Blue and Yellow or
all three the same time time we'll see a rectangular boxes of Red color for tracking of Red, blue
rectangular box for Blue color and Green for Yellow color bound the respective colors object and
there will be text on the top of the object showing the name of color. This will be helpful in
recognizing color's and in robotics. This type of system used in driverless cars also to detect traffic
and vehicle back light and take desicion to stop, start or continuing driving. This also have many
application in industry to pick and place different colored object by the robotics arm.

Software used:
Opencv_3.0
python_2.7
Numpy python module

Opencv is a library used for computer vision, In this project I am using opencv with python.

Flow chart diagram:

The input from the camera is BGR so we have to convert it into HSV(Hue Saturation Value).
OpenCV usually captures images and videos in 8-bit, unsigned integer, BGR format. Captured
images can be considered as 3 matrices of BLUE, GREEN and RED with integer values ranges
from 0 to 255.
HSV color space is consists of 3 matrices, 'hue', 'saturation' and 'value'. In OpenCV, value range for
'hue', 'saturation' and 'value' are respectively 0-179, 0-255 and 0-255. 'Hue' represents the color,
'saturation' represents the amount to which that respective color is mixed with white and 'value'
represents the amount to which that respective color is mixed with black.
RGB color space describes colors in terms of the amount of red, green, and blue present. HSV color
space describes colors in terms of the Hue, Saturation, and Value. In situations where color
description plays an integral role, the HSV color model is often preferred over the RGB model.

Next we've to find the HSV of RED, BLUE and YELLOW.


Here we have to take care that none of the values overlap with the other color.
converting frame(img i.e BGR) to HSV (hue-saturation-value)
hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)

#definig the range of red color


red_lower=np.array([136,87,111],np.uint8)
red_upper=np.array([180,255,255],np.uint8)

#defining the Range of Blue color


blue_lower=np.array([99,115,150],np.uint8)
blue_upper=np.array([110,255,255],np.uint8)

#defining the Range of yellow color


yellow_lower=np.array([22,60,200],np.uint8)
yellow_upper=np.array([60,255,255],np.uint8)

After this we will do some Morphological transformation on the colors we're going to track:
Basically this done to remove small noises in the image, It is normally performed on binary images.
It needs two inputs, one is our original image, second one is called structuring element or kernel
which decides the nature of operation. Here I've used Dilation.There's a kernal matrix I've used 5x5
If any any 1 pixel element belongs to it, So it will Increases the region. It is also useful in joining
broken parts of an object.

kernal= np.ones((5 ,5), "uint8") #kernal is created of 5x5


red=cv2.dilate(red, kernal)
res=cv2.bitwise_and(img, img, mask = red)
blue=cv2.dilate(blue,kernal)
res1=cv2.bitwise_and(img, img, mask = blue)
yellow=cv2.dilate(yellow,kernal)
res2=cv2.bitwise_and(img, img, mask = yellow)
Now the next part is just to contour the following colors region we've previously
described.Contours can be described as a curve joining all the continuous points (along the
boundary), having same color or intensity.
There are three arguments in cv2.findContours function, first one is source image, second is contour
retrieval mode, third is contour approximation method.

(_,contours,hierarchy)=cv2.findContours(red,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIM
PLE)
for pic, contour in enumerate(contours):
area = cv2.contourArea(contour)
if(area>300): #for removing small noises
x,y,w,h = cv2.boundingRect(contour)
img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
cv2.putText(img,"RED color",(x,y),cv2.FONT_HERSHEY_SIMPLEX, 0.7,(0,0,255))

You might also like