2011-06-13 23:45:18 +02:00
|
|
|
import numpy as np
|
2011-07-16 11:35:56 +02:00
|
|
|
import cv2
|
2011-06-13 23:45:18 +02:00
|
|
|
import os
|
2011-06-15 15:58:40 +02:00
|
|
|
from common import splitfn
|
2011-06-13 23:45:18 +02:00
|
|
|
|
|
|
|
USAGE = '''
|
2011-06-19 13:33:15 +02:00
|
|
|
USAGE: calib.py [--save <filename>] [--debug <output path>] [--square_size] [<image mask>]
|
2011-06-13 23:45:18 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys, getopt
|
|
|
|
from glob import glob
|
|
|
|
|
2011-06-19 13:33:15 +02:00
|
|
|
args, img_mask = getopt.getopt(sys.argv[1:], '', ['save=', 'debug=', 'square_size='])
|
2011-06-13 23:45:18 +02:00
|
|
|
args = dict(args)
|
|
|
|
try: img_mask = img_mask[0]
|
|
|
|
except: img_mask = '../cpp/left*.jpg'
|
|
|
|
img_names = glob(img_mask)
|
2011-06-14 17:02:57 +02:00
|
|
|
debug_dir = args.get('--debug')
|
2011-06-19 13:33:15 +02:00
|
|
|
square_size = float(args.get('--square_size', 1.0))
|
2011-06-13 23:45:18 +02:00
|
|
|
|
|
|
|
pattern_size = (9, 6)
|
|
|
|
pattern_points = np.zeros( (np.prod(pattern_size), 3), np.float32 )
|
|
|
|
pattern_points[:,:2] = np.indices(pattern_size).T.reshape(-1, 2)
|
2011-06-19 13:33:15 +02:00
|
|
|
pattern_points *= square_size
|
2011-06-13 23:45:18 +02:00
|
|
|
|
|
|
|
obj_points = []
|
|
|
|
img_points = []
|
|
|
|
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 )
|
2011-06-15 15:58:40 +02:00
|
|
|
cv2.cornerSubPix(img, corners, (5, 5), (-1, -1), term)
|
2011-06-13 23:45:18 +02:00
|
|
|
if debug_dir:
|
2011-07-18 15:05:34 +02:00
|
|
|
vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
|
2011-06-13 23:45:18 +02:00
|
|
|
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'
|
2011-06-14 14:14:02 +02:00
|
|
|
continue
|
2011-06-13 23:45:18 +02:00
|
|
|
img_points.append(corners.reshape(-1, 2))
|
|
|
|
obj_points.append(pattern_points)
|
|
|
|
|
|
|
|
print 'ok'
|
|
|
|
|
2011-07-16 11:35:56 +02:00
|
|
|
rms, camera_matrix, dist_coefs, rvecs, tvecs = cv2.calibrateCamera(obj_points, img_points, (w, h))
|
2011-06-17 23:21:01 +02:00
|
|
|
print "RMS:", rms
|
2011-06-19 13:33:15 +02:00
|
|
|
print "camera matrix:\n", camera_matrix
|
|
|
|
print "distortion coefficients: ", dist_coefs.ravel()
|
2011-06-17 23:21:01 +02:00
|
|
|
|