In OpenCV, the orientation of a contour (clockwise or counter-clockwise) is not always the same and depends on whether it's an external contour or an internal contour (a hole).
Here's the general convention used by cv2.findContours():
External Contours (Outer Boundaries):
These are typically traced in a counter-clockwise (CCW) direction.
Reasoning: This convention means that as you trace the contour, the "interior" of the object is always to your left.
Verification: If you calculate the area of an external contour using cv2.contourArea(), it will return a positive value.
Internal Contours (Holes):
These are typically traced in a clockwise (CW) direction.
Reasoning: This is done to maintain consistency. If you imagine tracing the hole, its "interior" (which is actually the filled part of the parent object, or the "outside" of the hole) is still to your left. Or, simply, it's the opposite orientation of its parent contour.
Verification: If you calculate the area of an internal contour (a hole) using cv2.contourArea(), it will return a negative value.
Example:
Imagine a donut shape (a circle with a hole in the middle):
The outer circle (external contour) will be traced counter-clockwise.
The inner circle (internal contour/hole) will be traced clockwise.
When you sum the signed areas of these two contours, you get the correct total area of the filled part of the donut.
Why this matters:
Signed Area: The signed area returned by cv2.contourArea() is a direct indicator of orientation.
Consistency: This convention helps various algorithms (like point-in-polygon tests, or determining convexity) to work predictably.
How to get a consistent orientation if needed:
If for some reason you need all your contours to be in a specific orientation (e.g., all CCW), you can check their area and reverse them if necessary:
python
import cv2
import numpy as np
# Create a dummy image with a square and a hole
image = np.zeros((100, 100), dtype=np.uint8)
cv2.rectangle(image, (10, 10), (90, 90), 255, -1) # Outer square (solid)
cv2.rectangle(image, (30, 30), (70, 70), 0, -1) # Inner square (hole)
contours, hierarchy = cv2.findContours(image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for i, contour in enumerate(contours):
area = cv2.contourArea(contour)
print(f"Contour {i}: Area = {area:.2f}")
if area > 0:
print(f" --> Likely an external contour (CCW)")
elif area < 0:
print(f" --> Likely an internal contour/hole (CW)")
# If you wanted to make it CCW, you could reverse it:
# contour_reversed = contour[::-1]
# print(f" --> Area if reversed: {cv2.contourArea(contour_reversed):.2f}")
else:
print(f" --> Area is zero (e.g., a point or line)")
# Expected output will show one positive area (CCW) and one negative area (CW)
In summary, OpenCV's findContours intelligently assigns orientation based on whether the contour defines the outer boundary of an object or a hole within it.
User
What if i always use
cv::findContours(maks, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
?
Assistant
You are absolutely correct to ask this specific question!
If you always use cv::findContours(mask, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);, then:
All the contours returned in your contours vector will always be traced in a counter-clockwise (CCW) direction.
Here's why:
cv::RETR_EXTERNAL's Purpose: This retrieval mode specifically tells findContours to only find the outermost (external) contours of objects. It completely ignores any internal contours (holes) or contours nested within other contours.
OpenCV's Convention for External Contours: As discussed, OpenCV's convention for external contours is to trace them in a counter-clockwise (CCW) direction. This ensures that the "interior" of the object is always to the left as you follow the contour.
Resulting Orientation: Since RETR_EXTERNALonly returns external contours, and external contours are always CCW, all contours you get will consistently have a counter-clockwise orientation.
Verification:
If you calculate the area of any of these contours using cv::contourArea(), it will always return a positive value, confirming their counter-clockwise orientation.
In summary:
Using cv::RETR_EXTERNAL simplifies contour processing by ensuring all returned contours represent the outer boundaries of objects and share a consistent counter-clockwise orientation. You will never get a clockwise (negative area) contour when using this mode.