python - 立体标定Opencv Python和视差图

标签 python opencv image-processing computer-vision camera-calibration

我有兴趣找到一个场景的视差图。首先,我使用以下代码进行了立体校准(我在 Google 的一点帮助下自己编写了它,因为没有找到任何有用的教程,用 Python 为 OpenCV 2.4.10 编写了相同的代码)。

我用两个相机同时拍摄了棋盘的图像,并将它们保存为 left*.jpg 和 right*.jpg。

import numpy as np
import cv2
import glob

# termination criteria
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)

# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((6*9,3), np.float32)
objp[:,:2] = np.mgrid[0:9,0:6].T.reshape(-1,2)


# Arrays to store object points and image points from all the images.
objpointsL = [] # 3d point in real world space
imgpointsL = [] # 2d points in image plane.
objpointsR = []
imgpointsR = []

images = glob.glob('left*.jpg')

for fname in images:
    img = cv2.imread(fname)
    grayL = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    # Find the chess board corners
    ret, cornersL = cv2.findChessboardCorners(grayL, (9,6),None)
    # If found, add object points, image points (after refining them)
    if ret == True:
        objpointsL.append(objp)

        cv2.cornerSubPix(grayL,cornersL,(11,11),(-1,-1),criteria)
        imgpointsL.append(cornersL)


images = glob.glob('right*.jpg')

for fname in images:
    img = cv2.imread(fname)
    grayR = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    # Find the chess board corners
    ret, cornersR = cv2.findChessboardCorners(grayR, (9,6),None)

    # If found, add object points, image points (after refining them)
    if ret == True:
        objpointsR.append(objp)

        cv2.cornerSubPix(grayR,cornersR,(11,11),(-1,-1),criteria)
        imgpointsR.append(cornersR)



retval,cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, R, T, E, F = cv2.stereoCalibrate(objpointsL, imgpointsL, imgpointsR, (320,240))

如何修正图像?在继续查找视差图之前,我还应该执行哪些其他步骤?我在某处读到,在计算视差图时,在两个帧上检测到的特征应该位于同一条水平线上。请帮帮我。任何帮助将非常感激。

最佳答案

对于 cv2.undistort(),您需要 cameraMatrix1distCoeffs1cameraMatrix2distCoeffs2 和“newCameraMatrix”

您可以使用 cv2.getOptimalNewCameraMatrix() 获取“newCameraMatrix”

所以在你脚本的其余部分粘贴这个:

# Assuming you have left01.jpg and right01.jpg that you want to rectify
lFrame = cv2.imread('left01.jpg')
rFrame = cv2.imread('right01.jpg')
w, h = lFrame.shape[:2] # both frames should be of same shape
frames = [lFrame, rFrame]

# Params from camera calibration
camMats = [cameraMatrix1, cameraMatrix2]
distCoeffs = [distCoeffs1, distCoeffs2]

camSources = [0,1]
for src in camSources:
    distCoeffs[src][0][4] = 0.0 # use only the first 2 values in distCoeffs

# The rectification process
newCams = [0,0]
roi = [0,0]
for src in camSources:
    newCams[src], roi[src] = cv2.getOptimalNewCameraMatrix(cameraMatrix = camMats[src], 
                                                           distCoeffs = distCoeffs[src], 
                                                           imageSize = (w,h), 
                                                           alpha = 0)



rectFrames = [0,0]
for src in camSources:
        rectFrames[src] = cv2.undistort(frames[src], 
                                        camMats[src], 
                                        distCoeffs[src])

# See the results
view = np.hstack([frames[0], frames[1]])    
rectView = np.hstack([rectFrames[0], rectFrames[1]])

cv2.imshow('view', view)
cv2.imshow('rectView', rectView)

# Wait indefinitely for any keypress
cv2.waitKey(0)

希望这能让你进入下一个可能正在计算“视差图”的事情;)

引用:

http://www.janeriksolem.net/2014/05/how-to-calibrate-camera-with-opencv-and.html

关于python - 立体标定Opencv Python和视差图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28222763/

相关文章:

python - 从文档字符串生成 DITA xml

python - Pandas 数据框与自身的笛卡尔积

python - 请求 - 获取内容类型/大小而不获取整个页面/内容

c++ - OpenCV 中的 Sobel 导数

python - 如何将物体随机放置在不同角度和不同位置的python上

Python:列表列表到字典

matlab - 如何使用 24 block 色卡估计和应用色彩校正矩阵?

opencv - 使用opencv检测棋盘上方的手

python - 使用OCR识别的文本对图像进行模糊处理

c# - 使用 System.Windows.Media.Imaging 合成两个位图