# cv::error: (-215:Assertion failed) 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows in function 'getRectSubPix'

- **ID:** `opencv/imgproc-getrectsubpix-out-of-bounds`
- **Domain:** opencv
- **Category:** assertion_error
- **Error Code:** `-215`
- **Verification:** ai_generated
- **Fix Rate:** 90%

## Root Cause

The region of interest (ROI) specified by the center point and patch size extends beyond the image boundaries in getRectSubPix.

## 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. **Clamp the center point coordinates so that the entire patch stays within image bounds: center_x = max(patch_w/2, min(center_x, img_w - patch_w/2)); center_y = max(patch_h/2, min(center_y, img_h - patch_h/2))** (95% success)
   ```
   Clamp the center point coordinates so that the entire patch stays within image bounds: center_x = max(patch_w/2, min(center_x, img_w - patch_w/2)); center_y = max(patch_h/2, min(center_y, img_h - patch_h/2))
   ```
2. **If the patch must be exactly at the requested location, pad the image with zeros or border replication before calling getRectSubPix: padded = cv2.copyMakeBorder(img, patch_h, patch_h, patch_w, patch_w, cv2.BORDER_REPLICATE); then adjust the center coordinates accordingly.** (85% success)
   ```
   If the patch must be exactly at the requested location, pad the image with zeros or border replication before calling getRectSubPix: padded = cv2.copyMakeBorder(img, patch_h, patch_h, patch_w, patch_w, cv2.BORDER_REPLICATE); then adjust the center coordinates accordingly.
   ```

## Dead Ends

- **** — The function expects all coordinates to be within [0, image dimensions]; negative values cause immediate failure. (95% fail)
- **** — getRectSubPix requires the entire patch to fit within the image; no automatic cropping is performed. (90% fail)
