# cv::error: (-215:Assertion failed) count >= 0 in function 'cv::approxPolyDP'

- **ID:** `opencv/contour-approximation-empty`
- **Domain:** opencv
- **Category:** assertion_error
- **Error Code:** `-215`
- **Verification:** ai_generated
- **Fix Rate:** 85%

## Root Cause

approxPolyDP received an empty or invalid contour (zero points) because the input contour was not properly extracted or was filtered out.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 4.5.5 | active | — | — |
| 4.6.0 | active | — | — |
| 4.7.0 | active | — | — |
| 4.8.0 | active | — | — |
| 4.9.0 | active | — | — |

## Workarounds

1. **Add a guard clause before calling approxPolyDP: if len(contour) == 0: continue. Example: for cnt in contours: if cnt.shape[0] < 3: continue; approx = cv2.approxPolyDP(cnt, epsilon, True)** (95% success)
   ```
   Add a guard clause before calling approxPolyDP: if len(contour) == 0: continue. Example: for cnt in contours: if cnt.shape[0] < 3: continue; approx = cv2.approxPolyDP(cnt, epsilon, True)
   ```
2. **Use cv2.contourArea to filter out contours with zero area before approximation: if cv2.contourArea(cnt) > 0: approx = cv2.approxPolyDP(cnt, 0.02 * cv2.arcLength(cnt, True), True)** (90% success)
   ```
   Use cv2.contourArea to filter out contours with zero area before approximation: if cv2.contourArea(cnt) > 0: approx = cv2.approxPolyDP(cnt, 0.02 * cv2.arcLength(cnt, True), True)
   ```
3. **Ensure findContours is called with the correct mode (e.g., RETR_TREE or RETR_LIST) and that the hierarchy is not corrupt; sometimes contour[0] can be empty if the image is all black** (80% success)
   ```
   Ensure findContours is called with the correct mode (e.g., RETR_TREE or RETR_LIST) and that the hierarchy is not corrupt; sometimes contour[0] can be empty if the image is all black
   ```

## Dead Ends

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