2013-03-06 14:41:02 +08:00
|
|
|
#!/usr/bin/env python
|
2012-11-24 02:57:22 +08:00
|
|
|
|
2015-12-15 07:33:55 +08:00
|
|
|
'''
|
|
|
|
camera calibration for distorted images with chess board samples
|
|
|
|
reads distorted images, calculates the calibration and write undistorted images
|
|
|
|
|
|
|
|
usage:
|
|
|
|
calibrate.py [--debug <output path>] [--square_size] [<image mask>]
|
|
|
|
|
|
|
|
default values:
|
|
|
|
--debug: ./output/
|
|
|
|
--square_size: 1.0
|
|
|
|
<image mask> defaults to ../data/left*.jpg
|
|
|
|
'''
|
|
|
|
|
2015-09-14 00:00:22 +08:00
|
|
|
# Python 2/3 compatibility
|
|
|
|
from __future__ import print_function
|
|
|
|
|
2012-10-17 07:18:30 +08:00
|
|
|
import numpy as np
|
|
|
|
import cv2
|
2013-03-06 14:41:02 +08:00
|
|
|
|
|
|
|
# local modules
|
2012-10-17 07:18:30 +08:00
|
|
|
from common import splitfn
|
|
|
|
|
2013-03-06 14:41:02 +08:00
|
|
|
# built-in modules
|
|
|
|
import os
|
|
|
|
|
2012-10-17 07:18:30 +08:00
|
|
|
if __name__ == '__main__':
|
2013-03-06 14:41:02 +08:00
|
|
|
import sys
|
|
|
|
import getopt
|
2012-10-17 07:18:30 +08:00
|
|
|
from glob import glob
|
|
|
|
|
2017-11-24 02:27:11 +08:00
|
|
|
args, img_mask = getopt.getopt(sys.argv[1:], '', ['debug=', 'square_size=', 'threads='])
|
2012-10-17 07:18:30 +08:00
|
|
|
args = dict(args)
|
2015-12-15 07:33:55 +08:00
|
|
|
args.setdefault('--debug', './output/')
|
|
|
|
args.setdefault('--square_size', 1.0)
|
2017-11-24 02:27:11 +08:00
|
|
|
args.setdefault('--threads', 4)
|
2015-12-15 07:33:55 +08:00
|
|
|
if not img_mask:
|
2017-11-24 02:27:11 +08:00
|
|
|
img_mask = '../data/left??.jpg' # default
|
2015-12-15 07:33:55 +08:00
|
|
|
else:
|
2013-03-06 14:41:02 +08:00
|
|
|
img_mask = img_mask[0]
|
2013-03-15 20:55:58 +08:00
|
|
|
|
2012-10-17 07:18:30 +08:00
|
|
|
img_names = glob(img_mask)
|
|
|
|
debug_dir = args.get('--debug')
|
2017-11-24 02:27:11 +08:00
|
|
|
if debug_dir and not os.path.isdir(debug_dir):
|
2015-12-15 07:33:55 +08:00
|
|
|
os.mkdir(debug_dir)
|
|
|
|
square_size = float(args.get('--square_size'))
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
pattern_size = (9, 6)
|
2015-12-15 07:33:55 +08:00
|
|
|
pattern_points = np.zeros((np.prod(pattern_size), 3), np.float32)
|
|
|
|
pattern_points[:, :2] = np.indices(pattern_size).T.reshape(-1, 2)
|
2012-10-17 07:18:30 +08:00
|
|
|
pattern_points *= square_size
|
|
|
|
|
|
|
|
obj_points = []
|
|
|
|
img_points = []
|
2017-11-24 02:27:11 +08:00
|
|
|
h, w = cv2.imread(img_names[0], 0).shape[:2] # TODO: use imquery call to retrieve results
|
|
|
|
|
|
|
|
def processImage(fn):
|
|
|
|
print('processing %s... ' % fn)
|
2012-10-17 07:18:30 +08:00
|
|
|
img = cv2.imread(fn, 0)
|
2013-03-06 14:41:02 +08:00
|
|
|
if img is None:
|
2015-12-15 07:33:55 +08:00
|
|
|
print("Failed to load", fn)
|
2017-11-24 02:27:11 +08:00
|
|
|
return None
|
2013-03-15 20:55:58 +08:00
|
|
|
|
2017-11-24 02:27:11 +08:00
|
|
|
assert w == img.shape[1] and h == img.shape[0], ("size: %d x %d ... " % (img.shape[1], img.shape[0]))
|
2012-10-17 07:18:30 +08:00
|
|
|
found, corners = cv2.findChessboardCorners(img, pattern_size)
|
|
|
|
if found:
|
2015-12-15 07:33:55 +08:00
|
|
|
term = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1)
|
2012-10-17 07:18:30 +08:00
|
|
|
cv2.cornerSubPix(img, corners, (5, 5), (-1, -1), term)
|
2015-12-15 07:33:55 +08:00
|
|
|
|
2012-10-17 07:18:30 +08:00
|
|
|
if debug_dir:
|
|
|
|
vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
|
|
|
|
cv2.drawChessboardCorners(vis, pattern_size, corners, found)
|
|
|
|
path, name, ext = splitfn(fn)
|
2017-11-24 02:27:11 +08:00
|
|
|
outfile = os.path.join(debug_dir, name + '_chess.png')
|
2015-12-15 07:33:55 +08:00
|
|
|
cv2.imwrite(outfile, vis)
|
|
|
|
|
2012-10-17 07:18:30 +08:00
|
|
|
if not found:
|
2015-09-14 00:00:22 +08:00
|
|
|
print('chessboard not found')
|
2017-11-24 02:27:11 +08:00
|
|
|
return None
|
2015-12-15 07:33:55 +08:00
|
|
|
|
2017-11-24 02:27:11 +08:00
|
|
|
print(' %s... OK' % fn)
|
|
|
|
return (corners.reshape(-1, 2), pattern_points)
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2017-11-24 02:27:11 +08:00
|
|
|
threads_num = int(args.get('--threads'))
|
|
|
|
if threads_num <= 1:
|
|
|
|
chessboards = [processImage(fn) for fn in img_names]
|
|
|
|
else:
|
|
|
|
print("Run with %d threads..." % threads_num)
|
|
|
|
from multiprocessing.dummy import Pool as ThreadPool
|
|
|
|
pool = ThreadPool(threads_num)
|
|
|
|
chessboards = pool.map(processImage, img_names)
|
|
|
|
|
|
|
|
chessboards = [x for x in chessboards if x is not None]
|
|
|
|
for (corners, pattern_points) in chessboards:
|
|
|
|
img_points.append(corners)
|
|
|
|
obj_points.append(pattern_points)
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2015-12-15 07:33:55 +08:00
|
|
|
# calculate camera distortion
|
2013-03-15 20:55:58 +08:00
|
|
|
rms, camera_matrix, dist_coefs, rvecs, tvecs = cv2.calibrateCamera(obj_points, img_points, (w, h), None, None)
|
2015-12-15 07:33:55 +08:00
|
|
|
|
2015-12-17 05:01:18 +08:00
|
|
|
print("\nRMS:", rms)
|
|
|
|
print("camera matrix:\n", camera_matrix)
|
|
|
|
print("distortion coefficients: ", dist_coefs.ravel())
|
2015-12-15 07:33:55 +08:00
|
|
|
|
|
|
|
# undistort the image with the calibration
|
|
|
|
print('')
|
2017-11-24 02:27:11 +08:00
|
|
|
for fn in img_names if debug_dir else []:
|
|
|
|
path, name, ext = splitfn(fn)
|
|
|
|
img_found = os.path.join(debug_dir, name + '_chess.png')
|
|
|
|
outfile = os.path.join(debug_dir, name + '_undistorted.png')
|
|
|
|
|
2015-12-15 07:33:55 +08:00
|
|
|
img = cv2.imread(img_found)
|
2017-11-24 02:27:11 +08:00
|
|
|
if img is None:
|
|
|
|
continue
|
2015-12-15 07:33:55 +08:00
|
|
|
|
2017-11-24 02:27:11 +08:00
|
|
|
h, w = img.shape[:2]
|
2015-12-15 07:33:55 +08:00
|
|
|
newcameramtx, roi = cv2.getOptimalNewCameraMatrix(camera_matrix, dist_coefs, (w, h), 1, (w, h))
|
|
|
|
|
|
|
|
dst = cv2.undistort(img, camera_matrix, dist_coefs, None, newcameramtx)
|
|
|
|
|
|
|
|
# crop and save the image
|
|
|
|
x, y, w, h = roi
|
|
|
|
dst = dst[y:y+h, x:x+w]
|
2017-11-24 02:27:11 +08:00
|
|
|
|
2015-12-15 07:33:55 +08:00
|
|
|
print('Undistorted image written to: %s' % outfile)
|
|
|
|
cv2.imwrite(outfile, dst)
|
|
|
|
|
2012-10-17 07:18:30 +08:00
|
|
|
cv2.destroyAllWindows()
|