Approximates Freeman chain(s) with a polygonal curve.
Parameters: |
|
---|
This is a stand-alone approximation routine. The function cvApproxChains works exactly in the same way as FindContours with the corresponding approximation flag. The function returns pointer to the first resultant contour. Other approximated contours, if any, can be accessed via the v_next or h_next fields of the returned structure.
Approximates polygonal curve(s) with the specified precision.
Parameters: |
|
---|
The function approximates one or more curves and returns the approximation result[s]. In the case of multiple curves, the resultant tree will have the same structure as the input one (1:1 correspondence).
Calculates the contour perimeter or the curve length.
Parameters: |
|
---|
The function calculates the length or curve as the sum of lengths of segments between subsequent points
Calculates the up-right bounding rectangle of a point set.
Parameters: |
|
---|
The function returns the up-right bounding rectangle for a 2d point set. Here is the list of possible combination of the flag values and type of points :
update | points | action |
---|---|---|
0 | CvContour* | the bounding rectangle is not calculated, but it is taken from rect field of the contour header. |
1 | CvContour* | the bounding rectangle is calculated and written to rect field of the contour header. |
0 | CvSeq* or CvMat* | the bounding rectangle is calculated and returned. |
1 | CvSeq* or CvMat* | runtime error is raised. |
Finds the box vertices.
Parameters: |
|
---|
The function calculates the vertices of the input 2d box.
Here is the function code:
void cvBoxPoints( CvBox2D box, CvPoint2D32f pt[4] )
{
float a = (float)cos(box.angle)*0.5f;
float b = (float)sin(box.angle)*0.5f;
pt[0].x = box.center.x - a*box.size.height - b*box.size.width;
pt[0].y = box.center.y + b*box.size.height - a*box.size.width;
pt[1].x = box.center.x + a*box.size.height - b*box.size.width;
pt[1].y = box.center.y - b*box.size.height - a*box.size.width;
pt[2].x = 2*box.center.x - pt[0].x;
pt[2].y = 2*box.center.y - pt[0].y;
pt[3].x = 2*box.center.x - pt[1].x;
pt[3].y = 2*box.center.y - pt[1].y;
}
Calculates a pair-wise geometrical histogram for a contour.
Parameters: |
|
---|
The function calculates a 2D pair-wise geometrical histogram (PGH), described in Iivarinen97 for the contour. The algorithm considers every pair of contour edges. The angle between the edges and the minimum/maximum distances are determined for every pair. To do this each of the edges in turn is taken as the base, while the function loops through all the other edges. When the base edge and any other edge are considered, the minimum and maximum distances from the points on the non-base edge and line of the base edge are selected. The angle between the edges defines the row of the histogram in which all the bins that correspond to the distance between the calculated minimum and maximum distances are incremented (that is, the histogram is transposed relatively to the Iivarninen97 definition). The histogram can be used for contour matching.
Computes the “minimal work” distance between two weighted point configurations.
Parameters: |
|
---|
The function computes the earth mover distance and/or a lower boundary of the distance between the two weighted point configurations. One of the applications described in RubnerSept98 is multi-dimensional histogram comparison for image retrieval. EMD is a a transportation problem that is solved using some modification of a simplex algorithm, thus the complexity is exponential in the worst case, though, on average it is much faster. In the case of a real metric the lower boundary can be calculated even faster (using linear-time algorithm) and it can be used to determine roughly whether the two signatures are far enough so that they cannot relate to the same object.
Tests contour convexity.
Parameters: |
|
---|
The function tests whether the input contour is convex or not. The contour must be simple, without self-intersections.
Structure describing a single contour convexity defect.
typedef struct CvConvexityDefect
{
CvPoint* start; /* point of the contour where the defect begins */
CvPoint* end; /* point of the contour where the defect ends */
CvPoint* depth_point; /* the farthest from the convex hull point within the defect */
float depth; /* distance between the farthest point and the convex hull */
} CvConvexityDefect;
Calculates the area of a whole contour or a contour section.
Parameters: |
|
---|
The function calculates the area of a whole contour or a contour section. In the latter case the total area bounded by the contour arc and the chord connecting the 2 selected points is calculated as shown on the picture below:
Orientation of the contour affects the area sign, thus the function may return a negative result. Use the fabs() function from C runtime to get the absolute value of the area.
Restores a contour from the tree.
Parameters: |
|
---|
The function restores the contour from its binary tree representation. The parameter criteria determines the accuracy and/or the number of tree levels used for reconstruction, so it is possible to build an approximated contour. The function returns the reconstructed contour.
Finds the convex hull of a point set.
Parameters: |
|
---|
The function finds the convex hull of a 2D point set using Sklansky’s algorithm. If storage is memory storage, the function creates a sequence containing the hull points or pointers to them, depending on return_points value and returns the sequence on output. If storage is a CvMat, the function returns NULL.
Example. Building convex hull for a sequence or array of points
#include "cv.h"
#include "highgui.h"
#include <stdlib.h>
#define ARRAY 0 /* switch between array/sequence method by replacing 0<=>1 */
void main( int argc, char** argv )
{
IplImage* img = cvCreateImage( cvSize( 500, 500 ), 8, 3 );
cvNamedWindow( "hull", 1 );
#if !ARRAY
CvMemStorage* storage = cvCreateMemStorage();
#endif
for(;;)
{
int i, count = rand()
CvPoint pt0;
#if !ARRAY
CvSeq* ptseq = cvCreateSeq( CV_SEQ_KIND_GENERIC|CV_32SC2,
sizeof(CvContour),
sizeof(CvPoint),
storage );
CvSeq* hull;
for( i = 0; i < count; i++ )
{
pt0.x = rand()
pt0.y = rand()
cvSeqPush( ptseq, &pt0 );
}
hull = cvConvexHull2( ptseq, 0, CV_CLOCKWISE, 0 );
hullcount = hull->total;
#else
CvPoint* points = (CvPoint*)malloc( count * sizeof(points[0]));
int* hull = (int*)malloc( count * sizeof(hull[0]));
CvMat point_mat = cvMat( 1, count, CV_32SC2, points );
CvMat hull_mat = cvMat( 1, count, CV_32SC1, hull );
for( i = 0; i < count; i++ )
{
pt0.x = rand()
pt0.y = rand()
points[i] = pt0;
}
cvConvexHull2( &point_mat, &hull_mat, CV_CLOCKWISE, 0 );
hullcount = hull_mat.cols;
#endif
cvZero( img );
for( i = 0; i < count; i++ )
{
#if !ARRAY
pt0 = *CV_GET_SEQ_ELEM( CvPoint, ptseq, i );
#else
pt0 = points[i];
#endif
cvCircle( img, pt0, 2, CV_RGB( 255, 0, 0 ), CV_FILLED );
}
#if !ARRAY
pt0 = **CV_GET_SEQ_ELEM( CvPoint*, hull, hullcount - 1 );
#else
pt0 = points[hull[hullcount-1]];
#endif
for( i = 0; i < hullcount; i++ )
{
#if !ARRAY
CvPoint pt = **CV_GET_SEQ_ELEM( CvPoint*, hull, i );
#else
CvPoint pt = points[hull[i]];
#endif
cvLine( img, pt0, pt, CV_RGB( 0, 255, 0 ));
pt0 = pt;
}
cvShowImage( "hull", img );
int key = cvWaitKey(0);
if( key == 27 ) // 'ESC'
break;
#if !ARRAY
cvClearMemStorage( storage );
#else
free( points );
free( hull );
#endif
}
}
Finds the convexity defects of a contour.
Parameters: |
|
---|
The function finds all convexity defects of the input contour and returns a sequence of the CvConvexityDefect structures.
Creates a hierarchical representation of a contour.
Parameters: |
|
---|
The function creates a binary tree representation for the input contour and returns the pointer to its root. If the parameter threshold is less than or equal to 0, the function creates a full binary tree representation. If the threshold is greater than 0, the function creates a representation with the precision threshold : if the vertices with the interceptive area of its base line are less than threshold , the tree should not be built any further. The function returns the created tree.
Finishes the scanning process.
Parameters: |
|
---|
The function finishes the scanning process and returns a pointer to the first contour on the highest level.
Finds the contours in a binary image.
Parameters: |
|
---|
The function retrieves contours from the binary image using the algorithm Suzuki85 . The contours are a useful tool for shape analysis and object detection and recognition.
The function retrieves contours from the binary image and returns the number of retrieved contours. The pointer first_contour is filled by the function. It will contain a pointer to the first outermost contour or NULL if no contours are detected (if the image is completely black). Other contours may be reached from first_contour using the h_next and v_next links. The sample in the DrawContours discussion shows how to use contours for connected component detection. Contours can be also used for shape analysis and object recognition - see squares.c in the OpenCV sample directory.
Note: the source image is modified by this function.
Finds the next contour in the image.
Parameters: |
|
---|
The function locates and retrieves the next contour in the image and returns a pointer to it. The function returns NULL if there are no more contours.
Fits an ellipse around a set of 2D points.
Parameters: |
|
---|
The function calculates the ellipse that fits best (in least-squares sense) around a set of 2D points. The meaning of the returned structure fields is similar to those in Ellipse except that size stores the full lengths of the ellipse axises, not half-lengths.
Fits a line to a 2D or 3D point set.
Parameters: |
|
---|
The function fits a line to a 2D or 3D point set by minimizing where is the distance between the th point and the line and is a distance function, one of:
dist_type=CV_DIST_L2
dist_type=CV_DIST_L1
dist_type=CV_DIST_L12
dist_type=CV_DIST_FAIR
dist_type=CV_DIST_WELSCH
dist_type=CV_DIST_HUBER
Retrieves the central moment from the moment state structure.
Parameters: |
|
---|
The function retrieves the central moment, which in the case of image moments is defined as:
where are the coordinates of the gravity center:
Calculates the seven Hu invariants.
Parameters: |
|
---|
The function calculates the seven Hu invariants, see http://en.wikipedia.org/wiki/Image_moment , that are defined as:
where denote the normalized central moments.
These values are proved to be invariant to the image scale, rotation, and reflection except the seventh one, whose sign is changed by reflection. Of course, this invariance was proved with the assumption of infinite image resolution. In case of a raster images the computed Hu invariants for the original and transformed images will be a bit different.
Retrieves the normalized central moment from the moment state structure.
Parameters: |
|
---|
The function retrieves the normalized central moment:
Retrieves the spatial moment from the moment state structure.
Parameters: |
|
---|
The function retrieves the spatial moment, which in the case of image moments is defined as:
where is the intensity of the pixel .
Compares two contours using their tree representations.
Parameters: |
|
---|
The function calculates the value of the matching measure for two contour trees. The similarity measure is calculated level by level from the binary tree roots. If at a certain level the difference between contours becomes less than threshold , the reconstruction process is interrupted and the current difference is returned.
Compares two shapes.
Parameters: |
|
---|
The function compares two shapes. The 3 implemented methods all use Hu moments (see GetHuMoments ) ( is object1 , is object2 ):
method=CV_CONTOUR_MATCH_I1
method=CV_CONTOUR_MATCH_I2
method=CV_CONTOUR_MATCH_I3
where
and are the Hu moments of and respectively.
Finds the circumscribed rectangle of minimal area for a given 2D point set.
Parameters: |
|
---|
The function finds a circumscribed rectangle of the minimal area for a 2D point set by building a convex hull for the set and applying the rotating calipers technique to the hull.
Picture. Minimal-area bounding rectangle for contour
Finds the circumscribed circle of minimal area for a given 2D point set.
Parameters: |
|
---|
The function finds the minimal circumscribed circle for a 2D point set using an iterative algorithm. It returns nonzero if the resultant circle contains all the input points and zero otherwise (i.e. the algorithm failed).
Calculates all of the moments up to the third order of a polygon or rasterized shape.
Parameters: |
|
---|
The function calculates spatial and central moments up to the third order and writes them to moments . The moments may then be used then to calculate the gravity center of the shape, its area, main axises and various shape characeteristics including 7 Hu invariants.
Point in contour test.
Parameters: |
|
---|
The function determines whether the point is inside a contour, outside, or lies on an edge (or coinsides with a vertex). It returns positive, negative or zero value, correspondingly. When , the return value is +1, -1 and 0, respectively. When , it is a signed distance between the point and the nearest contour edge.
Here is the sample output of the function, where each image pixel is tested against the contour.
Initializes a point sequence header from a point vector.
Parameters: |
|
---|
The function initializes a sequence header to create a “virtual” sequence in which elements reside in the specified matrix. No data is copied. The initialized sequence header may be passed to any function that takes a point sequence on input. No extra elements can be added to the sequence, but some may be removed. The function is a specialized variant of MakeSeqHeaderForArray and uses the latter internally. It returns a pointer to the initialized contour header. Note that the bounding rectangle (field rect of CvContour strucuture) is not initialized by the function. If you need one, use BoundingRect .
Here is a simple usage example.
CvContour header;
CvSeqBlock block;
CvMat* vector = cvCreateMat( 1, 3, CV_32SC2 );
CV_MAT_ELEM( *vector, CvPoint, 0, 0 ) = cvPoint(100,100);
CV_MAT_ELEM( *vector, CvPoint, 0, 1 ) = cvPoint(100,200);
CV_MAT_ELEM( *vector, CvPoint, 0, 2 ) = cvPoint(200,100);
IplImage* img = cvCreateImage( cvSize(300,300), 8, 3 );
cvZero(img);
cvDrawContours( img,
cvPointSeqFromMat(CV_SEQ_KIND_CURVE+CV_SEQ_FLAG_CLOSED,
vector,
&header,
&block),
CV_RGB(255,0,0),
CV_RGB(255,0,0),
0, 3, 8, cvPoint(0,0));
Gets the next chain point.
Parameters: |
|
---|
The function returns the current chain point and updates the reader position.
Initializes the contour scanning process.
Parameters: |
|
---|
The function initializes and returns a pointer to the contour scanner. The scanner is used in FindNextContour to retrieve the rest of the contours.
Initializes the chain reader.
The function initializes a special reader.
Replaces a retrieved contour.
Parameters: |
|
---|
The function replaces the retrieved contour, that was returned from the preceding call of FindNextContour and stored inside the contour scanner state, with the user-specified contour. The contour is inserted into the resulting structure, list, two-level hierarchy, or tree, depending on the retrieval mode. If the parameter new_contour is NULL , the retrieved contour is not included in the resulting structure, nor are any of its children that might be added to this structure later.