簡體   English   中英

opencv立體相機校准

[英]opencv stereo camera calibration

我正在根據http://docs.opencv.org/2.4.11/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html#stereorectify給出的標准教程,使用OpenCV進行立體攝像機校准。 但是,校准后的輸出效果不佳,均方根值為78.26。 我已經嘗試過從Google可以找到的所有可用解決方案,但是它們都不起作用。

詳細實現:我使用13對圖像通過以下代碼查找對象點和圖像點。

def getCalibrateParams(leftImgPath, rightImgPath): 
# termination criteria
w = 9
h = 7
chess_size = (9, 7)
chess_size_r = (7,9)
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((np.prod(chess_size),3), np.float32)
#objp[:,:2] = np.indices(chess_size).T.reshape(-1,2)

objp = np.zeros((w*h, 3), np.float32)
objp[:,:2] = np.mgrid[0:w, 0:h].T.reshape(-1,2)
# Arrays to store object points and image points from all the images.
objpoints = [] # 3d point in real world space
leftImgpoints = [] # 2d points in image plane.
rightImgPoints = []
leftImg = glob.glob(leftImgPath)
rightImg = glob.glob(rightImgPath)

for fname in leftImg:
    img = cv2.imread(fname)
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    # Find the chess board corners
    ret, corners = cv2.findChessboardCorners(gray, (w,h), None)

    if not ret:
        raise ChessboardNotFoundError('No chessboard could be found!')
    else:
        objpoints.append(objp)
        #increase the accuracy of seeking for corners
        cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
        leftImgpoints.append(corners)

        # Draw and display the corners
        #cv2.drawChessboardCorners(img, chess_size, corners,ret)
        #cv2.imshow('img',img)
        #cv2.waitKey()
for fname in rightImg:
    img = cv2.imread(fname)
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    ret, corners = cv2.findChessboardCorners(gray, chess_size_r)

    if not ret:
        raise ChessboardNotFoundError('No chessboard could be found!')
    else:
        #increase the accuracy of seeking for corners
        cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
        rightImgPoints.append(corners)

return objpoints,leftImgpoints,rightImgPoints

之后,我嘗試使用以下代碼校准圖像對:

objectPoints,imagePoints1,imagePoints2 = getCalibrateParams(leftImgPath,rightImgPath)#使用任何圖像查找大小img = cv2.imread('/ home / wuyang / vr / img / test / test_1_01_02.jpg')灰色= cv2.cvtColor(img ,cv2.COLOR_BGR2GRAY)h,w = img.shape [:2]

#single camera calibration to fetch a more accurate camera matrix
ret1, cameraMatrix1, distCoeffs1, rvecs1, tvecs1 = cv2.calibrateCamera(objectPoints, imagePoints1, gray.shape[::-1],None, None)
ret2, cameraMatrix2, distCoeffs2, rvecs2, tvecs2 = cv2.calibrateCamera(objectPoints, imagePoints2, gray.shape[::-1],None, None)

print ret1, ret2
stereo_criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
stereo_flags = cv2.CALIB_FIX_INTRINSIC

rms, cameraMatrix1,distCoeffs1, cameraMatrix2, distCoeffs2, R, T = cv2.stereoCalibrate(objectPoints, imagePoints1, 
                                                                    imagePoints2, imageSize = (w,h), 
                                                                    cameraMatrix1 = cameraMatrix1, distCoeffs1 = distCoeffs1, 
                                                                    cameraMatrix2 = cameraMatrix2, distCoeffs2 = distCoeffs2,
                                                                    criteria = stereo_criteria, flags = stereo_flags)[:-2]

print 'stereo calibration result: ',rms 
#print cv2.CALIB_FIX_INTRINSIC 256
#print cv2.CALIB_USE_INTRINSIC_GUESS 1
#print cv2.CALIB_FIX_PRINCIPAL_POINT 4  
#print cv2.CALIB_FIX_FOCAL_LENGTH 16
#print cv2.CALIB_FIX_ASPECT_RATIO 2
#print cv2.CALIB_SAME_FOCAL_LENGTH 512
#print cv2.CALIB_RATIONAL_MODEL 16384
#print cv2.CALIB_ZERO_TANGENT_DIST 8
#print cv2.CALIB_FIX_K1 32
#print cv2.CALIB_FIX_K2 64
#print cv2.CALIB_FIX_K3 128
#print cv2.CALIB_FIX_K4 2048
#print cv2.CALIB_FIX_K5 4096
#print cv2.CALIB_FIX_K6 8192
'''
print 'rms value:', rms
print 'cameraMatrix1:\n', cameraMatrix1
print 'cameraMatrix2:\n', cameraMatrix2
print 'disCoeffs1:\n', distCoeffs1
print 'disCoeffs2:\n', distCoeffs2
print 'rotation vector:\n', R
print 'translation vector:\n', T
'''
#left camera calibration test
'''
computeReprojectionError(objectPoints, imagePoints1, rvecs1, tvecs1, cameraMatrix1, distCoeffs1)
newcameramtx1, roi1 = getCameraMatrix(img, cameraMatrix1, distCoeffs1)
undistort(img, cameraMatrix1, distCoeffs1, newcameramtx1, roi1)
'''

R1, R2, P1, P2, Q = cv2.stereoRectify(cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2,
                  (w,h), R, T, flags = 0, alpha = -1)[:-2]

# distort images

undistort_map1, rectify_map1 = cv2.initUndistortRectifyMap(cameraMatrix1, distCoeffs1, R1, P1, (w,h), cv2.CV_32FC1)
undistort_map2, rectify_map2 = cv2.initUndistortRectifyMap(cameraMatrix2, distCoeffs2, R2, P2, (w,h), cv2.CV_32FC1)

lpath = '/home/wuyang/vr/img/test/test_2_01_01.jpg'
rpath = '/home/wuyang/vr/img/test/test_2_01_02.jpg'
lImg = cv2.imread(lpath)
rImg = cv2.imread(rpath)
#undistor_output1 = cv2.undistort(test,undistort_map1, rectify_map1, None, newcameramtx)
undistor_output1 = cv2.remap(lImg, undistort_map1, rectify_map1, cv2.INTER_LINEAR)
undistor_output2 = cv2.remap(rImg, undistort_map2, rectify_map2, cv2.INTER_LINEAR)

cv2.imwrite('ss.jpg', undistor_output1)

流程很標准,但輸出效果不好。 要校准的左側圖像: http : //imgur.com/8WvzTvc校准結果: 在此處輸入鏈接描述

請幫助查看如何獲得合理的良好校准結果。 非常感謝!

我要說的是,您捕獲的照片不夠好... rms錯誤的值太高。 仔細分析您的照片對,看看它們是否模糊。 此外,還可以從不同的角度,到相機的不同距離拍攝多對照片,並且始終在圖像的邊界上具有棋盤的示例。 良好的校准應在0.5以下的誤差。 請注意,一對不好的圖像可能會大大增加您的錯誤率。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM