# cv::error: (-215:Assertion failed) hierarchy.checkVector(3, 4) >= 0 in function 'drawContours'

- **ID:** `opencv/contour-not-found-in-hierarchy`
- **Domain:** opencv
- **Category:** assertion_error
- **Error Code:** `-215`
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Contour hierarchy vector is empty or malformed when calling drawContours with RETR_TREE or RETR_CCOMP mode after findContours.

## Version Compatibility

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

## Workarounds

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)`** (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)`
   ```
2. **Use RETR_LIST mode which returns no hierarchy, then draw without hierarchy parameter.** (75% success)
   ```
   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)`** (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)`
   ```

## Dead Ends

- **** — 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% fail)
- **** — Changing CHAIN_APPROX_NONE to other approximations doesn't fix hierarchy emptiness; it only changes point storage. (50% fail)
- **** — Increasing threshold values may reduce contour count but doesn't guarantee hierarchy is non-empty. (40% fail)
