OpenCV  3.0.0-rc1
Open Source Computer Vision
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Changing Colorspaces

Goal

Changing Color-space

There are more than 150 color-space conversion methods available in OpenCV. But we will look into only two which are most widely used ones, BGR \(\leftrightarrow\) Gray and BGR \(\leftrightarrow\) HSV.

For color conversion, we use the function cv2.cvtColor(input_image, flag) where flag determines the type of conversion.

For BGR \(\rightarrow\) Gray conversion we use the flags cv2.COLOR_BGR2GRAY. Similarly for BGR \(\rightarrow\) HSV, we use the flag cv2.COLOR_BGR2HSV. To get other flags, just run following commands in your Python terminal :

1 >>> import cv2
2 >>> flags = [i for i in dir(cv2) if i.startswith('COLOR_')]
3 >>> print flags
Note
For HSV, Hue range is [0,179], Saturation range is [0,255] and Value range is [0,255]. Different softwares use different scales. So if you are comparing OpenCV values with them, you need to normalize these ranges.

Object Tracking

Now we know how to convert BGR image to HSV, we can use this to extract a colored object. In HSV, it is more easier to represent a color than RGB color-space. In our application, we will try to extract a blue colored object. So here is the method:

Below is the code which are commented in detail :

1 import cv2
2 import numpy as np
3 
4 cap = cv2.VideoCapture(0)
5 
6 while(1):
7 
8  # Take each frame
9  _, frame = cap.read()
10 
11  # Convert BGR to HSV
12  hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
13 
14  # define range of blue color in HSV
15  lower_blue = np.array([110,50,50])
16  upper_blue = np.array([130,255,255])
17 
18  # Threshold the HSV image to get only blue colors
19  mask = cv2.inRange(hsv, lower_green, upper_green)
20 
21  # Bitwise-AND mask and original image
22  res = cv2.bitwise_and(frame,frame, mask= mask)
23 
24  cv2.imshow('frame',frame)
25  cv2.imshow('mask',mask)
26  cv2.imshow('res',res)
27  k = cv2.waitKey(5) & 0xFF
28  if k == 27:
29  break
30 
31 cv2.destroyAllWindows()

Below image shows tracking of the blue object:

frame.jpg
image
Note
There are some noises in the image. We will see how to remove them in later chapters.
This is the simplest method in object tracking. Once you learn functions of contours, you can do plenty of things like find centroid of this object and use it to track the object, draw diagrams just by moving your hand in front of camera and many other funny stuffs.

How to find HSV values to track?

This is a common question found in stackoverflow.com. It is very simple and you can use the same function, cv2.cvtColor(). Instead of passing an image, you just pass the BGR values you want. For example, to find the HSV value of Green, try following commands in Python terminal:

1 >>> green = np.uint8([[[0,255,0 ]]])
2 >>> hsv_green = cv2.cvtColor(green,cv2.COLOR_BGR2HSV)
3 >>> print hsv_green
4 [[[ 60 255 255]]]

Now you take [H-10, 100,100] and [H+10, 255, 255] as lower bound and upper bound respectively. Apart from this method, you can use any image editing tools like GIMP or any online converters to find these values, but don't forget to adjust the HSV ranges.

Additional Resources

Exercises

  1. Try to find a way to extract more than one colored objects, for eg, extract red, blue, green objects simultaneously.