Calculates all of the moments up to the third order of a polygon or rasterized shape.
where the class Moments is defined as:
class Moments
{
public:
Moments();
Moments(double m00, double m10, double m01, double m20, double m11,
double m02, double m30, double m21, double m12, double m03 );
Moments( const CvMoments& moments );
operator CvMoments() const;
// spatial moments
double m00, m10, m01, m20, m11, m02, m30, m21, m12, m03;
// central moments
double mu20, mu11, mu02, mu30, mu21, mu12, mu03;
// central normalized moments
double nu20, nu11, nu02, nu30, nu21, nu12, nu03;
};
param array: A raster image (single-channel, 8-bit or floating-point 2D array) or an array ( or ) of 2D points ( Point or Point2f ) param binaryImage: (For images only) If it is true, then all the non-zero image pixels are treated as 1’s
The function computes moments, up to the 3rd order, of a vector shape or a rasterized shape. In case of a raster image, the spatial moments are computed as:
the central moments are computed as:
where is the mass center:
and the normalized central moments are computed as:
Note that , , hence the values are not stored.
The moments of a contour are defined in the same way, but computed using Green’s formula (see http://en.wikipedia.org/wiki/Green_theorem ), therefore, because of a limited raster resolution, the moments computed for a contour will be slightly different from the moments computed for the same contour rasterized.
See also: contourArea() , arcLength()
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 stand for .
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.
See also: matchShapes()
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. See squares.c in the OpenCV sample directory.
Note: the source image is modified by this function.
Draws contours’ outlines or filled contours.
Parameters: |
|
---|
The function draws contour outlines in the image if or fills the area bounded by the contours if . Here is the example on how to retrieve connected components from the binary image and label them
#include "cv.h"
#include "highgui.h"
using namespace cv;
int main( int argc, char** argv )
{
Mat src;
// the first command line parameter must be file name of binary
// (black-n-white) image
if( argc != 2 || !(src=imread(argv[1], 0)).data)
return -1;
Mat dst = Mat::zeros(src.rows, src.cols, CV_8UC3);
src = src > 1;
namedWindow( "Source", 1 );
imshow( "Source", src );
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours( src, contours, hierarchy,
CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
// iterate through all the top-level contours,
// draw each connected component with its own random color
int idx = 0;
for( ; idx >= 0; idx = hierarchy[idx][0] )
{
Scalar color( rand()&255, rand()&255, rand()&255 );
drawContours( dst, contours, idx, color, CV_FILLED, 8, hierarchy );
}
namedWindow( "Components", 1 );
imshow( "Components", dst );
waitKey(0);
}
Approximates polygonal curve(s) with the specified precision.
Parameters: |
|
---|
The functions approxPolyDP approximate a curve or a polygon with another curve/polygon with less vertices, so that the distance between them is less or equal to the specified precision. It used Douglas-Peucker algorithm http://en.wikipedia.org/wiki/Ramer-Douglas-Peucker_algorithm
Calculates a contour perimeter or a curve length.
Parameters: |
|
---|
The function computes the curve length or the closed contour perimeter.
Calculates the up-right bounding rectangle of a point set.
Parameters: |
|
---|
The function calculates and returns the minimal up-right bounding rectangle for the specified point set.
Computes optimal affine transformation between two 2D point sets
Parameters: |
|
---|
The function finds the optimal affine transform (a floating-point matrix) that approximates best the transformation from to :
where can be either arbitrary (when fullAffine=true ) or have form
when fullAffine=false .
See also: getAffineTransform() , getPerspectiveTransform() , findHomography()
Computes optimal affine transformation between two 3D point sets
Parameters: |
|
---|
The function estimates the optimal 3D affine transformation between two 3D point sets using RANSAC algorithm.
Calculates the contour area
Parameters: |
|
---|
The function computes the contour area. Similarly to moments() the area is computed using the Green formula, thus the returned area and the number of non-zero pixels, if you draw the contour using drawContours() or fillPoly() , can be different. Here is a short example:
vector<Point> contour;
contour.push_back(Point2f(0, 0));
contour.push_back(Point2f(10, 0));
contour.push_back(Point2f(10, 10));
contour.push_back(Point2f(5, 4));
double area0 = contourArea(contour);
vector<Point> approx;
approxPolyDP(contour, approx, 5, true);
double area1 = contourArea(approx);
cout << "area0 =" << area0 << endl <<
"area1 =" << area1 << endl <<
"approx poly vertices" << approx.size() << endl;
Finds the convex hull of a point set.
Parameters: |
|
---|
The functions find the convex hull of a 2D point set using Sklansky’s algorithm Sklansky82 that has or complexity (where is the number of input points), depending on how the initial sorting is implemented (currently it is . See the OpenCV sample convexhull.c that demonstrates the use of the different function variants.
Fits an ellipse around a set of 2D points.
Parameters: |
|
---|
The function calculates the ellipse that fits best (in least-squares sense) a set of 2D points. It returns the rotated rectangle in which the ellipse is inscribed.
Fits a line to a 2D or 3D point set.
Parameters: |
|
---|
The functions fitLine fit a line to a 2D or 3D point set by minimizing where is the distance between the point and the line and is a distance function, one of:
distType=CV_DIST_L2
distType=CV_DIST_L1
distType=CV_DIST_L12
distType=CV_DIST_FAIR
distType=CV_DIST_WELSCH
distType=CV_DIST_HUBER
The algorithm is based on the M-estimator ( http://en.wikipedia.org/wiki/M-estimator ) technique, that iteratively fits the line using weighted least-squares algorithm and after each iteration the weights are adjusted to beinversely proportional to .
Tests contour convexity.
Parameters: |
|
---|
The function tests whether the input contour is convex or not. The contour must be simple, i.e. without self-intersections, otherwise the function output is undefined.
Finds the minimum area rotated rectangle enclosing a 2D point set.
Parameters: |
|
---|
The function calculates and returns the minimum area bounding rectangle (possibly rotated) for the specified point set. See the OpenCV sample minarea.c
Finds the minimum area circle enclosing a 2D point set.
Parameters: |
|
---|
The function finds the minimal enclosing circle of a 2D point set using iterative algorithm. See the OpenCV sample minarea.c
Compares two shapes.
Parameters: |
|
---|
The function compares two shapes. The 3 implemented methods all use Hu invariants (see HuMoments() ) as following ( denotes object1 , denotes 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.
Performs point-in-contour test.
Parameters: |
|
---|
The function determines whether the point is inside a contour, outside, or lies on an edge (or coincides with a vertex). It returns positive (inside), negative (outside) or zero (on an edge) value, correspondingly. When measureDist=false , the return value is +1, -1 and 0, respectively. Otherwise, the return value 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.