首页 文章

opencv立体相机校准

提问于
浏览
0

我正在根据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)#use any image to find size img = cv2.imread('/ home / wuyang / vr / img / test / test_1_01_02.jpg')gray = 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校准结果:enter link description here

请帮助看看如何获得合理的良好校准结果 . 非常感谢!

1 回答

  • 0

    我会说你拍摄的照片不够好......这是一个太高的rms错误值 . 仔细分析你的一对照片,看看它们是否模糊不清 . 此外,从不同的视角捕捉更多的照片对,到相机的不同距离,并且总是在图像的边界上具有棋盘的示例 . 良好的校准应该具有低于0.5的误差 . 请注意,一对错误的图像可能会极大地增加您的错误 .

相关问题