# cv::error: (-215:断言失败) hierarchy.checkVector(3, 4) >= 0 在函数 'drawContours' 中

- **ID:** `opencv/contour-not-found-in-hierarchy`
- **领域:** opencv
- **类别:** assertion_error
- **错误码:** `-215`
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

在调用 findContours 后使用 RETR_TREE 或 RETR_CCOMP 模式调用 drawContours 时，轮廓层次向量为空或格式错误。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 4.5.5 | active | — | — |
| 4.6.0 | active | — | — |
| 4.7.0 | active | — | — |
| 4.8.0 | active | — | — |

## 解决方案

1. ```
   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)`
   ```
2. ```
   Use RETR_LIST mode which returns no hierarchy, then draw without hierarchy parameter.
   ```
3. ```
   Ensure contours list is non-empty before drawing: `if len(contours) > 0: cv2.drawContours(img, contours, -1, (0,255,0), 2, hierarchy=hierarchy)`
   ```

## 无效尝试

- **** — Setting RETR_EXTERNAL only retrieves outer contours, but hierarchy is still required for drawing with RETR_TREE. The error persists because hierarchy is empty. (60% 失败率)
- **** — Changing CHAIN_APPROX_NONE to other approximations doesn't fix hierarchy emptiness; it only changes point storage. (50% 失败率)
- **** — Increasing threshold values may reduce contour count but doesn't guarantee hierarchy is non-empty. (40% 失败率)
