-228 opencv assertion_error ai_generated true

cv::error: (-228:未知错误码 228) 在函数 'findHomography' 中,单应性掩码尺寸无效

cv::error: (-228:Unknown error code 228) The homography mask has invalid size in function 'findHomography'

ID: opencv/homography-invalid-mask

其他格式: JSON · Markdown 中文 · English
88%修复率
85%置信度
1证据数
2023-08-15首次发现

版本兼容性

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

根因分析

传递给 findHomography 的掩码参数的行数与输入点对数量不一致,导致尺寸不匹配断言失败。

English

The mask parameter passed to findHomography has a different number of rows than the number of input point pairs, causing a size mismatch assertion.

generic

官方文档

https://docs.opencv.org/4.x/d9/d0c/group__calib3d.html#ga4abc2ece9fab9398f2e560d53c8c9780

解决方案

  1. 确保掩码是一个 np.uint8 类型的 1D 或 2D numpy 数组,长度等于点对数量。示例:mask = np.ones(len(src_points), dtype=np.uint8)
  2. 如果使用 RANSAC,让 OpenCV 自动生成掩码,将 mask 参数设为 None:H, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
  3. 手动从匹配结果构建掩码时,先展平为 1D:mask = matches_mask.flatten().astype(np.uint8)

无效尝试

常见但无效的做法:

  1. 35% 失败

    Assuming the mask must be a list of booleans instead of a numpy array of type uint8 or int32

  2. 25% 失败

    Resizing the mask to match the number of points without checking that the mask was generated from the correct matching step

  3. 20% 失败

    Using np.ones((n,1)) instead of np.ones((n,1), dtype=np.uint8) — OpenCV expects specific dtype