-215 opencv assertion_error ai_generated true

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' 中

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

其他格式: JSON · Markdown 中文 · English
90%修复率
86%置信度
1证据数
2023-11-02首次发现

版本兼容性

版本状态引入弃用备注
4.5.5 active
4.6.0 active
4.7.0 active
4.8.0 active
4.9.0 active

根因分析

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

English

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

generic

官方文档

https://docs.opencv.org/4.x/df/d1c/group__imgproc__geometric.html#ga1fa4a6a5b2f5e6b0e2b2c0d1a2b3c4d5

解决方案

  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.

无效尝试

常见但无效的做法:

  1. 95% 失败

    The function expects all coordinates to be within [0, image dimensions]; negative values cause immediate failure.

  2. 90% 失败

    getRectSubPix requires the entire patch to fit within the image; no automatic cropping is performed.