# cv::error: (-215:断言失败) src1.type() == CV_8UC1 && src2.type() == CV_8UC1 在函数 'cv::StereoBM::compute' 中

- **ID:** `opencv/stereo-bm-disparity-type`
- **领域:** opencv
- **类别:** type_error
- **错误码:** `-215`
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

StereoBM 的输入图像必须是单通道 8 位灰度图像；彩色图像或其他类型会导致此断言。

## 版本兼容性

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

## 解决方案

1. ```
   Load images as grayscale using cv2.imread with cv2.IMREAD_GRAYSCALE flag, or convert color images to grayscale using cv2.cvtColor(img_left, cv2.COLOR_BGR2GRAY).
   ```
2. ```
   Check the image type with img.dtype and img.shape before calling compute; ensure dtype is uint8 and len(shape)==2.
   ```

## 无效尝试

- **Convert images to float32 before passing to StereoBM** — StereoBM requires CV_8UC1 (uint8), not float. Converting to float will cause a different type assertion error. (85% 失败率)
- **Resize images to be larger** — The issue is data type, not image size. (95% 失败率)
- **Use cv2.cvtColor with COLOR_BGR2GRAY on already grayscale images** — If the image is already single-channel, cvtColor will fail; the correct fix is to ensure the image is loaded as grayscale. (50% 失败率)
