|
30 | 30 | import getopt
|
31 | 31 | from glob import glob
|
32 | 32 |
|
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=']) |
34 | 34 | args = dict(args)
|
35 | 35 | args.setdefault('--debug', './output/')
|
36 | 36 | args.setdefault('--square_size', 1.0)
|
| 37 | + args.setdefault('--threads', 4) |
37 | 38 | if not img_mask:
|
38 |
| - img_mask = '../data/left*.jpg' # default |
| 39 | + img_mask = '../data/left??.jpg' # default |
39 | 40 | else:
|
40 | 41 | img_mask = img_mask[0]
|
41 | 42 |
|
42 | 43 | img_names = glob(img_mask)
|
43 | 44 | debug_dir = args.get('--debug')
|
44 |
| - if not os.path.isdir(debug_dir): |
| 45 | + if debug_dir and not os.path.isdir(debug_dir): |
45 | 46 | os.mkdir(debug_dir)
|
46 | 47 | square_size = float(args.get('--square_size'))
|
47 | 48 |
|
|
52 | 53 |
|
53 | 54 | obj_points = []
|
54 | 55 | 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) |
59 | 60 | img = cv2.imread(fn, 0)
|
60 | 61 | if img is None:
|
61 | 62 | print("Failed to load", fn)
|
62 |
| - continue |
| 63 | + return None |
63 | 64 |
|
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])) |
65 | 66 | found, corners = cv2.findChessboardCorners(img, pattern_size)
|
66 | 67 | if found:
|
67 | 68 | term = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1)
|
|
71 | 72 | vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
|
72 | 73 | cv2.drawChessboardCorners(vis, pattern_size, corners, found)
|
73 | 74 | path, name, ext = splitfn(fn)
|
74 |
| - outfile = debug_dir + name + '_chess.png' |
| 75 | + outfile = os.path.join(debug_dir, name + '_chess.png') |
75 | 76 | cv2.imwrite(outfile, vis)
|
76 |
| - if found: |
77 |
| - img_names_undistort.append(outfile) |
78 | 77 |
|
79 | 78 | if not found:
|
80 | 79 | print('chessboard not found')
|
81 |
| - continue |
| 80 | + return None |
82 | 81 |
|
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) |
85 | 84 |
|
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) |
87 | 98 |
|
88 | 99 | # calculate camera distortion
|
89 | 100 | rms, camera_matrix, dist_coefs, rvecs, tvecs = cv2.calibrateCamera(obj_points, img_points, (w, h), None, None)
|
|
94 | 105 |
|
95 | 106 | # undistort the image with the calibration
|
96 | 107 | 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 | + |
98 | 113 | img = cv2.imread(img_found)
|
| 114 | + if img is None: |
| 115 | + continue |
99 | 116 |
|
100 |
| - h, w = img.shape[:2] |
| 117 | + h, w = img.shape[:2] |
101 | 118 | newcameramtx, roi = cv2.getOptimalNewCameraMatrix(camera_matrix, dist_coefs, (w, h), 1, (w, h))
|
102 | 119 |
|
103 | 120 | dst = cv2.undistort(img, camera_matrix, dist_coefs, None, newcameramtx)
|
104 | 121 |
|
105 | 122 | # crop and save the image
|
106 | 123 | x, y, w, h = roi
|
107 | 124 | dst = dst[y:y+h, x:x+w]
|
108 |
| - outfile = img_found + '_undistorted.png' |
| 125 | + |
109 | 126 | print('Undistorted image written to: %s' % outfile)
|
110 | 127 | cv2.imwrite(outfile, dst)
|
111 | 128 |
|
|
0 commit comments