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

- **ID:** `opencv/homography-invalid-mask`
- **领域:** opencv
- **类别:** assertion_error
- **错误码:** `-228`
- **验证级别:** ai_generated
- **修复率:** 88%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 4.5.5 | active | — | — |
| 4.6.0 | active | — | — |
| 4.7.0 | active | — | — |
| 4.8.0 | active | — | — |
| 4.9.0 | active | — | — |

## 解决方案

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)
   ```

## 无效尝试

- **** — Assuming the mask must be a list of booleans instead of a numpy array of type uint8 or int32 (35% 失败率)
- **** — Resizing the mask to match the number of points without checking that the mask was generated from the correct matching step (25% 失败率)
- **** — Using np.ones((n,1)) instead of np.ones((n,1), dtype=np.uint8) — OpenCV expects specific dtype (20% 失败率)
