mirror of
https://github.com/opencv/opencv.git
synced 2025-08-01 02:18:01 +08:00
Merge pull request #9895 from art049:parrallel_calibration_py
* Adding threading in calibrate.py * samples: update calibrate.py
This commit is contained in:
parent
107582c767
commit
2feb0c2f61
@ -30,18 +30,19 @@ if __name__ == '__main__':
|
|||||||
import getopt
|
import getopt
|
||||||
from glob import glob
|
from glob import glob
|
||||||
|
|
||||||
args, img_mask = getopt.getopt(sys.argv[1:], '', ['debug=', 'square_size='])
|
args, img_mask = getopt.getopt(sys.argv[1:], '', ['debug=', 'square_size=', 'threads='])
|
||||||
args = dict(args)
|
args = dict(args)
|
||||||
args.setdefault('--debug', './output/')
|
args.setdefault('--debug', './output/')
|
||||||
args.setdefault('--square_size', 1.0)
|
args.setdefault('--square_size', 1.0)
|
||||||
|
args.setdefault('--threads', 4)
|
||||||
if not img_mask:
|
if not img_mask:
|
||||||
img_mask = '../data/left*.jpg' # default
|
img_mask = '../data/left??.jpg' # default
|
||||||
else:
|
else:
|
||||||
img_mask = img_mask[0]
|
img_mask = img_mask[0]
|
||||||
|
|
||||||
img_names = glob(img_mask)
|
img_names = glob(img_mask)
|
||||||
debug_dir = args.get('--debug')
|
debug_dir = args.get('--debug')
|
||||||
if not os.path.isdir(debug_dir):
|
if debug_dir and not os.path.isdir(debug_dir):
|
||||||
os.mkdir(debug_dir)
|
os.mkdir(debug_dir)
|
||||||
square_size = float(args.get('--square_size'))
|
square_size = float(args.get('--square_size'))
|
||||||
|
|
||||||
@ -52,16 +53,16 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
obj_points = []
|
obj_points = []
|
||||||
img_points = []
|
img_points = []
|
||||||
h, w = 0, 0
|
h, w = cv2.imread(img_names[0], 0).shape[:2] # TODO: use imquery call to retrieve results
|
||||||
img_names_undistort = []
|
|
||||||
for fn in img_names:
|
def processImage(fn):
|
||||||
print('processing %s... ' % fn, end='')
|
print('processing %s... ' % fn)
|
||||||
img = cv2.imread(fn, 0)
|
img = cv2.imread(fn, 0)
|
||||||
if img is None:
|
if img is None:
|
||||||
print("Failed to load", fn)
|
print("Failed to load", fn)
|
||||||
continue
|
return None
|
||||||
|
|
||||||
h, w = img.shape[:2]
|
assert w == img.shape[1] and h == img.shape[0], ("size: %d x %d ... " % (img.shape[1], img.shape[0]))
|
||||||
found, corners = cv2.findChessboardCorners(img, pattern_size)
|
found, corners = cv2.findChessboardCorners(img, pattern_size)
|
||||||
if found:
|
if found:
|
||||||
term = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1)
|
term = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1)
|
||||||
@ -71,20 +72,30 @@ if __name__ == '__main__':
|
|||||||
vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
|
vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
|
||||||
cv2.drawChessboardCorners(vis, pattern_size, corners, found)
|
cv2.drawChessboardCorners(vis, pattern_size, corners, found)
|
||||||
path, name, ext = splitfn(fn)
|
path, name, ext = splitfn(fn)
|
||||||
outfile = debug_dir + name + '_chess.png'
|
outfile = os.path.join(debug_dir, name + '_chess.png')
|
||||||
cv2.imwrite(outfile, vis)
|
cv2.imwrite(outfile, vis)
|
||||||
if found:
|
|
||||||
img_names_undistort.append(outfile)
|
|
||||||
|
|
||||||
if not found:
|
if not found:
|
||||||
print('chessboard not found')
|
print('chessboard not found')
|
||||||
continue
|
return None
|
||||||
|
|
||||||
img_points.append(corners.reshape(-1, 2))
|
print(' %s... OK' % fn)
|
||||||
|
return (corners.reshape(-1, 2), pattern_points)
|
||||||
|
|
||||||
|
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)
|
obj_points.append(pattern_points)
|
||||||
|
|
||||||
print('ok')
|
|
||||||
|
|
||||||
# calculate camera distortion
|
# calculate camera distortion
|
||||||
rms, camera_matrix, dist_coefs, rvecs, tvecs = cv2.calibrateCamera(obj_points, img_points, (w, h), None, None)
|
rms, camera_matrix, dist_coefs, rvecs, tvecs = cv2.calibrateCamera(obj_points, img_points, (w, h), None, None)
|
||||||
|
|
||||||
@ -94,10 +105,16 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
# undistort the image with the calibration
|
# undistort the image with the calibration
|
||||||
print('')
|
print('')
|
||||||
for img_found in img_names_undistort:
|
for fn in img_names if debug_dir else []:
|
||||||
img = cv2.imread(img_found)
|
path, name, ext = splitfn(fn)
|
||||||
|
img_found = os.path.join(debug_dir, name + '_chess.png')
|
||||||
|
outfile = os.path.join(debug_dir, name + '_undistorted.png')
|
||||||
|
|
||||||
h, w = img.shape[:2]
|
img = cv2.imread(img_found)
|
||||||
|
if img is None:
|
||||||
|
continue
|
||||||
|
|
||||||
|
h, w = img.shape[:2]
|
||||||
newcameramtx, roi = cv2.getOptimalNewCameraMatrix(camera_matrix, dist_coefs, (w, h), 1, (w, h))
|
newcameramtx, roi = cv2.getOptimalNewCameraMatrix(camera_matrix, dist_coefs, (w, h), 1, (w, h))
|
||||||
|
|
||||||
dst = cv2.undistort(img, camera_matrix, dist_coefs, None, newcameramtx)
|
dst = cv2.undistort(img, camera_matrix, dist_coefs, None, newcameramtx)
|
||||||
@ -105,7 +122,7 @@ if __name__ == '__main__':
|
|||||||
# crop and save the image
|
# crop and save the image
|
||||||
x, y, w, h = roi
|
x, y, w, h = roi
|
||||||
dst = dst[y:y+h, x:x+w]
|
dst = dst[y:y+h, x:x+w]
|
||||||
outfile = img_found + '_undistorted.png'
|
|
||||||
print('Undistorted image written to: %s' % outfile)
|
print('Undistorted image written to: %s' % outfile)
|
||||||
cv2.imwrite(outfile, dst)
|
cv2.imwrite(outfile, dst)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user