Showing posts with label opencv. Show all posts
Showing posts with label opencv. Show all posts

Tuesday, 1 July 2014

Identifying Coloured Objects Using OpenCv

Hey everyone , in this blog we will be learning how to create a mask for a particular colour  and extract the particular coloured object from an image.

Algorithm used:

1) Read the BGR image
2) Convert the image into HSV color space
3) Create a color mask by setting the upper and lower thresholds from the hsv image
4) Filter the original BGR  image using the color mask

Sample Code :

import cv2
import numpy as np

img = cv2.imread("tomato.jpg",-1)
cv2.imshow("orginal",img)

blur=cv2.GaussianBlur(img,(5,5),0)
cv2.imshow("blurred image",blur)
hsv=cv2.cvtColor(blur,cv2.COLOR_BGR2HSV)

lower=np.array([0,100,100])
upper=np.array([20,255,255])
mask=cv2.inRange(hsv,lower,upper)
cv2.imshow("mask",mask)
out=cv2.bitwise_and(img,img,mask=mask)
cv2.imshow("masked image",out)

cv2.waitKey(0)

Explanation:

The sample code above separates all the red coloured objects from your image.

import cv2
import numpy as np

img = cv2.imread("tomato.jpg",-1)
cv2.imshow("orginal",img)

The above lines are for reading the original image with the name tomato.jpg stored in the working directory.In case the image is stored in some other directory you have to give the complete path.

blur=cv2.GaussianBlur(img,(5,5),0)
cv2.imshow("blurred image",blur)
hsv=cv2.cvtColor(blur,cv2.COLOR_BGR2HSV)

The first two lines removes the noise in the image using a gaussian filter and displays the blurred image. Next line converts the image into into hsv color space.For more details of the conversion visit my blog.

lower=np.array([0,100,100])
upper=np.array([20,255,255])
mask=cv2.inRange(hsv,lower,upper)
cv2.imshow("mask",mask)

The lines #1,#2 sets the lower and upper threshold for the mask.The hue value is selected to be in between 0-20 since we want to get the red objects.If we want to select some other colour you can do it by just changing the hue value here.The value for saturation and value are set to be 100-255 to include all tones and shades of red.The line #3 creates a mask from the hsv image.Line #4 displays the mask.

out=cv2.bitwise_and(img,img,mask=mask)
cv2.imshow("masked image",out)
cv2.waitKey(0)

The line 1 use a bitwise 'and' operation to filter the original BGR image using the mask and return a masked image in which only the red objects will be shown.

Original Image
Blurred Image
Masked Image
Mask

 Hoping the blog was helpful.Take care & Continue Hacking ;-)


Colour Spaces and their conversions in opencv

In this blog we will be dealing with various color spaces and their conversions using opencv functions.

Introduction to Color spaces

According to definitions given in wikipedia the colorspace of an image is defined as: 
"Color space, also known as the color model (or color system), is an abstract mathematical model which simply describes the range of colors as tuples of numbers, typically as 3 or 4 values or color components "

For example in a RGB image every color is defined as a combination of three basic colors: red,green and blue.The color of a point changes as the value of red,green and blue component changes.(0,0,0) represents perfect black (255,255,255) represents white.One thing to be taken into account always is that in opencv the images are represented in the BGR format.So (0,0,255) represents perfect red and not blue as in a ordinary RGB image.

Another important color space used in image processing projects is HSV color space.Here the color value of each point is represented by three parameters Hue,Saturation and value.You can find more details here.Hue is the color component.In opencv it ranges from 0-180 unlike the normal image handling softwares such as Gimp or photoshop were the hsv value of hue varies from 0-360.So dont forget to divide the value by to before applying to opencv.

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.
The syntax of the function used for color space conversion is :
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 :
>>> import cv2
>>> flags = [i for i in dir(cv2) if i.startswith('COLOR_')]
>>> print flags

Example Code for BGR-> HSV conversion:

import cv2

img=cv2.imread('image.jpg')  # A BGR image is taken
hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV) #image is converted to hsv color space

Example Code for BGR-> GRAY conversion:

import cv2

img=cv2.imread('image.jpg')    # A BGR image is taken
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  #image is converted to gray sacle image

Similarly by changing the flags the image can be converted to various color spaces.Different color spaces have their own advantages and disadvantages.Each of them is selected according to their suitability to the application.