-215 opencv type_error ai_generated true

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

ID: opencv/stereo-bm-disparity-type

Also available as: JSON · Markdown · 中文
90%Fix Rate
87%Confidence
1Evidence
2023-09-05First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
4.5.0 active
4.6.0 active
4.7.0 active
4.8.0 active
4.9.0 active

Root Cause

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

generic

中文

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

Official Documentation

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

Workarounds

  1. 95% success 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).
    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. 85% success Check the image type with img.dtype and img.shape before calling compute; ensure dtype is uint8 and len(shape)==2.
    Check the image type with img.dtype and img.shape before calling compute; ensure dtype is uint8 and len(shape)==2.

中文步骤

  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.

Dead Ends

Common approaches that don't work:

  1. Convert images to float32 before passing to StereoBM 85% fail

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

  2. Resize images to be larger 95% fail

    The issue is data type, not image size.

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

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