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:
|
2023-05-17 23:30:30 +08:00
|
|
|
calibrate.py [--debug <output path>] [-w <width>] [-h <height>] [-t <pattern type>] [--square_size=<square size>]
|
|
|
|
[--marker_size=<aruco marker size>] [--aruco_dict=<aruco dictionary name>] [<image mask>]
|
|
|
|
|
|
|
|
usage example:
|
|
|
|
calibrate.py -w 4 -h 6 -t chessboard --square_size=50 ../data/left*.jpg
|
2015-12-15 07:33:55 +08:00
|
|
|
|
|
|
|
default values:
|
|
|
|
--debug: ./output/
|
2023-05-17 23:30:30 +08:00
|
|
|
-w: 4
|
|
|
|
-h: 6
|
|
|
|
-t: chessboard
|
2023-06-24 03:04:02 +08:00
|
|
|
--square_size: 10
|
|
|
|
--marker_size: 5
|
2023-05-17 23:30:30 +08:00
|
|
|
--aruco_dict: DICT_4X4_50
|
|
|
|
--threads: 4
|
2015-12-15 07:33:55 +08:00
|
|
|
<image mask> defaults to ../data/left*.jpg
|
2023-06-24 03:04:02 +08:00
|
|
|
|
|
|
|
NOTE: Chessboard size is defined in inner corners. Charuco board size is defined in units.
|
2015-12-15 07:33:55 +08:00
|
|
|
'''
|
|
|
|
|
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
|
2017-12-11 17:55:03 +08:00
|
|
|
import cv2 as cv
|
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
|
|
|
|
|
2019-03-20 02:03:58 +08:00
|
|
|
def 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
|
|
|
|
|
2023-05-17 23:30:30 +08:00
|
|
|
args, img_names = getopt.getopt(sys.argv[1:], 'w:h:t:', ['debug=','square_size=', 'marker_size=',
|
|
|
|
'aruco_dict=', 'threads=', ])
|
2012-10-17 07:18:30 +08:00
|
|
|
args = dict(args)
|
2015-12-15 07:33:55 +08:00
|
|
|
args.setdefault('--debug', './output/')
|
2023-05-17 23:30:30 +08:00
|
|
|
args.setdefault('-w', 4)
|
|
|
|
args.setdefault('-h', 6)
|
|
|
|
args.setdefault('-t', 'chessboard')
|
|
|
|
args.setdefault('--square_size', 10)
|
|
|
|
args.setdefault('--marker_size', 5)
|
|
|
|
args.setdefault('--aruco_dict', 'DICT_4X4_50')
|
2017-11-24 02:27:11 +08:00
|
|
|
args.setdefault('--threads', 4)
|
2023-05-17 23:30:30 +08:00
|
|
|
|
|
|
|
if not img_names:
|
2017-11-24 02:27:11 +08:00
|
|
|
img_mask = '../data/left??.jpg' # default
|
2023-05-17 23:30:30 +08:00
|
|
|
img_names = glob(img_mask)
|
2013-03-15 20:55:58 +08:00
|
|
|
|
2012-10-17 07:18:30 +08:00
|
|
|
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)
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2023-05-17 23:30:30 +08:00
|
|
|
height = int(args.get('-h'))
|
|
|
|
width = int(args.get('-w'))
|
|
|
|
pattern_type = str(args.get('-t'))
|
|
|
|
square_size = float(args.get('--square_size'))
|
|
|
|
marker_size = float(args.get('--marker_size'))
|
|
|
|
aruco_dict_name = str(args.get('--aruco_dict'))
|
|
|
|
|
2023-06-24 03:04:02 +08:00
|
|
|
pattern_size = (width, height)
|
2023-05-17 23:30:30 +08:00
|
|
|
if pattern_type == 'chessboard':
|
|
|
|
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
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
obj_points = []
|
|
|
|
img_points = []
|
2018-11-14 23:56:21 +08:00
|
|
|
h, w = cv.imread(img_names[0], cv.IMREAD_GRAYSCALE).shape[:2] # TODO: use imquery call to retrieve results
|
2017-11-24 02:27:11 +08:00
|
|
|
|
2023-05-17 23:30:30 +08:00
|
|
|
aruco_dicts = {
|
2023-06-24 03:04:02 +08:00
|
|
|
'DICT_4X4_50': cv.aruco.DICT_4X4_50,
|
|
|
|
'DICT_4X4_100': cv.aruco.DICT_4X4_100,
|
|
|
|
'DICT_4X4_250': cv.aruco.DICT_4X4_250,
|
|
|
|
'DICT_4X4_1000': cv.aruco.DICT_4X4_1000,
|
|
|
|
'DICT_5X5_50': cv.aruco.DICT_5X5_50,
|
|
|
|
'DICT_5X5_100': cv.aruco.DICT_5X5_100,
|
|
|
|
'DICT_5X5_250': cv.aruco.DICT_5X5_250,
|
|
|
|
'DICT_5X5_1000': cv.aruco.DICT_5X5_1000,
|
|
|
|
'DICT_6X6_50': cv.aruco.DICT_6X6_50,
|
|
|
|
'DICT_6X6_100': cv.aruco.DICT_6X6_100,
|
|
|
|
'DICT_6X6_250': cv.aruco.DICT_6X6_250,
|
|
|
|
'DICT_6X6_1000': cv.aruco.DICT_6X6_1000,
|
|
|
|
'DICT_7X7_50': cv.aruco.DICT_7X7_50,
|
|
|
|
'DICT_7X7_100': cv.aruco.DICT_7X7_100,
|
|
|
|
'DICT_7X7_250': cv.aruco.DICT_7X7_250,
|
|
|
|
'DICT_7X7_1000': cv.aruco.DICT_7X7_1000,
|
|
|
|
'DICT_ARUCO_ORIGINAL': cv.aruco.DICT_ARUCO_ORIGINAL,
|
|
|
|
'DICT_APRILTAG_16h5': cv.aruco.DICT_APRILTAG_16h5,
|
|
|
|
'DICT_APRILTAG_25h9': cv.aruco.DICT_APRILTAG_25h9,
|
|
|
|
'DICT_APRILTAG_36h10': cv.aruco.DICT_APRILTAG_36h10,
|
|
|
|
'DICT_APRILTAG_36h11': cv.aruco.DICT_APRILTAG_36h11
|
2023-05-17 23:30:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aruco_dict_name not in set(aruco_dicts.keys())):
|
|
|
|
print("unknown aruco dictionary name")
|
|
|
|
return None
|
|
|
|
aruco_dict = cv.aruco.getPredefinedDictionary(aruco_dicts[aruco_dict_name])
|
|
|
|
board = cv.aruco.CharucoBoard(pattern_size, square_size, marker_size, aruco_dict)
|
|
|
|
charuco_detector = cv.aruco.CharucoDetector(board)
|
|
|
|
|
2017-11-24 02:27:11 +08:00
|
|
|
def processImage(fn):
|
|
|
|
print('processing %s... ' % fn)
|
2023-01-09 17:55:31 +08:00
|
|
|
img = cv.imread(fn, cv.IMREAD_GRAYSCALE)
|
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]))
|
2023-05-17 23:30:30 +08:00
|
|
|
found = False
|
|
|
|
corners = 0
|
|
|
|
if pattern_type == 'chessboard':
|
|
|
|
found, corners = cv.findChessboardCorners(img, pattern_size)
|
|
|
|
if found:
|
|
|
|
term = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_COUNT, 30, 0.1)
|
|
|
|
cv.cornerSubPix(img, corners, (5, 5), (-1, -1), term)
|
2023-06-24 03:04:02 +08:00
|
|
|
frame_img_points = corners.reshape(-1, 2)
|
|
|
|
frame_obj_points = pattern_points
|
2023-05-17 23:30:30 +08:00
|
|
|
elif pattern_type == 'charucoboard':
|
2023-06-24 03:04:02 +08:00
|
|
|
corners, charucoIds, _, _ = charuco_detector.detectBoard(img)
|
|
|
|
if (len(corners) > 0):
|
|
|
|
frame_obj_points, frame_img_points = board.matchImagePoints(corners, charucoIds)
|
2023-05-17 23:30:30 +08:00
|
|
|
found = True
|
2023-06-24 03:04:02 +08:00
|
|
|
else:
|
|
|
|
found = False
|
2023-05-17 23:30:30 +08:00
|
|
|
else:
|
|
|
|
print("unknown pattern type", pattern_type)
|
|
|
|
return None
|
2015-12-15 07:33:55 +08:00
|
|
|
|
2012-10-17 07:18:30 +08:00
|
|
|
if debug_dir:
|
2017-12-11 17:55:03 +08:00
|
|
|
vis = cv.cvtColor(img, cv.COLOR_GRAY2BGR)
|
2023-06-24 03:04:02 +08:00
|
|
|
if pattern_type == 'chessboard':
|
|
|
|
cv.drawChessboardCorners(vis, pattern_size, corners, found)
|
|
|
|
elif pattern_type == 'charucoboard':
|
|
|
|
cv.aruco.drawDetectedCornersCharuco(vis, corners, charucoIds=charucoIds)
|
2018-06-04 22:58:06 +08:00
|
|
|
_path, name, _ext = splitfn(fn)
|
2023-06-24 03:04:02 +08:00
|
|
|
outfile = os.path.join(debug_dir, name + '_board.png')
|
2017-12-11 17:55:03 +08:00
|
|
|
cv.imwrite(outfile, vis)
|
2015-12-15 07:33:55 +08:00
|
|
|
|
2012-10-17 07:18:30 +08:00
|
|
|
if not found:
|
2023-05-17 23:30:30 +08:00
|
|
|
print('pattern 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)
|
2023-06-24 03:04:02 +08:00
|
|
|
return (frame_img_points, frame_obj_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
|
2019-10-16 23:49:33 +08:00
|
|
|
rms, camera_matrix, dist_coefs, _rvecs, _tvecs = cv.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 []:
|
2019-10-16 23:49:33 +08:00
|
|
|
_path, name, _ext = splitfn(fn)
|
2023-06-24 03:04:02 +08:00
|
|
|
img_found = os.path.join(debug_dir, name + '_board.png')
|
2017-11-24 02:27:11 +08:00
|
|
|
outfile = os.path.join(debug_dir, name + '_undistorted.png')
|
|
|
|
|
2017-12-11 17:55:03 +08:00
|
|
|
img = cv.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]
|
2017-12-11 17:55:03 +08:00
|
|
|
newcameramtx, roi = cv.getOptimalNewCameraMatrix(camera_matrix, dist_coefs, (w, h), 1, (w, h))
|
2015-12-15 07:33:55 +08:00
|
|
|
|
2017-12-11 17:55:03 +08:00
|
|
|
dst = cv.undistort(img, camera_matrix, dist_coefs, None, newcameramtx)
|
2015-12-15 07:33:55 +08:00
|
|
|
|
|
|
|
# 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)
|
2017-12-11 17:55:03 +08:00
|
|
|
cv.imwrite(outfile, dst)
|
2015-12-15 07:33:55 +08:00
|
|
|
|
2019-03-20 02:03:58 +08:00
|
|
|
print('Done')
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
print(__doc__)
|
|
|
|
main()
|
2017-12-11 17:55:03 +08:00
|
|
|
cv.destroyAllWindows()
|