Using contours in computer vision and robotics using OpenCv

January 28, 2017

Written with love by

 

Read time : 4 minutes , level : Intermediate 

 

We continue our blog series on computer vision. In case you are new to computer vision refer our earlier blog here

 

Have a look at the picture of the 'cat' on your left. It looks awesome and is great. We know that because we are humans. The problem comes when you want your robot to view this image through a file and tell us that it can see a cat !! There is where machine learning, computer vision comes in. The most basic way to decide a cat mathematically and logically is to train the robot on some basic shapes cats mostly have and filter out the outer boundary of his body as below. 

 

This partly may solve the problem as we can provide some values of the area of the head, ears and body and tail and tell us this is a cat. (very basic approach)

 

Curves as the figure on your left play an important aspect in image processing and computer vision. Robots needs to process the images that they see in order to either logically deduce via an algorithm or needs to be trained on image data sets. Needless of the approaches curves remain the most basic aspect that we need to use. 

 

 

Contours can be explained simply as a curve joining all the continuous points (along the boundary), having same color or intensity. The contours are a useful tool for shape analysis and object detection and recognition.

 

Curves are different from an edge although related. Contours are always closed continuous lines whereas an edge is a local maxima or minima single dimension. No wonder we say in English as falling off an edge and not falling off a contour. 

 

So now that we know why contours are used and what is a contour, lets try to understand how we use and find contours. Contours in OpenCv work with binary image - which in simple words means you need a black object over a white background or vice versa. It can also work with an image with just two colors hence the name binary.  But what if we have more colours like the cat above ? We would need to pre process the image before our robot can actually understand them. 

 

Common steps are  - Conversion to binary image using steps as edge detection and to reduce high frequency noise to make our contour detection process more accurate.

 

Finally and fortunately OpenCv provides these options readily in three ways to apply contours and they are : 

 

findContours()  --- Finds contours in a binary image. 

 

void cv::findContours(InputOutputArray image,

OutputArrayOfArrays contours,

OutputArray hierarchy,

int mode,

int method,

Point offset = Point() 

)

 

 

contourArea()  --- Calculates a contour area

 

double cv::contourArea(InputArray contour,

bool oriented = false 

)

 

drawContours()  --- Draws contours outlines or filled contours.The function draws contour outlines in the image if thickness≥0 or fills the area bounded by the contours if thickness<0.

 

void cv::drawContours(InputOutputArray image,

InputArrayOfArrays contours,

int contourIdx,

const Scalar & color,

int thickness = 1,

int lineType = LINE_8,

InputArray hierarchy = noArray(),

int maxLevel = INT_MAX,

Point offset = Point() 

)

 

These three functions are most basic ones we shall be using along with a code example in our next blog on contours. Robots as Sid2 have used contours to detect a RED ball - so start using contours as well. Like share and comment.

Function source : http://docs.opencv.org/3.2.0/

Read next  OpenCV2 Contour sample code - findContours() and drawContours()

 

 

 

 

 

 

Share on Facebook
Share on Twitter
Please reload

Please reload