To find the different features of contours, like area, perimeter, centroid, bounding box etc
You will learn plenty of functions related to contours.
1. Moments
Image moments help you to calculate some features like center of mass of the object, area of the object etc. Check out the wikipedia page on Image Moments
We use the function: cv.moments (array, binaryImage = false)
Parameters
array
raster image (single-channel, 8-bit or floating-point 2D array) or an array ( 1×N or N×1 ) of 2D points.
binaryImage
if it is true, all non-zero image pixels are treated as 1's. The parameter is used for images only.
From this moments, you can extract useful data like area, centroid etc. Centroid is given by the relations, \(C_x = \frac{M_{10}}{M_{00}}\) and \(C_y = \frac{M_{01}}{M_{00}}\). This can be done as follows:
letcx = M.m10/M.m00letcy = M.m01/M.m00
2. Contour Area
Contour area is given by the function cv.contourArea() or from moments, M['m00'].
We use the function: cv.contourArea (contour, oriented = false)
Parameters
contour
input vector of 2D points (contour vertices)
oriented
oriented area flag. If it is true, the function returns a signed area value, depending on the contour orientation (clockwise or counter-clockwise). Using this feature you can determine orientation of a contour by taking the sign of an area. By default, the parameter is false, which means that the absolute value is returned.
It approximates a contour shape to another shape with less number of vertices depending upon the precision we specify. It is an implementation of Douglas-Peucker algorithm. Check the wikipedia page for algorithm and demonstration.
We use the function: cv.approxPolyDP (curve, approxCurve, epsilon, closed)
Convex Hull will look similar to contour approximation, but it is not (Both may provide same results in some cases). Here, cv.convexHull() function checks a curve for convexity defects and corrects it. Generally speaking, convex curves are the curves which are always bulged out, or at-least flat. And if it is bulged inside, it is called convexity defects. For example, check the below image of hand. Red line shows the convex hull of hand. The double-sided arrow marks shows the convexity defects, which are the local maximum deviations of hull from contours.
image
We use the function: cv.convexHull (points, hull, clockwise = false, returnPoints = true)
Parameters
points
input 2D point set.
hull
output convex hull.
clockwise
orientation flag. If it is true, the output convex hull is oriented clockwise. Otherwise, it is oriented counter-clockwise. The assumed coordinate system has its X axis pointing to the right, and its Y axis pointing upwards.
returnPoints
operation flag. In case of a matrix, when the flag is true, the function returns convex hull points. Otherwise, it returns indices of the convex hull points.
Next we find the circumcircle of an object using the function cv.minEnclosingCircle(). It is a circle which completely covers the object with minimum area.
Next one is to fit an ellipse to an object. It returns the rotated rectangle in which the ellipse is inscribed. We use the functions: cv.fitEllipse (points)
Similarly we can fit a line to a set of points. We can approximate a straight line to it.
We use the functions: cv.fitLine (points, line, distType, param, reps, aeps)
Parameters
points
input 2D point set.
line
output line parameters. It should be a Mat of 4 elements[vx, vy, x0, y0], where [vx, vy] is a normalized vector collinear to the line and [x0, y0] is a point on the line.