[OpenCV function] contour extraction; contour drawing; contour area; External rectangle and opencv rectangle
FindContours
Search for a contour in a binary image
IntCvFindContours(CvArr * image, CvMemStorage * storage, CvSeq ** first_contour,
Int header_size = sizeof (CvContour), int mode = CV_RETR_LIST,
Int method = CV_CHAIN_APPROX_SIMPLE, CvPoint offset = cvPoint (0, 0 ));
- Image Input 8-bit, single-channel image. Non-zero elements are treated as 1, and the value of 0 pixels is kept as 0-so that the image is considered as a binary value. To obtain such a binary image from a gray image, you can use cvThreshold, cvAdaptiveThreshold, or cvkan. This function changes the input image content.
- Storage to get the outline of the storage container
- First_contour output parameter: pointer containing the first output profile
- Header_size if method = CV_CHAIN_CODE, the size of the sequence header> = sizeof (CvChain); otherwise,> = sizeof (CvContour ).
- Mode Extraction mode.
CV_RETR_EXTERNAL-extract only the contour of the outermost layer
CV_RETR_LIST-extract all outlines and place them in the list
CV_RETR_CCOMP-extract all outlines and organize them into two hierarchy layers. The top layer is the peripheral boundary of the connected domain, and the second layer is the inner boundary of the hole.
CV_RETR_TREE-extract all outlines and Reconstruct All hierarchy of nested outlines
- Method approximation method (for all nodes, excluding CV_RETR_RUNS using internal approximation ).
Output contour of CV_CHAIN_CODE-Freeman chaincodes. Other Methods output polygon (Fixed Point sequence ).
CV_CHAIN_APPROX_NONE-translate (convert) all vertices from chaincodes to point Sequences
CV_CHAIN_APPROX_SIMPLE-horizontal, vertical, and diagonal division of compression, that is, the function only retains the pixel points at the end;
CV_CHAIN_APPROX_TC89_L1,
CV_CHAIN_APPROX_TC89_KCOS-use the Teh-Chin link approximation algorithm. CV_LINK_RUNS-uses a completely different contour extraction algorithm by concatenating 1-level fragments. Only the CV_RETR_LIST Extraction mode can be applied in this method.
- Offset the offset of each contour point. offset is useful when the contour is extracted from the Image ROI, because the contour can be analyzed from the context of the entire image.
The cvFindContours function extracts a contour from a binary image and returns the number of extracted contours. The content of the pointer first_contour is filled in by the function. It contains the pointer to the first outermost profile. if the pointer is NULL, no contour is detected (like all black ones ). Other outlines can be accessed from the links of first_contour using h_next and v_next. The sample in cvDrawContours shows how to use the profile to detect the connected domain. Contour can also be used for Shape Analysis and object recognition-see squares sample in the CVPR2001 tutorial. This tutorial can be found on the SourceForge website.
DrawContours
Draw external and internal outlines in the image. Void
CvDrawContours( CvArr *img, CvSeq* contour, CvScalar external_color, CvScalar hole_color, int max_level, int thickness=1, int line_type=8, CvPoint offset=cvPoint(0,0) );
- Img is used to draw a contour image. Like other plot functions, the boundary image is cut by the region of interest (ROI.
- The contour Pointer Points to the first profile. The color of the outer contour of external_color.
- The color of the Interior contour of hole_color.
- Max_level: the maximum level for creating an outline. If the level is 0, draw a separate profile. If it is 1, draw the outline and the outline at the same level after it. If the value is 2, all outlines. If the level is 2, draw all the same level outlines and all the lower level outlines. If the value is negative, the function does not draw a child profile of the same level, but is drawn in ascending order until the level is abs (max_level)-1.
- Width of the line used when the thickness draws the contour. If the value is negative (e.g. = CV_FILLED), draw the inner contour.
- The type of the line_type line. See cvLine.
- Offset moves the coordinates of each contour point according to the given offset. this parameter is used when the contour is extracted from some areas of interest (ROI) and you need to consider the ROI offset in the operation.
When thickness> = 0, the cvDrawContours function draws an outline in the image, or when thickness <0, fills the area restricted by the contour.
# Include "cv. h "# include" highgui. h "int main (int argc, char ** argv) {IplImage * src; // The first command line parameter determines the image file name. If (argc = 2 & (src = cvLoadImage (argv [1], 0 ))! = 0) {IplImage * dst = cvCreateImage (cvGetSize (src), 8, 3); CvMemStorage * storage = cvCreateMemStorage (0); CvSeq * contour = 0; cvThreshold (src, src, 1,255, CV_THRESH_BINARY); cvNamedWindow ("Source", 1); cvShowImage ("Source", src); cvFindContours (src, storage, & contour, sizeof (CvContour ), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE); cvZero (dst); for (; contour! = 0; contour = contour-> h_next) {CvScalar color = CV_RGB (rand () & 255, rand () & 255, rand () & 255 ); /* replace the profile shape indicated by CV_FILLED with 1 */cvDrawContours (dst, contour, color, color,-1, CV_FILLED, 8);} cvNamedWindow ("Components ", 1); cvShowImage ("Components", dst); cvWaitKey (0 );}}
Replace CV_FILLED with 1 in the sample to indicate the shape.
Administrative Area
double cvContourArea( const CvArr* contour, CvSliceslice=CV_WHOLE_SEQ );
- Contour: contour (sequence or array of vertices ).
- Slice: the start and end points of the contour part of the interest area. The area of the entire contour is calculated by default.
The cv1_area function is used to calculate the area of the entire or partial contour. When calculating a part of the contour, the arc of the contour and the string connecting the points at both ends
The total area is calculated, as shown in:
NOTE:
The position of the contour affects the symbol of the area, so the function range may be a negative value. You can use the C Runtime function fabs ()
Obtain the absolute value of the area.
E.g. double area = fabs (cv1_area (contour ));
BoundingRect
Calculates the up-right rectangular boundary of a point set.
CvRectCvBoundingRect(CvArr * points, int update = 0 );
- Points
Two-dimensional point set, point sequence or vector (CvMat)
- Update
Update the ID. Below are some possible combinations of contour types and identifiers:
Update = 0, contour ~ CvContour *: the rectangle boundary is not calculated, but is obtained directly from the rect field of the contour header.
Update = 1, contour ~ CvContour *: calculates the rectangle boundary and writes the result to the header in the rect domain of the contour header.
Update = 0, contour ~ CvSeq * or CvMat *: calculates and returns the boundary rectangle.
Update = 1, contour ~ CvSeq * or CvMat *: RUN error (runtime error is raised)
The cvBoundingRect function returns the up-right rectangular boundary of the Two-dimensional point set.