imgproc_structural_analysis_and_shape_descriptors.rst 38.6 KB

Structural Analysis and Shape Descriptors

ApproxChains

id=0.891722904536 Comments from the Wiki

This is a stand-alone approximation routine. The function cvApproxChains works exactly in the same way as :ref:`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.

ApproxPoly

id=0.511872225874 Comments from the Wiki

ApproxPoly(src_seq, storage, method, parameter=0, parameter2=0) -> sequence

Approximates polygonal curve(s) with the specified precision.

param src_seq: Sequence of an array of points
type src_seq: :class:`CvArr` or :class:`CvSeq`
param storage: Container for the approximated contours. If it is NULL, the input sequences' storage is used
type storage: :class:`CvMemStorage`
param method: Approximation method; only CV_POLY_APPROX_DP is supported, that corresponds to the Douglas-Peucker algorithm
type method: int
param parameter: Method-specific parameter; in the case of CV_POLY_APPROX_DP it is a desired approximation accuracy
type parameter: float
param parameter2: If case if src_seq is a sequence, the parameter determines whether the single sequence should be approximated or all sequences on the same level or below src_seq (see :ref:`FindContours` for description of hierarchical contour structures). If src_seq is an array CvMat* of points, the parameter specifies whether the curve is closed ( parameter2 !=0) or not ( parameter2 =0)
type parameter2: int

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).

ArcLength

id=0.00865407229522 Comments from the Wiki

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

BoundingRect

id=0.0890624819294 Comments from the Wiki

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.

BoxPoints

id=0.833111863214 Comments from the Wiki

The function calculates the vertices of the input 2d box.

CalcPGH

id=0.492952101193 Comments from the Wiki

The function calculates a 2D pair-wise geometrical histogram (PGH), described in :ref:`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 :ref:`Iivarninen97` definition). The histogram can be used for contour matching.

CalcEMD2

id=0.390300478738 Comments from the Wiki

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 :ref:`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.

CheckContourConvexity

id=0.472970769213 Comments from the Wiki

The function tests whether the input contour is convex or not. The contour must be simple, without self-intersections.

CvConvexityDefect

id=0.484949869638 Comments from the Wiki

A single contour convexity defect, represented by a tuple (start, end, depthpoint, depth) .

../pics/defects.png

ContourArea

id=0.958766756024 Comments from the Wiki

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:

../pics/contoursecarea.png

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.

ContourFromContourTree

id=0.162786680111 Comments from the Wiki

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.

ConvexHull2

id=0.277941655963 Comments from the Wiki

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.

ConvexityDefects

id=0.819226332619 Comments from the Wiki

The function finds all convexity defects of the input contour and returns a sequence of the CvConvexityDefect structures.

CreateContourTree

id=0.249344062216 Comments from the Wiki

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.

FindContours

id=0.577796431347 Comments from the Wiki

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 :ref:`DrawContours` discussion shows how to use contours for connected component detection. Contours can be also used for shape analysis and object recognition - see squares.py in the OpenCV sample directory.

Note: the source image is modified by this function.

FitEllipse2

id=0.461296656973 Comments from the Wiki

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 :ref:`Ellipse` except that size stores the full lengths of the ellipse axises, not half-lengths.

FitLine

id=0.681153539505 Comments from the Wiki

The function fits a line to a 2D or 3D point set by minimizing \sum_i \rho(r_i) where r_i is the distance between the i th point and the line and \rho(r) is a distance function, one of:

  • dist_type=CV_DIST_L2

    \rho (r) = r^2/2  \quad \text{(the simplest and the fastest least-squares method)}
    
  • dist_type=CV_DIST_L1

    \rho (r) = r
    
  • dist_type=CV_DIST_L12

    \rho (r) = 2  \cdot ( \sqrt{1 + \frac{r^2}{2}} - 1)
    
  • dist_type=CV_DIST_FAIR

    \rho \left (r \right ) = C^2  \cdot \left (  \frac{r}{C} -  \log{\left(1 + \frac{r}{C}\right)} \right )  \quad \text{where} \quad C=1.3998
    
  • dist_type=CV_DIST_WELSCH

    \rho \left (r \right ) =  \frac{C^2}{2} \cdot \left ( 1 -  \exp{\left(-\left(\frac{r}{C}\right)^2\right)} \right )  \quad \text{where} \quad C=2.9846
    
  • dist_type=CV_DIST_HUBER

    \rho (r) =  \fork{r^2/2}{if $r < C$}{C \cdot (r-C/2)}{otherwise} \quad \text{where} \quad C=1.345
    

GetCentralMoment

id=0.28222157071 Comments from the Wiki

The function retrieves the central moment, which in the case of image moments is defined as:

\mu _{x \_ order,  \, y \_ order} =  \sum _{x,y} (I(x,y)  \cdot (x-x_c)^{x \_ order}  \cdot (y-y_c)^{y \_ order})

where x_c,y_c are the coordinates of the gravity center:

x_c= \frac{M_{10}}{M_{00}} , y_c= \frac{M_{01}}{M_{00}}

GetHuMoments

id=0.855980556214 Comments from the Wiki

The function calculates the seven Hu invariants, see http://en.wikipedia.org/wiki/Image_moment , that are defined as:

\begin{array}{l} hu_1= \eta _{20}+ \eta _{02} \\ hu_2=( \eta _{20}- \eta _{02})^{2}+4 \eta _{11}^{2} \\ hu_3=( \eta _{30}-3 \eta _{12})^{2}+ (3 \eta _{21}- \eta _{03})^{2} \\ hu_4=( \eta _{30}+ \eta _{12})^{2}+ ( \eta _{21}+ \eta _{03})^{2} \\ hu_5=( \eta _{30}-3 \eta _{12})( \eta _{30}+ \eta _{12})[( \eta _{30}+ \eta _{12})^{2}-3( \eta _{21}+ \eta _{03})^{2}]+(3 \eta _{21}- \eta _{03})( \eta _{21}+ \eta _{03})[3( \eta _{30}+ \eta _{12})^{2}-( \eta _{21}+ \eta _{03})^{2}] \\ hu_6=( \eta _{20}- \eta _{02})[( \eta _{30}+ \eta _{12})^{2}- ( \eta _{21}+ \eta _{03})^{2}]+4 \eta _{11}( \eta _{30}+ \eta _{12})( \eta _{21}+ \eta _{03}) \\ hu_7=(3 \eta _{21}- \eta _{03})( \eta _{21}+ \eta _{03})[3( \eta _{30}+ \eta _{12})^{2}-( \eta _{21}+ \eta _{03})^{2}]-( \eta _{30}-3 \eta _{12})( \eta _{21}+ \eta _{03})[3( \eta _{30}+ \eta _{12})^{2}-( \eta _{21}+ \eta _{03})^{2}] \\ \end{array}

where \eta_{ji} 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.

>>> import cv
>>> original = cv.LoadImageM("building.jpg", cv.CV_LOAD_IMAGE_GRAYSCALE)
>>> print cv.GetHuMoments(cv.Moments(original))
(0.0010620951868446141, 1.7962726159653835e-07, 1.4932744974469421e-11, 4.4832441315737963e-12, -1.0819359198251739e-23, -9.5726503811945833e-16, -3.5050592804744648e-23)
>>> flipped = cv.CloneMat(original)
>>> cv.Flip(original, flipped)
>>> print cv.GetHuMoments(cv.Moments(flipped))
(0.0010620951868446141, 1.796272615965384e-07, 1.4932744974469935e-11, 4.4832441315740249e-12, -1.0819359198259393e-23, -9.572650381193327e-16, 3.5050592804745877e-23)

GetNormalizedCentralMoment

id=0.725127020324 Comments from the Wiki

The function retrieves the normalized central moment:

\eta _{x \_ order,  \, y \_ order} =  \frac{\mu_{x\_order, \, y\_order}}{M_{00}^{(y\_order+x\_order)/2+1}}

GetSpatialMoment

id=0.191749557246 Comments from the Wiki

The function retrieves the spatial moment, which in the case of image moments is defined as:

M_{x \_ order,  \, y \_ order} =  \sum _{x,y} (I(x,y)  \cdot x^{x \_ order}  \cdot y^{y \_ order})

where I(x,y) is the intensity of the pixel (x, y) .

MatchContourTrees

id=0.480547577878 Comments from the Wiki

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.

MatchShapes

id=0.317335085559 Comments from the Wiki

The function compares two shapes. The 3 implemented methods all use Hu moments (see :ref:`GetHuMoments` ) ( A is object1 , B is object2 ):

  • method=CV_CONTOUR_MATCH_I1

    I_1(A,B) =  \sum _{i=1...7}  \left |  \frac{1}{m^A_i} -  \frac{1}{m^B_i} \right |
    
  • method=CV_CONTOUR_MATCH_I2

    I_2(A,B) =  \sum _{i=1...7}  \left | m^A_i - m^B_i  \right |
    
  • method=CV_CONTOUR_MATCH_I3

    I_3(A,B) =  \sum _{i=1...7}  \frac{ \left| m^A_i - m^B_i \right| }{ \left| m^A_i \right| }
    

where

\begin{array}{l} m^A_i = sign(h^A_i)  \cdot \log{h^A_i} m^B_i = sign(h^B_i)  \cdot \log{h^B_i} \end{array}

and h^A_i, h^B_i are the Hu moments of A and B respectively.

MinAreaRect2

id=0.271581100753 Comments from the Wiki

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

../pics/minareabox.png

MinEnclosingCircle

id=0.696424733079 Comments from the Wiki

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).

Moments

id=0.590982994221 Comments from the Wiki

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.

PointPolygonTest

id=0.315610322671 Comments from the Wiki

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 \texttt{measure\_dist} =0 , the return value is +1, -1 and 0, respectively. When \texttt{measure\_dist} \ne 0 , 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.

../pics/pointpolygon.png