Structural Analysis Reference



Contour Processing Functions


ApproxChains

Approximates Freeman chain(s) with polygonal curve

CvSeq* cvApproxChains( CvSeq* srcSeq, CvMemStorage* storage,
                       CvChainApproxMethod method=CV_CHAIN_APPROX_SIMPLE,
                       double parameter=0, int minimalPerimeter=0, int recursive=0 );

srcSeq
Pointer to the chain that can refer to other chains.
storage
Storage location for the resulting polylines.
method
Approximation method (see the description of the function cvFindContours).
parameter
Method parameter (not used now).
minimalPerimeter
Approximates only those contours whose perimeters are not less than minimalPerimeter. Other chains are removed from the resulting structure.
recursive
If not 0, the function approximates all chains that access can be obtained to from srcSeq by h_next or v_next links. If 0, the single chain is approximated.

This is a stand-alone approximation routine. The function cvApproxChains works exactly in the same way as cvFindContours with the corresponding approximation flag. The function returns pointer to the first resultant contour. Other approximated contours, if any, can be accessed via v_next or h_next fields of the returned structure.


StartReadChainPoints

Initializes chain reader

void cvStartReadChainPoints( CvChain* chain, CvChainPtReader* reader );

chain Pointer to chain. reader Chain reader state.

The function cvStartReadChainPoints initializes a special reader (see Dynamic Data Structures for more information on sets and sequences).


ReadChainPoint

Gets next chain point

CvPoint cvReadChainPoint( CvChainPtReader* reader );

reader
Chain reader state.

The function cvReadChainPoint returns the current chain point and updates the reader position.


ApproxPoly

Approximates polygonal curve(s) with desired precision

CvSeq* cvApproxPoly( const void* srcSeq, int headerSize, CvMemStorage* storage,
                     int method, double parameter,
                     int parameter2=0 );

srcSeq
Sequence of array of points.
headerSize
Header size of approximated curve[s].
storage
Container for approximated contours. If it is NULL, the input sequences' storage is used.
method
Approximation method; only CV_POLY_APPROX_DP is supported, that corresponds to Douglas-Peucker algorithm.
parameter
Method-specific parameter; in case of CV_POLY_APPROX_DP it is a desired approximation accuracy.
parameter2
If case if srcSeq is sequence it means whether the single sequence should be approximated or all sequences on the same level or below srcSeq (see cvFindContours for description of hierarchical contour structures). And if srcSeq is array (CvMat*) of points, the parameter specifies whether the curve is closed (parameter2!=0) or not (parameter2=0).

The function cvApproxPoly approximates one or more curves and returns the approximation result[s]. In case of multiple curves approximation the resultant tree will have the same structure as the input one (1:1 correspondence).


BoundingRect

Calculates up-right bounding rectangle of point set

CvRect cvBoundingRect( CvArr* contour, int update );

contour
Sequence or array of points.
update
The update flag. Here is list of possible combination of the flag values and type of contour:

The function cvBoundingRect returns the up-right bounding rectangle for 2d point set.


ContourArea

Calculates area of the whole contour or contour section

double cvContourArea( const CvArr* contour, CvSlice slice=CV_WHOLE_SEQ );

contour
Contour (sequence or array of vertices).
slice
Starting and ending points of the contour section of interest, by default area of the whole contour is calculated.

The function cvContourArea calculates area of the whole contour or 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:

NOTE: Orientation of the contour affects the area sign, thus the function may return negative result. Use fabs() function from C runtime to get the absolute value of area.


ArcLength

Calculates contour perimeter or curve length

double cvArcLength( const void* curve, CvSlice slice=CV_WHOLE_SEQ, int isClosed=-1 );

curve
Sequence or array of the curve points.
slice
Starting and ending points of the curve, by default the whole curve length is calculated.
isClosed
Indicates whether the curve is closed or not. There are 3 cases:

The function cvArcLength calculates length or curve as sum of lengths of segments between subsequent points


MatchShapes

Compares two shapes

double cvMatchShapes( const void* A, const void* B,
                      int method, double parameter=0 );

A
First contour or grayscale image
B
Second contour or grayscale image
method
Comparison method, one of CV_CONTOUR_MATCH_I1, CV_CONTOURS_MATCH_I2 or CV_CONTOURS_MATCH_I3.
parameter
Method-specific parameter (is not used now).

The function cvMatchShapes compares two shapes. The 3 implemented methods all use Hu moments (see cvGetHuMoments):

method=CV_CONTOUR_MATCH_I1:
I1(A,B)=sumi=1..7abs(1/mAi - 1/mBi)

method=CV_CONTOUR_MATCH_I2:
I2(A,B)=sumi=1..7abs(mAi - mBi)

method=CV_CONTOUR_MATCH_I3:
I3(A,B)=sumi=1..7abs(mAi - mBi)/abs(mAi)

where
mAi=sign(hAi)•log(hAi),
mBi=sign(hBi)•log(hBi),
hAi, hBi - Hu moments of A and B, respectively.

CreateContourTree

Creates hierarchical representation of contour

CvContourTree* cvCreateContourTree( cont CvSeq* contour, CvMemStorage* storage, double threshold );

contour
Input contour.
storage
Container for output tree.
threshold
Approximation accuracy.

The function cvCreateContourTree creates 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 full binary tree representation. If the threshold is greater than 0, the function creates 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.


ContourFromContourTree

Restores contour from tree

CvSeq* cvContourFromContourTree( const CvContourTree* tree, CvMemStorage* storage,
                                 CvTermCriteria criteria );

tree
Contour tree.
storage
Container for the reconstructed contour.
criteria
Criteria, where to stop reconstruction.

The function cvContourFromContourTree 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 approximated contour. The function returns reconstructed contour.


MatchContourTrees

Compares two contours using their tree representations

double cvMatchContourTrees( const CvContourTree* tree1, const CvContourTree* tree2,
                            CvTreeMatchMethod method, double threshold );

tree1
First contour tree.
tree2
Second contour tree.
method
Similarity measure, only CV_CONTOUR_TREES_MATCH_I1 is supported.
threshold
Similarity threshold.

The function cvMatchContourTrees 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 the certain level difference between contours becomes less than threshold, the reconstruction process is interrupted and the current difference is returned.


Geometry Functions


MaxRect

Finds bounding rectangle for two given rectangles

CvRect cvMaxRect( const CvRect* rect1, const CvRect* rect2 );

rect1
First rectangle
rect2
Second rectangle

The function cvMaxRect finds minimum area rectangle that contains both input rectangles inside:


CvBox2D

Rotated 2D box

typedef struct CvBox2D
{
    CvPoint2D32f center;  /* center of the box */
    CvSize2D32f  size;    /* box width and length */
    float angle;          /* angle between the horizontal axis
                             and the first side (i.e. length) in radians */
}
CvBox2D;

BoxPoints

Finds box vertices

void cvBoxPoints( CvBox2D box, CvPoint2D32f pt[4] );

box
Box
pt
Array of vertices

The function cvBoxPoints calculates 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;
}

FitEllipse

Fits ellipse to set of 2D points

CvBox2D cvFitEllipse2( const CvArr* points );

points
Sequence or array of points.

The function cvFitEllipse calculates ellipse that fits best (in least-squares sense) to a set of 2D points. The meaning of the returned structure fields is similar to those in cvEllipse except that size stores the full lengths of the ellipse axises, not half-lengths


FitLine

Fits line to 2D or 3D point set

void  cvFitLine( const CvArr* points, CvDisType disType, double C,
                 double reps, double aeps, float* line );

points
Sequence or array of 2D or 3D points with 32-bit integer or floating-point coordinates.
disType
The distance used for fitting (see the discussion).
C
Numerical parameter for some types of distances, if 0 then some optimal value is chosen.
reps, aeps
Sufficient accuracy for radius (distance between the coordinate origin and the line) and angle, respectively, 0.01 would be a good defaults for both. is used.
line
The output line parameters. In case of 2d fitting it is array of 4 floats (vx, vy, x0, y0) where (vx, vy) is a normalized vector collinear to the line and (x0, y0) is some point on the line. In case of 3D fitting it is array of 6 floats (vx, vy, vz, x0, y0, z0) where (vx, vy, vz) is a normalized vector collinear to the line and (x0, y0, z0) is some point on the line.

The function cvFitLine fits line to 2D or 3D point set by minimizing sumiρ(ri), where ri is distance between i-th point and the line and ρ(r) is a distance function, one of:

disType=CV_DIST_L2 (L2):
ρ(r)=r2/2 (the simplest and the fastest least-squares method)

disType=CV_DIST_L1 (L1):
ρ(r)=r

disType=CV_DIST_L12 (L1-L2):
ρ(r)=2•[sqrt(1+r2/2) - 1]

disType=CV_DIST_FAIR (Fair):
ρ(r)=C2•[r/C - log(1 + r/C)],  C=1.3998

disType=CV_DIST_WELSCH (Welsch):
ρ(r)=C2/2•[1 - exp(-(r/C)2)],  C=2.9846

disType=CV_DIST_HUBER (Huber):
ρ(r)= r2/2,   if r < C
      C•(r-C/2),   otherwise;   C=1.345


ConvexHull2

Finds convex hull of points set

CvSeq* cvConvexHull2( const void* points, void* hullStorage=0,
                      int orientation=CV_CLOCKWISE, int returnPoints=0 );

points
Sequence or array of 2D points with 32-bit integer or floating-point coordinates.
hullStorage
The destination array (CvMat*) or memory storage (CvMemStorage*) that will store the convex hull. If it is array, it should be 1d and have the same number of elements as the input array/sequence. On output the header is modified so to truncate the array downto the hull size.
orientation
Desired orientation of convex hull: CV_CLOCKWISE or CV_COUNTER_CLOCKWISE.
returnPoints
If non-zero, the points themselves will be stored in the hull instead of indices if hullStorage is array, or pointers if hullStorage is memory storage.

The function cvConvexHull2 finds convex hull of 2D point set using Sklansky's algorithm. If hullStorage is memory storage, the function creates a sequence containing the hull points or pointers