首页 > 解决方案 > OpenCV - Python Assertion Error : SAD algorithm - Stereo Camera Disparity Map Calculation

问题描述

I want to calculate Disparity map using SAD algorithm, and when I run the code I get the error Message: return stereo.compute(left_image, right_image).astype(np.float32) / 16.0 cv2.error: OpenCV(3.4.3) C:\projects\opencv-python\opencv\modules\calib3d\src\stereosgbm.cpp:2156: error: (-215:Assertion failed) left.size() == right.size() && left.type() == right.type() && left.depth() == CV_8U in function 'cv::StereoSGBMImpl::compute'

My code is :

left_image = cv2.imread('left.jpg')
right_image = cv2.imread('right.jpg')

def calc_disparity(left_image, right_image):
    window_size = 3
    min_disp = 1
    num_disp = 16
    stereo = cv2.StereoSGBM_create(
        minDisparity=min_disp,
        numDisparities=num_disp,
        blockSize = 5,
        # SADWindowSize=window_size,
        uniquenessRatio=10,
        speckleWindowSize=100,
        speckleRange=32,
        disp12MaxDiff=1,
        P1=8*3*window_size**2,
        P2=32*3*window_size**2,
        # fullDP=False

    )
    return stereo.compute(left_image, right_image).astype(np.float32) / 16.0

标签: pythonc++opencv

解决方案


此断言失败的可能原因是:

  • 左右图像的大小不同
  • 左右图像的类型不同
  • 左图的深度类型错误(需要 CV_8U)

请确保您的输入图像具有上述条件(相同大小和类型)。


推荐阅读