# cv::error: (-215:断言失败) 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows 在函数 'getRectSubPix' 中

- **ID:** `opencv/imgproc-getrectsubpix-out-of-bounds`
- **领域:** opencv
- **类别:** assertion_error
- **错误码:** `-215`
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

由中心点和补丁大小指定的感兴趣区域 (ROI) 超出了 getRectSubPix 中的图像边界。

## 版本兼容性

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

## 解决方案

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))
   ```
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.
   ```

## 无效尝试

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