Python - OpenCV stereocalibration error

asked 2016-01-25 11:14:18 -0600

Bilou563 gravatar image

I m trying to use the stereo part of openCV (3.0.0-dev version). I can do all the calibration for each cameras so i got the 2 camera matrix and distorsion vectors. But when i try to apply the stereocalibrate function, the calculation is done but the RMS is very high : 40! and so the obtained results seem wrong... But i dont know how the problem is...

import numpy as np
import cv2
import matplotlib.pyplot as plt
import os
import sys, getopt
from glob import glob
import Image

a=int(9) #chessboard windows
b=int(5)


# CAM1 calibration
If __name__ == '__main__':

args, img_mask = getopt.getopt(sys.argv[1:], '', ['save=', 'debug=', 'square_size='])
args = dict(args)
img_mask = '/home/essais-2015-3/Bureau/test_22_01/calib/gauche/*.tif'
img_names = sorted(glob(img_mask))
debug_dir = args.get('--debug')
square_size = float(args.get('--square_size', 1))

pattern_size = (a,b)
pattern_points = np.zeros( (np.prod(pattern_size), 3), np.float32 )
pattern_points[:,:2] = np.indices(pattern_size).T.reshape(-1, 2)
pattern_points *= square_size

obj_points = []

img_points1 = []
h, w = 0, 0
for fn in img_names:
    print 'processing %s...' % fn,
    img = cv2.imread(fn, 0)
    h, w = img.shape[:2]
    found, corners = cv2.findChessboardCorners(img, pattern_size)
    if found:
        term = ( cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1 )
        cv2.cornerSubPix(img, corners, (5, 5), (-1, -1), term)
    if debug_dir:
        vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
        cv2.drawChessboardCorners(vis, pattern_size, corners, found)
        path, name, ext = splitfn(fn)
        cv2.imwrite('%s/%s_chess.bmp' % (debug_dir, name), vis)
    if not found:
        print 'chessboard not found'
        continue
    img_points1.append(corners.reshape(-1, 2))
    obj_points.append(pattern_points)
print 'ok'
rms, C1, dist1, rvecs1, tvecs1 = cv2.calibrateCamera(obj_points, img_points1, (w, h), None, None)
print "RMS:", rms
print "camera matrix:\n", C1
print "distortion coefficients: ", dist1.ravel()
cv2.destroyAllWindows()


# CAM2 calibration
if __name__ == '__main__':

args, img_mask = getopt.getopt(sys.argv[1:], '', ['save=', 'debug=', 'square_size='])
args = dict(args)
img_mask = '/home/essais-2015-3/Bureau/test_22_01/calib/droite/*.tif'
img_names = sorted(glob(img_mask))
debug_dir = args.get('--debug')
square_size = float(args.get('--square_size', 1))

pattern_size = (a,b)
pattern_points = np.zeros( (np.prod(pattern_size), 3), np.float32 )
pattern_points[:,:2] = np.indices(pattern_size).T.reshape(-1, 2)
pattern_points *= square_size

obj_points = []

img_points2 = []
h, w = 0, 0
for fn in img_names:
    print 'processing %s...' % fn,
    img = cv2.imread(fn, 0)
    h, w = img.shape[:2]
    found, corners = cv2.findChessboardCorners(img, pattern_size)
    if found:
        term = ( cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1 )
        cv2.cornerSubPix(img, corners, (5, 5), (-1, -1), term)
    if debug_dir:
        vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
        cv2.drawChessboardCorners(vis, pattern_size, corners, found)
        path, name, ext = splitfn(fn)
        cv2.imwrite('%s/%s_chess.bmp' % (debug_dir, name), vis)
    if not found:
        print 'chessboard not found'
        continue
img_points2.append(corners.reshape(-1, 2))
obj_points.append(pattern_points)

print 'ok'
rms, C2, dist2, rvecs2, tvecs2= cv2.calibrateCamera(obj_points, img_points2, (w, h), None, None)
print "RMS:", rms
print "camera matrix:\n", C2
print "distortion coefficients: ", dist2.ravel()
cv2.destroyAllWindows()


# stereo calibration
rms, C1, dist1, C2, dist2, R, T, E,F = cv2.stereoCalibrate(obj_points, img_points1, img_points2, C1, dist1, C2, dist2,     (w,h))
edit retag flag offensive close merge delete