Sub topic 8
Measuring Contours: Area, Perimeter and Centroid
Prerequisites and measurement model
Assume you know basic Python, NumPy array indexing, and how to load and save images with OpenCV. You should also understand that an image is an array of pixels and that a binary mask separates foreground pixels from background pixels.
A contour is a connected boundary represented by an ordered list of points. OpenCV stores each point as an array containing its x and y pixel coordinates. Contour measurements use these coordinates, so area is reported in square pixels and perimeter is reported in pixels unless you apply a camera calibration.
The following example assumes that contours came from a binary image:
contours, hierarchy = cv2.findContours(
binary,
cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE
)Area and perimeter
cv2.contourArea estimates the region enclosed by a contour. It does not count foreground pixels directly. The result can therefore differ slightly from the number of nonzero pixels in a mask, especially around curved or diagonal boundaries.
cv2.arcLength calculates the length of the contour. Pass True for a closed object boundary. Use False for an open curve.
import cv2
for index, contour in enumerate(contours):
area = cv2.contourArea(contour)
perimeter = cv2.arcLength(contour, True)
print(
f"contour {index}: "
f"area={area:.1f} square pixels, "
f"perimeter={perimeter:.1f} pixels"
)The contour direction can matter when you request a signed area:
signed_area = cv2.contourArea(contour, oriented=True)With the default oriented=False, OpenCV returns a nonnegative area. A signed result can help distinguish the direction of outer contours from hole contours, but it is rarely needed for basic object measurement.
Centroid from image moments
A centroid is the geometric center of a contour. For a uniform object, it is the point where the shape would balance. OpenCV computes it from image moments returned by cv2.moments.
For a contour, the common formulas are:
cx = m10 / m00cy = m01 / m00
Here, m00 is related to the contour area, while m10 and m01 contain weighted coordinate sums. Always check whether m00 is zero before dividing. A degenerate contour may have no enclosed area.
for index, contour in enumerate(contours):
area = cv2.contourArea(contour)
perimeter = cv2.arcLength(contour, True)
moments = cv2.moments(contour)
if moments["m00"] == 0:
print(f"contour {index}: no measurable area")
continue
cx = moments["m10"] / moments["m00"]
cy = moments["m01"] / moments["m00"]
print(
f"contour {index}: area={area:.1f}, "
f"perimeter={perimeter:.1f}, "
f"centroid=({cx:.1f}, {cy:.1f})"
)The centroid uses image coordinates. The origin is at the top-left corner, x increases to the right, and y increases downward. If you need physical units such as millimeters, convert the coordinates using a calibrated pixel-to-world mapping.
> callout note > A contour measurement is only as reliable as the mask that produced it. Gaps, attached noise, and incorrect foreground polarity change the boundary and therefore change every measurement.
Interpreting measurements
Area and perimeter are useful together. For example, a long thin object can have a moderate area but a large perimeter. A compact object usually encloses more area relative to its boundary.
Do not compare raw measurements from images with different scales. Doubling the image scale multiplies area by four but perimeter by two. Centroid coordinates also change with the image origin and scale.
Exercise
Why should a program check moments["m00"] before calculating a contour centroid?
Show answer
Because m00 can be zero for a degenerate contour, and dividing by zero would produce an invalid centroid.