You are on page 1of 20

Lections on Image analysis, OpenCV

6. Contours

http://howto.nicubunu.ro/gears/gears_16.png

www.uralvision.blogspot.com perevalovds@gmail.com
USU / IMM Fall 2010
The concept of contour
The concept of contour
Contour of the object - A line that represents the edge forms object.

If there is a partition of the image into regions corresponding to different objects, their
external contours can be specified uniquely.

To indicate Interior contours need 3d model of the object. In this case, the contours are the
projection of the image lines of bending 3d object.

http://cvpr.uni-muenster.de/research/rack/index.html
The concept of contour
If given the image that should be allocated contours
but No no partition on the field, no 3d-models
then there ambiguity determine what the circuit

associated with
-scale (How small objects of interest)
-texture (Whether the contours of the elements of texture)
-semantics (Sometimes the line in the image are simply painted on site, and do not reflect
the bending of its shape)

As to whether the object of a bush and seek only its outline,


or objects considered as individual leaves?
The concept of contour
Therefore, machine vision is often considered the problem of not finding the contours of
objects, and the search contours on image.

Contour in the image - this is the line along which a jump in brightness or color.

Contours on image moreThan the contours of objects in the image. Therefore, after solving
the problem of finding contours in an image made them an additional analysis to identify
the contours of objects of interest.
Why do I need to find the contour
1. Recognition

Contour of an object is usually well characterized by its shape.


Therefore, the contour can often determine the type of object
that we observe.

2. Measurements

It is possible to accurately estimate the size of the object of


their rotation and the location - it is important for industrial
automation, robotics, interactive systems.
Search the contour points
Sobel filter

To underline the points of contours is most often used filter Sobel.

void Sobel(Const Mat & src //Input image


Mat & dst //Output image size and channels - as in src
int ddepth //Depth of the result, for example, CV_32F
int xorder //Order of the derivative of x
int yorder //Order of the derivative of y
int ksize= 3, //window size: 1, 3, 5, 7
double scale= 1, double delta= 0, //zoom and shift the result
int borderType= BORDER_DEFAULT) //Work with the boundary
Filter performs a Gaussian smoothing and taking the partial derivatives of 1, 2, 3 order, or
mixed (xorder on x, yorder on y).
(Gaussian smoothing can get a result that is resistant to noise in the image.)
Sobel filter
Example
Mat image = imread ("lodka.jpg"); // Load image from disk
imshow ("image", image); // Show image
Mat imageDX, imageDY;
Sobel (image, imageDX, CV_32F, 1, 0, 3, 1.0 / 255.0); // derivative x
Sobel (image, imageDY, CV_32F, 0, 1, 3, 1.0 / 255.0); // derivative of y

Please note that only show positive values. Negative and zero values - are shown in black.
Sobel filter
Example, continued

If you transform images into gray


and take the sqrt (imageDX ^ 2 + imageDY ^ 2) - we underline the contour points.
Applying to them the threshold processing, we obtain a set of contour points.

Mat grayX, grayY;


cvtColor (imageDX, grayX, CV_RGB2GRAY);
cvtColor (imageDY, grayY, CV_RGB2GRAY);
pow (grayX, 2, grayX);
pow (grayY, 2, grayY);

Mat contImg = grayX + grayY;


sqrt (contImg, contImg);
contImg.convertTo (temp, CV_8UC3, 255.0);

Mat binary;
threshold (contImg, binary, 0.7, 1.0, CV_THRESH_BINARY);
Sobel filter
Example, continued
Other filters underscore the contour
points
1. Scharr (),
2. Laplacian (),
3. difference of two Gaussians (see the lecture about smoothing).

They all operate on the principle similar to the method of Sobel.

All the methods considered this shortcoming: they work at the level of individual pixels,
and so get the contours are not continuous smooth lines.

Solution: use the Canny algorithm.


Canny edge detector

Is a multi-step algorithm, which include emphasis on the


contour points Sobel filter, and further vectoring units.

Method uses two thresholds.

The idea - to trace the contour of moving along the points with
a maximum value of "loop".

The output of the algorithm gives a binary image with the found
pixels corresponding to the contours.
Canny edge detector
void Canny(Const Mat & image //Input image, 1-channel, 8-bit
Mat & edges, //Output image
double threshold1, //Thresholds, the highest - for contour seed
double threshold2, //Smallest - for contour joins
int apertureSize= 3, //window for the Sobel filter
bool L2gradient=false //whether to use the Euclidean length of vector
// as Sobel, sqrt (dx * dx + dy * dy),
// or simply | dx | + | dy |
);
Canny edge detector
Example
Mat imageGray, edges;
cvtColor (image, imageGray, CV_RGB2GRAY);
Canny (imageGray, edges, 230, 150);

Original image Sobel + Threshold Canny edge detector


Contours vectorization
Trace Contour

Trace Contour - Is to build on a set of broken pixels, ie


construction contour’s vector representation.

Vector representation is convenient in terms of further


processing and analysis of the circuit:

- Smoothing and decimation.


- Modification (rotation, extension).
- Matching, analysis and recognition of objects by their
contours.
Tracing the contours
void findContours(Const Mat & image //Input image
//1-channel, 8-bit
//interpreted as a binary (0 and 0)
vector <vector <Point> > &contours //Found the contours
int mode //The search loops
int method //Method for approximating the contours
Point offset=Point() //shift all rezultruyuschih contours
)

Values mode:
CV_RETR_EXTERNAL - Only the outer contours
CV_RETR_LIST - List all units
Used another form of features, see the documentation:
CV_RETR_CCOMP - 2-level hierarchy - the outer boundaries and holes
CV_RETR_TREE - is a tree of nested loops,

Values method:
CV_CHAIN_APPROX_NONE- Without approximation
CV_CHAIN_APPROX_SIMPLE - vybrasyavaet horizontal and vertical points within the segments
CV_CHAIN_APPROX_TC89_L1, CV_CHAIN_APPROX_TC89_KCOS - approximation methodTeh-Chin
Tracing the contours
Example
vector <vector <Point>> contours;
findContours (edges, contours, CV_RETR_LIST, CV_CHAIN_APPROX_TC89_L1);

Mat draw = image.clone ();


drawContours (draw, contours, -1, Scalar (255, 0, 0));
imshow ("find cont", draw);
Contour analysis

To determine which object belongs to the contour, using the geometric


characteristics
- The length of the contour
- Area be fenced loop
- The distribution of curvature along the contour

Also, the statistical moments and Fourier transform algorithms for direct
comparison of the contours.

Problems

- If the object is essentially three-dimensional, then its outer contour can vary
during the rotation of the object.
- If the object is blocked by another object, it adds complexity to the recognition
algorithm.