Find, express, draw, feature, and match the contour (how to use Contour?) Find, Component, Construct, Features & Match)

Source: Internet
Author: User

Http://www.cnblogs.com/xrwang/archive/2010/02/09/HowToUseContour.html

Wang Xianrong

Objective
A contour is a boundary or outline line that makes up any shape. How to match the color and color distribution (histogram contrast and template matching), let's take a look at how to use the contour of the object. Includes the following: Contour lookup, expression, organization, drawing, characteristics, matching.

Find Outlines
The first problem we face is how to find the contours in the image, and OpenCv (EMGUCV) does a lot of work for us, and our task is simply to call out the ready-made functions. The Findcontours method of the Image<tcolor,tdepth> class makes it easy to find outlines, but before looking, we need to convert the color image into a grayscale image and then convert the grayscale image to a two-value image. The code looks like this:

IMAGE&LT;BGR, byte> imagesource = new IMAGE&LT;BGR, byte> (sourceimagefilename); Get Source image
Image<gray, byte> Imagegray = Imagesource.convert<gray, byte> (); Convert source image to grayscale image
int thresholdvalue = Tbthreshold.value; Threshold values for binary values
Image<gray, byte> imagethreshold = Imagegray.thresholdbinary (new Gray (Thresholdvalue), New Gray (255d)); The binary value of grayscale image
Contour<point> contour=imagethreshold.findcontours ();

How the contours are expressed
Use the above code to get the default outline of the image, but how is the outline expressed in the computer? Two ways of expressing outlines are provided in OpenCV (EMGUCV): Sequence of vertices, Freeman chain code.

1. Sequence of vertices
Use multiple vertices (or segments between points) to express the contour. Suppose you want to express a rectangle from (0,0) to (2,2),
(1) If the point is indicated, then the storage may be: (0,0), (1,0), (2,0), (2,1), (2,2), (UP), (0,2), (0,1);
(2) If the contour is expressed by a line segment between points, then the storage may be: (0,0), (2,0), (2,2), (0,2).
The following code can be used to get the points on the contour:

for (int i = 0; i < contour. Total; i++)
Sbcontour.appendformat ("{0},", Contour[i]);

2.Freeman Chain Code
The Freeman chain code requires a starting point and a series of displacements from the starting point. Each displacement has 8 directions, from 0~7 to 8 directions starting from North. Suppose you want to express a rectangle from (0,0) to (2,2) with the Freeman chain Code, the possible representation is: The starting point (0,0), the direction chain 2,2,4,4,6,6,0,0.
EMGUCV support for Freeman chain code is rare, and we need to do a series of work to use the Freeman chain code in. NET:
(1) Get Freeman chain code

find outlines with freeman chain Codes


(2) Traversing the points on the Freeman chain Code

reading points on the Freeman chain Code

It is important to note that the Cvreadchainpoint function never seems to meet the condition of a cyclic termination, that is, Ptrreader is never set to NULL, which is inconsistent with learning OpenCV and reference. We need to use chain.total to assist in terminating the loop, and after reading all the points we can stop.

How to organize between outlines
After finding the contour, how are the different contours organized? Depending on the choice, they may be: (1) list, (2) Double-layer structure, (3) tree structure.
From a vertical point of view, the list is only one layer, the two-tier structure has one or both layers, the tree structure may have one or more layers.
If you want to traverse all the contours, you can use recursion, the code is as follows:

traversing outlines

Drawing of Outlines
Contour drawing is relatively simple, using the method mentioned above to obtain all the points of the contour, and then connect these points into a polygon.
Of course, for contours expressed in vertex sequences, use image<tcolor,tdepth> The draw method or the Cvdrawcontours function makes it easy to draw outlines. I found that if you set the parameter Max_level to 2, you can draw all the outlines.
The code for drawing outlines is as follows:

Drawing Outlines


Features of the Contour
There are many features of contour, which are described below.

1. Polygon Approximation of contours
Polygon approximation of outlines refers to the use of polygons to approximate a contour.
The goal of polygon approximation is to reduce the number of vertices in the contour.
The result of Polygon approximation is still a contour, but this contour is relatively rough.
Contour<point> can be used;. The Approxpoly method or the Cvapproxypoly function for polygonal approximation of the contour, the sample code is as follows:

Contour = Firstcontour.approxpoly (double. Parse (Txtapproxparameter.text), 2, New Memstorage ());

2. Key points of the contour
The key point of the contour is that the contour contains more points of the curve information. The key point is a subset of the contour vertices.
You can use the Cvfinddominantpoints function to get a key on the contour that returns a sequence that contains the key points indexed in the contour vertex. Again: is the index, not the specific point. If you want to get the exact coordinates of the key points, you can use the index to look up the contour.
The following code shows how to get the key points on the contour:

key points of the contour


3. Perimeter and area of contour
The perimeter of the contour can be used contour<point> Perimeter property or Cvarclength function to get.
The area of the contour can be used contour<point> The Area property or the Cvcontourarea function to get.

4. Bounding box for outlines
There are three common bounding boxes: rectangles, circles, and ellipses.
(1) Rectangle: A rectangle called rectangle is provided in the image processing system, but it can only express a special case of the vertical or horizontal side of the edge; OpenCV also has a rectangle called box, which is the same as the mathematical rectangle, as long as the 4 angle is right angle.
If you want to get the rectangle of the contour, you can use CONTOUR<POINT>. The Boundingrectangle property or the Cvboundingrect function.
If you want to get the outline of the box, you can use the Contour<point>. Getminarearect method or CvMinAreaRect2 function.
(2) Round
You can use the Cvminenclosingcircle function if you want to get a circular bounding box for a contour.
(3) Oval
You can use the CVFITELLIPSE2 function if you want to get the ellipse bounding box for the contour.
The following code shows how to get the various bounding boxes for outlines:

bounding box for outlines

5. The moment of the contour
We can use CONTOUR<POINT>. The Getmoments method or the Cvmoments function is convenient to get the moment set of the contour, and then the corresponding method or function obtains the various moments.
Specific moments: Mcvmoments.getspatialmoment method, Cvgetspatialmoment function
Center moment: Mcvmoments.getcentralmoment method, Cvgetcentralmoment function
Normalized Center moment: Mcvmoments.getnormalizedcentralmoment method, Cvgetnormalizedcentralmoment function
Hu moment: Mcvmoments.gethumoment method, Mcvhumoments.hu1~hu7 field, cvgethumoments function
The following code shows how to get the moment of the contour:

the moment of the contour


6. Outline of the Contour tree
The contour tree is used to describe the internal characteristics of a particular contour. Note: The contour tree is one by one corresponding to the contour, and the contour tree is not used to describe hierarchical relationships between multiple outlines.
You can use the function Cvcreatecontourtree to construct the contour tree.

IntPtr ptrTree1 = Cvinvoke.cvcreatecontourtree (contour1. PTR, New Memstorage (). PTR, thresholdofcreate);

7. Convex hull and convex defects of contour
Convex and convex defects of the contour are used to describe the shape of the object. Convex and convex defects are easy to get, but I don't know how they work at the moment.
If you want to determine whether the contour is convex, you can use CONTOUR<POINT> The Convex property and the Cvcheckcontourconvexity function.
If you want to get the convex hull of the contour, you can use Contour<point>. The Getconvexhull method, or CvConvexHull2 function, returns a sequence that contains a vertex.
If you want to get the convex defect of the contour, you can use Contour<point>. Getconvexitydefacts method or Cvconvexitydefects function.
Note: EMGUCV misspelled the defective word, defect is the flaw.
The following code shows how to get the convex hull and convex defects of the contour:

convex hull and convex defects of contour

8. Geometric histogram of the contour
There is less data on the geometry histogram, which I understand.
(1) The contour holds a series of vertices, and the contour is a polygon composed of a series of segments. For seemingly smooth contours (such as circles), only the number of line segments is more, the length of the segment is relatively short. In fact, any curve displayed in the computer is made up of line segments.
(2) There is a certain relationship between each of the two segments, including the angle between them (or their extension lines), the angle range of the two segments is: (0,180).
(3) There is a distance relationship between the points on each of the two segments, including the shortest (small) distance, the farthest (large) distance, and the average distance. Max distance I used a lazy calculation method, I think the outline of the outside rectangle diagonal length as the maximum distance.
(4) The statistical data used for the paired geometric histogram includes the angle and distance.
You can use the function cvcalcpgh to calculate the paired geometric histogram of the contour, the sample code is as follows:

the paired geometric histogram of the contour

Matching of contours
If you want to compare two objects, there are many features to choose from. If you want to judge the gender of a person, according to the length of his or her hair to judge, it is very intuitive, in the rare age of long hair male accuracy is also very high. It can also be judged by the range of the person's urine, if the range is greater than 0.50 meters, it is male. In short, there are many ways to do this.
We've got so many features of the contours above that they can also be used for matching. Typical contour matching methods are: Hu moment matching, contour tree matching, paired geometric histogram matching.
1.Hu moment Matching
The Hu moment of the contour is invariant to changes including scaling, rotation, and mirror mapping. Contour<point>. The Matchshapes method and the Cvmatchshapes function can be conveniently implemented to match 2 contours.
2. Contour Tree Matching
Compare two contours in the form of a tree. The Cvmatchcontourtrees function realizes the comparison of the contour tree.
3. Paired geometric histogram matching
After you get the paired geometric histogram of the contour, you can use the Histogram comparison method to match. If you've forgotten the way the histogram is contrasted with me, you can look at my other article, "calculation, display, processing, contrast, and reverse projection of color histograms" (How to use histogram?). Calculate, Show, Process, Compare and Backproject) ".

Sample code for various contour matches is as follows:

Matching of contours

 

With the above code, you can calculate the values of two contour comparisons, but what do these values mean? In fact, it's not clear to me at the moment that a lot of experimentation is needed.

Thank you for your patience to read this article, I hope you have some help.

Find, express, draw, feature, and match the contour (how to use Contour?) Find, Component, Construct, Features & Match)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.