# cv::error: (-215：断言失败) count >= 0 在函数 'cv::approxPolyDP' 中

- **ID:** `opencv/contour-approximation-empty`
- **领域:** opencv
- **类别:** assertion_error
- **错误码:** `-215`
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

approxPolyDP 接收到空或无效的轮廓（零个点），因为输入轮廓未被正确提取或被过滤掉。

## 版本兼容性

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

## 解决方案

1. ```
   在调用 approxPolyDP 前添加守卫检查：if len(contour) == 0: continue。示例：for cnt in contours: if cnt.shape[0] < 3: continue; approx = cv2.approxPolyDP(cnt, epsilon, True)
   ```
2. ```
   使用 cv2.contourArea 过滤面积为零的轮廓：if cv2.contourArea(cnt) > 0: approx = cv2.approxPolyDP(cnt, 0.02 * cv2.arcLength(cnt, True), True)
   ```
3. ```
   确保 findContours 使用正确的模式（如 RETR_TREE 或 RETR_LIST），并且层次结构未损坏；如果图像全黑，轮廓[0]可能为空
   ```

## 无效尝试

- **** — Increasing the epsilon parameter to make the approximation less strict, which only masks the issue but doesn't fix empty contours (40% 失败率)
- **** — Re-running findContours with different retrieval modes (e.g., RETR_EXTERNAL) without checking if contours exist (30% 失败率)
- **** — Assuming the contour is always valid and skipping the empty check entirely (50% 失败率)
