-228 opencv assertion_error ai_generated true

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

ID: opencv/homography-invalid-mask

Also available as: JSON · Markdown · 中文
88%Fix Rate
85%Confidence
1Evidence
2023-08-15First 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 mask parameter passed to findHomography has a different number of rows than the number of input point pairs, causing a size mismatch assertion.

generic

中文

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

Official Documentation

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

Workarounds

  1. 90% success Ensure the mask is a 1D or 2D numpy array of type np.uint8 with length equal to the number of point pairs. Example: mask = np.ones(len(src_points), dtype=np.uint8)
    Ensure the mask is a 1D or 2D numpy array of type np.uint8 with length equal to the number of point pairs. Example: mask = np.ones(len(src_points), dtype=np.uint8)
  2. 95% success If using RANSAC, let OpenCV generate the mask automatically by passing None for the mask parameter: H, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
    If using RANSAC, let OpenCV generate the mask automatically by passing None for the mask parameter: H, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
  3. 85% success When manually constructing a mask from matching results, flatten the mask to 1D: mask = matches_mask.flatten().astype(np.uint8)
    When manually constructing a mask from matching results, flatten the mask to 1D: mask = matches_mask.flatten().astype(np.uint8)

中文步骤

  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)

Dead Ends

Common approaches that don't work:

  1. 35% fail

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

  2. 25% fail

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

  3. 20% fail

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