-215
opencv
assertion_error
ai_generated
true
cv::error: (-215:Assertion failed) hierarchy.checkVector(3, 4) >= 0 in function 'drawContours'
ID: opencv/contour-not-found-in-hierarchy
80%Fix Rate
85%Confidence
1Evidence
2023-04-15First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 4.5.5 | active | — | — | — |
| 4.6.0 | active | — | — | — |
| 4.7.0 | active | — | — | — |
| 4.8.0 | active | — | — | — |
Root Cause
Contour hierarchy vector is empty or malformed when calling drawContours with RETR_TREE or RETR_CCOMP mode after findContours.
generic中文
在调用 findContours 后使用 RETR_TREE 或 RETR_CCOMP 模式调用 drawContours 时,轮廓层次向量为空或格式错误。
Official Documentation
https://docs.opencv.org/4.x/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689aWorkarounds
-
85% success Check if hierarchy is not None and has at least 4 elements before calling drawContours. Example: `if hierarchy is not None and len(hierarchy) > 0: cv2.drawContours(img, contours, -1, (0,255,0), 2)`
Check if hierarchy is not None and has at least 4 elements before calling drawContours. Example: `if hierarchy is not None and len(hierarchy) > 0: cv2.drawContours(img, contours, -1, (0,255,0), 2)`
-
75% success Use RETR_LIST mode which returns no hierarchy, then draw without hierarchy parameter.
Use RETR_LIST mode which returns no hierarchy, then draw without hierarchy parameter.
-
90% success Ensure contours list is non-empty before drawing: `if len(contours) > 0: cv2.drawContours(img, contours, -1, (0,255,0), 2, hierarchy=hierarchy)`
Ensure contours list is non-empty before drawing: `if len(contours) > 0: cv2.drawContours(img, contours, -1, (0,255,0), 2, hierarchy=hierarchy)`
中文步骤
Check if hierarchy is not None and has at least 4 elements before calling drawContours. Example: `if hierarchy is not None and len(hierarchy) > 0: cv2.drawContours(img, contours, -1, (0,255,0), 2)`
Use RETR_LIST mode which returns no hierarchy, then draw without hierarchy parameter.
Ensure contours list is non-empty before drawing: `if len(contours) > 0: cv2.drawContours(img, contours, -1, (0,255,0), 2, hierarchy=hierarchy)`
Dead Ends
Common approaches that don't work:
-
60% fail
Setting RETR_EXTERNAL only retrieves outer contours, but hierarchy is still required for drawing with RETR_TREE. The error persists because hierarchy is empty.
-
50% fail
Changing CHAIN_APPROX_NONE to other approximations doesn't fix hierarchy emptiness; it only changes point storage.
-
40% fail
Increasing threshold values may reduce contour count but doesn't guarantee hierarchy is non-empty.