-215 opencv type_error ai_generated true

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

cv::error: (-215:Assertion failed) src1.type() == CV_8UC1 && src2.type() == CV_8UC1 in function 'cv::StereoBM::compute'

ID: opencv/stereo-bm-disparity-type

其他格式: JSON · Markdown 中文 · English
90%修复率
87%置信度
1证据数
2023-09-05首次发现

版本兼容性

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

根因分析

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

English

The input images to StereoBM must be single-channel 8-bit grayscale images; color images or other types cause this assertion.

generic

官方文档

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

解决方案

  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.

无效尝试

常见但无效的做法:

  1. Convert images to float32 before passing to StereoBM 85% 失败

    StereoBM requires CV_8UC1 (uint8), not float. Converting to float will cause a different type assertion error.

  2. Resize images to be larger 95% 失败

    The issue is data type, not image size.

  3. Use cv2.cvtColor with COLOR_BGR2GRAY on already grayscale images 50% 失败

    If the image is already single-channel, cvtColor will fail; the correct fix is to ensure the image is loaded as grayscale.