-
Notifications
You must be signed in to change notification settings - Fork 0
/
calibrate.py
251 lines (223 loc) · 10 KB
/
calibrate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# coding: UTF-8
import os
import os.path
import glob
import argparse
import cv2
import numpy as np
import json
def main():
parser = argparse.ArgumentParser(
description='Calibrate pro-cam system using chessboard and structured light projection\n'
' Place captured images as \n'
' ./ --- capture_1/ --- graycode_00.png\n'
' | |- graycode_01.png\n'
' | | .\n'
' | | .\n'
' | |- graycode_??.png\n'
' |- capture_2/ --- graycode_00.png\n'
' | |- graycode_01.png\n'
' | . | .\n'
' | . | .\n',
formatter_class=argparse.RawTextHelpFormatter
)
parser.add_argument('proj_height', type=int, help='projector pixel height')
parser.add_argument('proj_width', type=int, help='projector pixel width')
parser.add_argument('chess_vert', type=int,
help='number of cross points of chessboard in vertical direction')
parser.add_argument('chess_hori', type=int,
help='number of cross points of chessboard in horizontal direction')
parser.add_argument('chess_block_size', type=float,
help='size of blocks of chessboard (mm or cm or m)')
parser.add_argument('graycode_step', type=int,
default=1, help='step size of graycode')
parser.add_argument('-black_thr', type=int, default=40,
help='threashold to determine whether a camera pixel captures projected area or not (default : 40)')
parser.add_argument('-white_thr', type=int, default=5,
help='threashold to specify robustness of graycode decoding (default : 5)')
parser.add_argument('-camera', type=str, default=str(),help='camera internal parameter json file')
args = parser.parse_args()
proj_shape = (args.proj_height, args.proj_width)
chess_shape = (args.chess_vert, args.chess_hori)
chess_block_size = args.chess_block_size
gc_step = args.graycode_step
black_thr = args.black_thr
white_thr = args.white_thr
camera_param_file = args.camera
dirnames = sorted(glob.glob('results1')) #+ glob.glob('results2') + glob.glob('results3') + glob.glob('results4') + glob.glob('results5'))
if len(dirnames) == 0:
print('Directories \'./capture_*\' were not found')
return
print('Searching input files ...')
used_dirnames = []
gc_fname_lists = []
for dname in dirnames:
gc_fnames = sorted(glob.glob(dname + '\*.png'))
if len(gc_fnames) == 0:
continue
used_dirnames.append(dname)
gc_fname_lists.append(gc_fnames)
print(' \'' + dname + '\' was found')
camP = None
cam_dist = None
path, ext = os.path.splitext(camera_param_file)
if(ext == ".json"):
camP,cam_dist = loadCameraParam(camera_param_file)
print('load camera parameters')
print(camP)
print(cam_dist)
calibrate(used_dirnames, gc_fname_lists,
proj_shape, chess_shape, chess_block_size, gc_step, black_thr, white_thr,
camP, cam_dist)
def printNumpyWithIndent(tar, indentchar):
print(indentchar + str(tar).replace('\n', '\n' + indentchar))
def loadCameraParam(json_file):
with open(json_file, 'r') as f:
param_data = json.load(f)
P = param_data['camera']['P']
d = param_data['camera']['distortion']
return np.array(P).reshape([3,3]), np.array(d)
def calibrate(dirnames, gc_fname_lists, proj_shape, chess_shape, chess_block_size, gc_step, black_thr, white_thr, camP, camD):
objps = np.zeros((chess_shape[0]*chess_shape[1], 3), np.float32)
objps[:, :2] = chess_block_size * \
np.mgrid[0:chess_shape[0], 0:chess_shape[1]].T.reshape(-1, 2)
print('Calibrating ...')
gc_height = int((proj_shape[0]-1)/gc_step)+1
gc_width = int((proj_shape[1]-1)/gc_step)+1
graycode = cv2.structured_light_GrayCodePattern.create(
gc_width, gc_height)
graycode.setBlackThreshold(black_thr)
graycode.setWhiteThreshold(white_thr)
cam_shape = cv2.imread(gc_fname_lists[0][0], cv2.IMREAD_GRAYSCALE).shape
patch_size_half = int(np.ceil(cam_shape[1] / 180))
print(' patch size :', patch_size_half * 2 + 1)
cam_corners_list = []
cam_objps_list = []
cam_corners_list2 = []
proj_objps_list = []
proj_corners_list = []
for dname, gc_filenames in zip(dirnames, gc_fname_lists):
print(' checking \'' + dname + '\'')
print(len(gc_filenames))
print(graycode.getNumberOfPatternImages())
if len(gc_filenames) != (graycode.getNumberOfPatternImages() + 2):
print('Error : invalid number of images in \'' + dname + '\'')
return None
imgs = []
for fname in gc_filenames:
img = cv2.imread(fname, cv2.IMREAD_GRAYSCALE)
if cam_shape != img.shape:
print('Error : image size of \'' + fname + '\' is mismatch')
return None
imgs.append(img)
black_img = imgs.pop()
white_img = imgs.pop()
res, cam_corners = cv2.findChessboardCorners(white_img, chess_shape)
if not res:
print('Error : chessboard was not found in \'' +
gc_filenames[-2] + '\'')
return None
cam_objps_list.append(objps)
cam_corners_list.append(cam_corners)
proj_objps = []
proj_corners = []
cam_corners2 = []
# viz_proj_points = np.zeros(proj_shape, np.uint8)
for corner, objp in zip(cam_corners, objps):
c_x = int(round(corner[0][0]))
c_y = int(round(corner[0][1]))
src_points = []
dst_points = []
for dx in range(-patch_size_half, patch_size_half + 1):
for dy in range(-patch_size_half, patch_size_half + 1):
x = c_x + dx
y = c_y + dy
if int(white_img[y, x]) - int(black_img[y, x]) <= black_thr:
continue
err, proj_pix = graycode.getProjPixel(imgs, x, y)
if not err:
src_points.append((x, y))
dst_points.append(gc_step*np.array(proj_pix))
if len(src_points) < patch_size_half**2:
print(
' Warning : corner', c_x, c_y,
'was skiped because decoded pixels were too few (check your images and threasholds)')
continue
h_mat, inliers = cv2.findHomography(
np.array(src_points), np.array(dst_points))
point = [email protected]([corner[0][0], corner[0][1], 1]).transpose()
point_pix = point[0:2]/point[2]
proj_objps.append(objp)
proj_corners.append([point_pix])
cam_corners2.append(corner)
# viz_proj_points[int(round(point_pix[1])),
# int(round(point_pix[0]))] = 255
if len(proj_corners) < 3:
print('Error : too few corners were found in \'' +
dname + '\' (less than 3)')
return None
proj_objps_list.append(np.float32(proj_objps))
proj_corners_list.append(np.float32(proj_corners))
cam_corners_list2.append(np.float32(cam_corners2))
# cv2.imwrite('visualize_corners_projector_' +
# str(cnt) + '.png', viz_proj_points)
# cnt += 1
print('Initial solution of camera\'s intrinsic parameters')
cam_rvecs = []
cam_tvecs = []
if(camP is None):
ret, cam_int, cam_dist, cam_rvecs, cam_tvecs = cv2.calibrateCamera(
cam_objps_list, cam_corners_list, cam_shape, None, None, None, None)
print(' RMS :', ret)
else:
for objp, corners in zip(cam_objps_list, cam_corners_list):
ret, cam_rvec, cam_tvec = cv2.solvePnP(objp, corners, camP, camD)
cam_rvecs.append(cam_rvec)
cam_tvecs.append(cam_tvec)
print(' RMS :', ret)
cam_int = camP
cam_dist = camD
print(' Intrinsic parameters :')
printNumpyWithIndent(cam_int, ' ')
print(' Distortion parameters :')
printNumpyWithIndent(cam_dist, ' ')
print()
print('Initial solution of projector\'s parameters')
ret, proj_int, proj_dist, proj_rvecs, proj_tvecs = cv2.calibrateCamera(
proj_objps_list, proj_corners_list, proj_shape, None, None, None, None)
print(' RMS :', ret)
print(' Intrinsic parameters :')
printNumpyWithIndent(proj_int, ' ')
print(' Distortion parameters :')
printNumpyWithIndent(proj_dist, ' ')
print()
print('=== Result ===')
ret, cam_int, cam_dist, proj_int, proj_dist, cam_proj_rmat, cam_proj_tvec, E, F = cv2.stereoCalibrate(
proj_objps_list, cam_corners_list2, proj_corners_list, cam_int, cam_dist, proj_int, proj_dist, None)
print(' RMS :', ret)
print(' Camera intrinsic parameters :')
printNumpyWithIndent(cam_int, ' ')
print(' Camera distortion parameters :')
printNumpyWithIndent(cam_dist, ' ')
print(' Projector intrinsic parameters :')
printNumpyWithIndent(proj_int, ' ')
print(' Projector distortion parameters :')
printNumpyWithIndent(proj_dist, ' ')
print(' Rotation matrix / translation vector from camera to projector')
print(' (they translate points from camera coord to projector coord) :')
printNumpyWithIndent(cam_proj_rmat, ' ')
printNumpyWithIndent(cam_proj_tvec, ' ')
print()
fs = cv2.FileStorage('calibration_result.xml', cv2.FILE_STORAGE_WRITE)
fs.write('img_shape', cam_shape)
fs.write('rms', ret)
fs.write('cam_int', cam_int)
fs.write('cam_dist', cam_dist)
fs.write('proj_int', proj_int)
fs.write('proj_dist', proj_dist)
fs.write('rotation', cam_proj_rmat)
fs.write('translation', cam_proj_tvec)
fs.release()
if __name__ == '__main__':
main()
#python calibrate.py 360 360 9 7 75 1 -black_thr 40 -white_thr 5