-215 opencv assertion_error ai_generated true

cv::error: (-215:Assertion failed) map1.size().area() > 0 in function 'cv::undistort'

ID: opencv/undistort-empty-map

Also available as: JSON · Markdown · 中文
89%Fix Rate
84%Confidence
1Evidence
2023-06-30First 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

undistort received an empty or uninitialized map (map1) because initUndistortRectifyMap was not called or produced an empty output due to invalid camera matrix parameters.

generic

中文

undistort 接收到空或未初始化的 map(map1),因为 initUndistortRectifyMap 未被调用,或由于相机矩阵参数无效而产生了空输出。

Official Documentation

https://docs.opencv.org/4.x/da/d54/group__imgproc__transform.html#ga55c716492470bfe86b0ee9bf3a1f0f7e

Workarounds

  1. 95% success Always call initUndistortRectifyMap before undistort. Example: newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx, dist, (w,h), 1, (w,h)); mapx, mapy = cv2.initUndistortRectifyMap(mtx, dist, None, newcameramtx, (w,h), 5); dst = cv2.remap(img, mapx, mapy, cv2.INTER_LINEAR). Check mapx.size > 0.
    Always call initUndistortRectifyMap before undistort. Example: newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx, dist, (w,h), 1, (w,h)); mapx, mapy = cv2.initUndistortRectifyMap(mtx, dist, None, newcameramtx, (w,h), 5); dst = cv2.remap(img, mapx, mapy, cv2.INTER_LINEAR). Check mapx.size > 0.
  2. 90% success If using cv2.undistort directly, ensure the camera matrix and distortion coefficients are not None and have correct shape: mtx must be 3x3 float64, dist must be 1xN or Nx1.
    If using cv2.undistort directly, ensure the camera matrix and distortion coefficients are not None and have correct shape: mtx must be 3x3 float64, dist must be 1xN or Nx1.
  3. 85% success Verify camera matrix validity: if np.any(np.isnan(mtx)) or np.any(np.isinf(mtx)): raise ValueError('Invalid camera matrix'). Recalibrate if needed.
    Verify camera matrix validity: if np.any(np.isnan(mtx)) or np.any(np.isinf(mtx)): raise ValueError('Invalid camera matrix'). Recalibrate if needed.

中文步骤

  1. 在 undistort 之前始终调用 initUndistortRectifyMap。示例:newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx, dist, (w,h), 1, (w,h)); mapx, mapy = cv2.initUndistortRectifyMap(mtx, dist, None, newcameramtx, (w,h), 5); dst = cv2.remap(img, mapx, mapy, cv2.INTER_LINEAR)。检查 mapx.size > 0。
  2. 如果直接使用 cv2.undistort,确保相机矩阵和畸变系数不为 None 且形状正确:mtx 必须是 3x3 float64,dist 必须是 1xN 或 Nx1。
  3. 验证相机矩阵有效性:if np.any(np.isnan(mtx)) or np.any(np.isinf(mtx)): raise ValueError('Invalid camera matrix')。必要时重新标定。

Dead Ends

Common approaches that don't work:

  1. 35% fail

    Re-running calibrateCamera with different flags without verifying that the camera matrix is finite and non-zero

  2. 25% fail

    Assuming the error is from the image size and resizing the image without recalculating the map

  3. 30% fail

    Using a pre-computed map from a different camera or resolution without checking its dimensions