Straightforward solution on how to stereo calibration and rectifications OpenCV?

与世无争的帅哥 提交于 2019-12-01 12:39:01

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 :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!