2017-06-11 25 views
0

http://docs.opencv.org/2.4.11/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html#stereorectifyで与えられた標準チュートリアルに従って、私はOpenCVでステレオカメラ較正に取り組んでいます。しかし、較正された出力は良好ではなく、rms値は78.26である。私はすでにGoogleから見つけられる利用可能なソリューションを試してみましたが、そのうち誰も働くことはできません。opencvステレオカメラ較正

詳細実装: 以下のコードでオブジェクトポイントとイメージポイントを見つけるために13のイメージペアを使用します。サイズ IMG =を見つけるために、任意の画像#USE

objectPoints、imagePoints1、imagePoints2 = getCalibrateParams(leftImgPath、rightImgPath) :

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 

はその後、私は以下のコードを有する画像対を較正しようcv2.imread( '/ホーム/五羊/ VR/IMG /試験/ 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 校正結果:enter link description here

は、合理的な良い校正結果を取得する方法を参照してください助けてください。どうもありがとう!

答えて

0

私は、あなたのキャプチャされた写真だけでは十分ではないと言います...それはrmsエラーの値が高すぎます。慎重にあなたの写真のペアを分析し、それらがぼやけていないかどうかを確認してください。さらに、さまざまな視点から、カメラとの距離が違っていて、常に画像の境界にチェス盤の例がある、もう少し写真のペアをキャプチャします。良好な較正は0.5未満の誤差を有するべきである。悪い画像ペアがエラーを大きく増やす可能性があることに注意してください。

+0

ありがとう@TFreitasの返信です。私はより多くの画像をキャプチャし、さらにrms値をテストします。 – wuyang

+0

また、これらのキャプチャされた画像を使用して1回のカメラ較正を行う場合、rms値が許容範囲であることを言いたいと思います。したがって、画質の問題のほかに、私は任意のパラメータを間違っている可能性がありますか? stereo_flagsのパラメータが較正効果に大きく影響することがわかりました。 – wuyang

+0

デフォルトのopencvステレオキャリブレーションの例のフラグから始まり、それらのフラグで再生します。その範囲のrmsエラーが高すぎます。あなたが望む結果が得られるまで、慎重に写真のペアを評価し、フラグで遊ぶ – TFreitas

関連する問題