Sub topic 5
Choosing a Contour Approximation Method
This chapter assumes that you can load images with OpenCV, work with NumPy arrays, and prepare a binary mask. A contour is a list of points that follows the boundary of a connected region. OpenCV can store that boundary in different ways.
Two common chain-approximation methods
Pass the approximation method as the fourth argument to cv2.findContours:
contours, hierarchy = cv2.findContours(
binary,
cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE
)CHAIN_APPROX_NONE stores every boundary point. If neighboring pixels follow a straight horizontal, vertical, or diagonal line, many of those points are redundant. This method preserves them anyway.
CHAIN_APPROX_SIMPLE removes redundant points while preserving the contour's shape. For example, a rectangular boundary may need only its corner points instead of every pixel along its four sides.
| Method | Stored points | Memory use | Useful when |
|---|---|---|---|
| `CHAIN_APPROX_NONE` | Nearly every boundary pixel | Higher | You need pixel-level boundary detail |
| `CHAIN_APPROX_SIMPLE` | Essential points on straight sections | Lower | You need measurements or shape detection |
| `CHAIN_APPROX_TC89_L1` | Teh-Chin simplified boundary | Usually lower | You want an alternative contour simplifier |
| `CHAIN_APPROX_TC89_KCOS` | Teh-Chin simplified boundary | Usually lower | You want another detail-preserving simplifier |
CHAIN_APPROX_SIMPLE is usually the best starting point. It reduces processing time and memory without changing the broad shape.
Compare the methods
The following program uses the same binary image twice and reports the number of points in each contour:
import cv2
binary = cv2.imread("mask.png", cv2.IMREAD_GRAYSCALE)
if binary is None:
raise FileNotFoundError("Could not read mask.png")
_, binary = cv2.threshold(binary, 127, 255, cv2.THRESH_BINARY)
for method, name in [
(cv2.CHAIN_APPROX_NONE, "NONE"),
(cv2.CHAIN_APPROX_SIMPLE, "SIMPLE"),
]:
contours, _ = cv2.findContours(
binary.copy(),
cv2.RETR_EXTERNAL,
method
)
print(name)
for index, contour in enumerate(contours):
print(f" contour {index}: {len(contour)} points")The .copy() is useful because OpenCV versions and related image-processing code may modify an input array. Giving each call its own copy makes the comparison predictable.
Chain approximation versus polygon approximation
Chain approximation happens while findContours stores the boundary. Polygon approximation happens afterward with cv2.approxPolyDP.
CHAIN_APPROX_SIMPLE might store the corners of a rectangle, but a noisy or curved object can still contain many points. approxPolyDP simplifies that contour according to an error tolerance called epsilon.
perimeter = cv2.arcLength(contour, True)
epsilon = 0.02 * perimeter
polygon = cv2.approxPolyDP(contour, epsilon, True)
print("original points:", len(contour))
print("polygon points:", len(polygon))A smaller epsilon keeps more detail. A larger value produces a simpler polygon but can remove meaningful corners. Use the same approximation method when comparing experiments, then tune epsilon for the classification task.
Exercise
Which method should you choose for detecting whether an object has four corners, and why?
Show answer
Start with CHAINAPPROXSIMPLE, then apply approxPolyDP with a suitable epsilon. SIMPLE reduces redundant points, while approxPolyDP exposes the meaningful polygon corners.