Skip to content

Commit 2feb0c2

Browse files
Arthur Pastelalalek
authored andcommitted
Merge pull request opencv#9895 from art049:parrallel_calibration_py
* Adding threading in calibrate.py * samples: update calibrate.py
1 parent 107582c commit 2feb0c2

File tree

1 file changed

+36
-19
lines changed

1 file changed

+36
-19
lines changed

samples/python/calibrate.py

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,19 @@
3030
import getopt
3131
from glob import glob
3232

33-
args, img_mask = getopt.getopt(sys.argv[1:], '', ['debug=', 'square_size='])
33+
args, img_mask = getopt.getopt(sys.argv[1:], '', ['debug=', 'square_size=', 'threads='])
3434
args = dict(args)
3535
args.setdefault('--debug', './output/')
3636
args.setdefault('--square_size', 1.0)
37+
args.setdefault('--threads', 4)
3738
if not img_mask:
38-
img_mask = '../data/left*.jpg' # default
39+
img_mask = '../data/left??.jpg' # default
3940
else:
4041
img_mask = img_mask[0]
4142

4243
img_names = glob(img_mask)
4344
debug_dir = args.get('--debug')
44-
if not os.path.isdir(debug_dir):
45+
if debug_dir and not os.path.isdir(debug_dir):
4546
os.mkdir(debug_dir)
4647
square_size = float(args.get('--square_size'))
4748

@@ -52,16 +53,16 @@
5253

5354
obj_points = []
5455
img_points = []
55-
h, w = 0, 0
56-
img_names_undistort = []
57-
for fn in img_names:
58-
print('processing %s... ' % fn, end='')
56+
h, w = cv2.imread(img_names[0], 0).shape[:2] # TODO: use imquery call to retrieve results
57+
58+
def processImage(fn):
59+
print('processing %s... ' % fn)
5960
img = cv2.imread(fn, 0)
6061
if img is None:
6162
print("Failed to load", fn)
62-
continue
63+
return None
6364

64-
h, w = img.shape[:2]
65+
assert w == img.shape[1] and h == img.shape[0], ("size: %d x %d ... " % (img.shape[1], img.shape[0]))
6566
found, corners = cv2.findChessboardCorners(img, pattern_size)
6667
if found:
6768
term = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1)
@@ -71,19 +72,29 @@
7172
vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
7273
cv2.drawChessboardCorners(vis, pattern_size, corners, found)
7374
path, name, ext = splitfn(fn)
74-
outfile = debug_dir + name + '_chess.png'
75+
outfile = os.path.join(debug_dir, name + '_chess.png')
7576
cv2.imwrite(outfile, vis)
76-
if found:
77-
img_names_undistort.append(outfile)
7877

7978
if not found:
8079
print('chessboard not found')
81-
continue
80+
return None
8281

83-
img_points.append(corners.reshape(-1, 2))
84-
obj_points.append(pattern_points)
82+
print(' %s... OK' % fn)
83+
return (corners.reshape(-1, 2), pattern_points)
8584

86-
print('ok')
85+
threads_num = int(args.get('--threads'))
86+
if threads_num <= 1:
87+
chessboards = [processImage(fn) for fn in img_names]
88+
else:
89+
print("Run with %d threads..." % threads_num)
90+
from multiprocessing.dummy import Pool as ThreadPool
91+
pool = ThreadPool(threads_num)
92+
chessboards = pool.map(processImage, img_names)
93+
94+
chessboards = [x for x in chessboards if x is not None]
95+
for (corners, pattern_points) in chessboards:
96+
img_points.append(corners)
97+
obj_points.append(pattern_points)
8798

8899
# calculate camera distortion
89100
rms, camera_matrix, dist_coefs, rvecs, tvecs = cv2.calibrateCamera(obj_points, img_points, (w, h), None, None)
@@ -94,18 +105,24 @@
94105

95106
# undistort the image with the calibration
96107
print('')
97-
for img_found in img_names_undistort:
108+
for fn in img_names if debug_dir else []:
109+
path, name, ext = splitfn(fn)
110+
img_found = os.path.join(debug_dir, name + '_chess.png')
111+
outfile = os.path.join(debug_dir, name + '_undistorted.png')
112+
98113
img = cv2.imread(img_found)
114+
if img is None:
115+
continue
99116

100-
h, w = img.shape[:2]
117+
h, w = img.shape[:2]
101118
newcameramtx, roi = cv2.getOptimalNewCameraMatrix(camera_matrix, dist_coefs, (w, h), 1, (w, h))
102119

103120
dst = cv2.undistort(img, camera_matrix, dist_coefs, None, newcameramtx)
104121

105122
# crop and save the image
106123
x, y, w, h = roi
107124
dst = dst[y:y+h, x:x+w]
108-
outfile = img_found + '_undistorted.png'
125+
109126
print('Undistorted image written to: %s' % outfile)
110127
cv2.imwrite(outfile, dst)
111128

0 commit comments

Comments
 (0)