opencv lections: 6. contours

20
Lections on Image analysis, OpenCV 6. Contours USU / IMM Fall 2010 www.uralvision.blogspot.com [email protected] http://howto.nicubunu.ro/gears/gears_16.png

Upload: denis-perevalov

Post on 04-Apr-2015

7.091 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: OpenCV Lections: 6. Contours

Lections on Image analysis, OpenCV

6. Contours

USU / IMM Fall 2010www.uralvision.blogspot.com [email protected]

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

Page 2: OpenCV Lections: 6. Contours

The concept of contour

Page 3: OpenCV Lections: 6. Contours

The concept of contourContour 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

Page 4: OpenCV Lections: 6. Contours

The concept of contourIf given the image that should be allocated contoursbut No no partition on the field, no 3d-modelsthen 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?

Page 5: OpenCV Lections: 6. Contours

The concept of contourTherefore, 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.

Page 6: OpenCV Lections: 6. Contours

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.

Page 7: OpenCV Lections: 6. Contours

Search the contour points

Page 8: OpenCV Lections: 6. Contours

Sobel filter

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

void Sobel(Const Mat & src //Input imageMat & dst //Output image size and channels - as in srcint ddepth //Depth of the result, for example, CV_32Fint xorder //Order of the derivative of xint yorder //Order of the derivative of yint ksize= 3, //window size: 1, 3, 5, 7double scale= 1, double delta= 0, //zoom and shift the resultint 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.)

Page 9: OpenCV Lections: 6. Contours

Sobel filterExampleMat image = imread ("lodka.jpg"); // Load image from disk imshow ("image", image); // Show imageMat imageDX, imageDY;Sobel (image, imageDX, CV_32F, 1, 0, 3, 1.0 / 255.0); // derivative xSobel (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.

Page 10: OpenCV Lections: 6. Contours

Sobel filterExample, 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);

Page 11: OpenCV Lections: 6. Contours

Sobel filterExample, continued

Page 12: OpenCV Lections: 6. Contours

Other filters underscore the contour points1. 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.

Page 13: OpenCV Lections: 6. Contours

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.

Page 14: OpenCV Lections: 6. Contours

Canny edge detector

void Canny(Const Mat & image //Input image, 1-channel, 8-bitMat & edges, //Output imagedouble threshold1, //Thresholds, the highest - for contour seed double threshold2, //Smallest - for contour joinsint apertureSize= 3, //window for the Sobel filterbool L2gradient=false //whether to use the Euclidean length of vector

// as Sobel, sqrt (dx * dx + dy * dy),// or simply | dx | + | dy |

);

Page 15: OpenCV Lections: 6. Contours

Canny edge detectorExampleMat imageGray, edges;cvtColor (image, imageGray, CV_RGB2GRAY);Canny (imageGray, edges, 230, 150);

Original image Sobel + Threshold Canny edge detector

Page 16: OpenCV Lections: 6. Contours

Contours vectorization

Page 17: OpenCV Lections: 6. Contours

Trace Contour

Trace Contour - Is to build on a set of broken pixels, ieconstruction 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.

Page 18: OpenCV Lections: 6. 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 contoursint mode //The search loopsint method //Method for approximating the contoursPoint offset=Point() //shift all rezultruyuschih contours

)

Values mode:CV_RETR_EXTERNAL - Only the outer contoursCV_RETR_LIST - List all unitsUsed another form of features, see the documentation:CV_RETR_CCOMP - 2-level hierarchy - the outer boundaries and holesCV_RETR_TREE - is a tree of nested loops,

Values method:CV_CHAIN_APPROX_NONE- Without approximationCV_CHAIN_APPROX_SIMPLE - vybrasyavaet horizontal and vertical points within the segments

CV_CHAIN_APPROX_TC89_L1, CV_CHAIN_APPROX_TC89_KCOS - approximation methodTeh-Chin

Page 19: OpenCV Lections: 6. Contours

Tracing the contours

Examplevector <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);

Page 20: OpenCV Lections: 6. Contours

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.