-215 opencv assertion_error ai_generated true

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

Also available as: JSON · Markdown · 中文
90%Fix Rate
86%Confidence
1Evidence
2023-11-02First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
4.5.5 active
4.6.0 active
4.7.0 active
4.8.0 active
4.9.0 active

Root Cause

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

generic

中文

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

Official Documentation

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 95% fail

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

  2. 90% fail

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