-215 opencv assertion_error ai_generated true

cv::error: (-215:断言失败) map1.size().area() > 0 在函数 'cv::undistort' 中

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

ID: opencv/undistort-empty-map

其他格式: JSON · Markdown 中文 · English
89%修复率
84%置信度
1证据数
2023-06-30首次发现

版本兼容性

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

根因分析

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

English

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

官方文档

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

解决方案

  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')。必要时重新标定。

无效尝试

常见但无效的做法:

  1. 35% 失败

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

  2. 25% 失败

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

  3. 30% 失败

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