Straightforward solution on how to stereo calibration and rectifications OpenCV?

后端 未结 1 548
故里飘歌
故里飘歌 2021-01-16 07:03

I have been digging on this topic for almost a week and couldn\'t find any solid solution yet. It is interesting that no one ever posted a straightforward s

相关标签:
1条回答
  • 2021-01-16 07:48

    I couldn't find what was the thing I did wrong which led to incorrect answers, but for what it worth I have found a solution that does rectify OK and more!!
    I came across the StereoVision library and considering the low documentation level it has, I have managed to fetch/write the following snaps which calibrate and rectifies OK.

    import cv2
    import os.path
    import numpy as np
    from stereovision.calibration import StereoCalibrator, StereoCalibration
    from stereovision.blockmatchers import StereoBM, StereoSGBM
    
    calib_dir = 'data/config/calibration'
    if(not os.path.exists(calib_dir)):
        calibrator = StereoCalibrator(9, 6, 2, (480, 640))
        for idx in range(1, 14):
            calibrator.add_corners((cv2.imread('images/left%02d.jpg' %idx), cv2.imread('images/right%02d.jpg' %idx)))
    
        calibration = calibrator.calibrate_cameras()
        print "Calibation error:", calibrator.check_calibration(calibration)
        calibration.export(calib_dir)
    
    calibration = StereoCalibration(input_folder=calib_dir)
    
    if True:
        block_matcher = StereoBM()
    else:
        block_matcher = StereoSGBM()
    
    for idx in range(1, 14):
        image_pair = (cv2.imread('images/left%02d.jpg' %idx), cv2.imread('images/right%02d.jpg' %idx))
        rectified_pair = calibration.rectify(image_pair)
        disparity = block_matcher.get_disparity(rectified_pair)
        norm_coeff = 255 / disparity.max()
        cv2.imshow('Disparity %02d' %idx, disparity * norm_coeff / 255)
    
        for line in range(0, int(rectified_pair[0].shape[0] / 20)):
            rectified_pair[0][line * 20, :] = (0, 0, 255)
            rectified_pair[1][line * 20, :] = (0, 0, 255)
    
        cv2.imshow('Rect %02d' %idx, np.hstack(rectified_pair))
        cv2.waitKey()
    

    The following is the result of rectification of the same image I have posted in my question. Although for computing the disparity map It needs its parameters to be tuned(a tool is provided by the package) but it will do the job :)

    0 讨论(0)
提交回复
热议问题