-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
316 lines (260 loc) · 12.8 KB
/
main.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
''' CONFIDENTIAL
Copyright (c) 2021 Eugeniu Vezeteu,
Department of Remote Sensing and Photogrammetry,
Finnish Geospatial Research Institute (FGI), National Land Survey of Finland (NLS)
PERMISSION IS HEREBY LIMITED TO FGI'S INTERNAL USE ONLY. THE CODE
MAY BE RE-LICENSED, SHARED, OR TAKEN INTO OTHER USE ONLY WITH
A WRITTEN CONSENT FROM THE HEAD OF THE DEPARTMENT.
The software is provided "as is", without warranty of any kind, express or
implied, including but not limited to the warranties of merchantability,
fitness for a particular purpose and noninfringement. In no event shall the
authors or copyright holders be liable for any claim, damages or other
liability, whether in an action of contract, tort or otherwise, arising from,
out of or in connection with the software or the use or other dealings in the
software.
'''
import glob
try:
from CameraCalibration.scripts.Charuco_Chess_combine import MonoCombinedCalibration, CombinedCalibration
from CameraCalibration.scripts.MonoCharuco import MonoCharuco_Calibrator
from CameraCalibration.scripts.StereoCharuco import StereoCharuco_Calibrator
from CameraCalibration.scripts.MonoChess import MonoChess_Calibrator
from CameraCalibration.scripts.StereoChess import StereoChess_Calibrator
from utils import *
except:
from MonoChess import MonoChess_Calibrator
from MonoCharuco import MonoCharuco_Calibrator
def Chess_MonoCalibration():
def calibrate_(leftCam=False, outside=False):
calibrator = MonoChess_Calibrator(pattern_type="chessboard", pattern_rows=10,
pattern_columns=7, distance_in_world_units=.1,# 10, # square is 10 cm
figsize=(14, 10))
if leftCam:
if outside:
images = glob.glob(
'/home/eugeniu/Desktop/my_data/CameraCalibration/data/car_cam_data/chess/outside/Left/*.png')
calibrator.name = 'chess_outside_left'
else:
images = glob.glob(
'/home/eugeniu/Desktop/my_data/CameraCalibration/data/car_cam_data/chess/inside/Left/*.png')
calibrator.name = 'chess_inside_left'
else:
if outside:
images = glob.glob(
'/home/eugeniu/Desktop/my_data/CameraCalibration/data/car_cam_data/chess/outside/Right/*.png')
calibrator.name = 'chess_outside_right'
else:
images = glob.glob(
'/home/eugeniu/Desktop/my_data/CameraCalibration/data/car_cam_data/chess/inside/Right/*.png')
calibrator.name = 'chess_inside_right'
calibrator.doStuff(images=images, project=True)
return calibrator
# ---------Work with right camera ----------------------------------------------
# calibrate right camera outside
print('=== Calibrate right camera outside ===')
calibrator = calibrate_(leftCam=False, outside=True)
calibrator.visualize_calibration_boards(cam_width=.2, cam_height=.1, scale_focal=1)
# calibrate right camera inside
print('=== Calibrate right camera inside ===')
# calibrator = calibrate_(leftCam=False, outside=False)
# calibrator.visualize_calibration_boards()
# ideal_calibration_right = load_obj(name='chess_outside_right')
# calibrator.adjustCalibration(images=calibrator.images, K=ideal_calibration_right['K'],
# D=ideal_calibration_right['D'])
# ---------Work with left camera -----------------------------------------------
# calibrate left camera outside
print('=== Calibrate left camera outside ===')
#calibrator = calibrate_(leftCam=True, outside=True)
# calibrate left camera inside
print('=== Calibrate left camera inside ===')
# calibrator = calibrate_(leftCam=True, outside=False)
# ideal_calibration_left = load_obj(name='chess_outside_left')
# calibrator.adjustCalibration(images=calibrator.images, K=ideal_calibration_left['K'],
# D=ideal_calibration_left['D'])
print('Mono camera calibration done')
def Chess_StereoCalibration():
def stereo_calibrate(outside=True):
if outside:
images = '/home/eugeniu/Desktop/my_data/CameraCalibration/data/car_cam_data/chess/outside/Stereo'
name = 'outside'
else:
images = '/home/eugeniu/Desktop/my_data/CameraCalibration/data/car_cam_data/chess/inside/Stereo'
name = 'inside'
calibrator = StereoChess_Calibrator(images)
calibrator.name = name
calibrator.readMonoData()
calibrator.read_images(test=False)
#calibrator.calibrationReport()
# calibrator.stereoCalibrate()
# calibrator.readStereoData()
# Calibrate outside--------
print('Stereo Calibrate outside')
# stereo_calibrate(outside=True)
# Calibrate inside---------
print('Stereo Calibrate inside')
stereo_calibrate(outside=False)
def testCalibration(outside=True):
if outside:
images = '/home/eugeniu/Desktop/my_data/CameraCalibration/data/car_cam_data/chess/outside/Stereo'
name = 'outside'
else:
images = '/home/eugeniu/Desktop/my_data/CameraCalibration/data/car_cam_data/chess/inside/Stereo'
name = 'inside'
images = '/home/eugeniu/Desktop/Stereo_test'
calibrator = StereoChess_Calibrator(images)
calibrator.name = name
calibrator.readMonoData()
calibrator.read_images(test=True)
calibrator.readStereoData()
calibrator.depth()
# Test calibration outside-----
print('Test calibration outside')
# testCalibration(outside=True)
# Test calibration inside
print('Test calibration inside')
# testCalibration(outside=False)
def Charuco_MonoCalibration():
def calibrate_(leftCam=False, outside=False):
if leftCam:
if outside:
images = glob.glob(
'/home/eugeniu/Desktop/my_data/CameraCalibration/data/car_cam_data/charuco/outside/Left/*.png')
name = 'charuco_outside_left'
else:
images = glob.glob(
'/home/eugeniu/Desktop/my_data/CameraCalibration/data/car_cam_data/charuco/inside/Left/*.png')
name = 'charuco_inside_left'
else:
if outside:
images = glob.glob(
'/home/eugeniu/Desktop/my_data/CameraCalibration/data/car_cam_data/charuco/outside/Right/*.png')
name = 'charuco_outside_right'
else:
images = glob.glob(
'/home/eugeniu/Desktop/my_data/CameraCalibration/data/car_cam_data/charuco/inside/Right/*.png')
name = 'charuco_inside_right'
calibrator = MonoCharuco_Calibrator(name=name)
calibrator.doStuff(images=images, project=True, single_flag=True, extended=True)
return calibrator
# ---------Work with right camera ----------------------------------------------
# calibrate right camera outside
print('=== Calibrate right camera outside ===')
calibrator = calibrate_(leftCam=False, outside=True)
#calibrator.visualize_calibration_boards()
# calibrate right camera inside
print('=== Calibrate right camera inside ===')
#calibrator = calibrate_(leftCam=False, outside=False)
# calibrator.visualize_calibration_boards()
'''ideal_calibration_right = load_obj(name='charuco_outside_right')
calibrator.adjustCalibration(K=ideal_calibration_right['K'],
D=ideal_calibration_right['D'])'''
# ---------Work with left camera -----------------------------------------------
# calibrate left camera outside
print('=== Calibrate left camera outside ===')
#calibrator = calibrate_(leftCam=True, outside=True)
# calibrate left camera inside
print('=== Calibrate left camera inside ===')
#calibrator = calibrate_(leftCam=True, outside=False)
'''ideal_calibration_left = load_obj(name='charuco_outside_left')
calibrator.adjustCalibration(K=ideal_calibration_left['K'],
D=ideal_calibration_left['D'])'''
print('Mono camera calibration done')
plt.show()
def Charuco_StereoCalibration():
def stereo_calibrate(outside=True):
if outside:
images = '/home/eugeniu/Desktop/my_data/CameraCalibration/data/car_cam_data/charuco/outside/Stereo'
name = 'outside'
else:
images = '/home/eugeniu/Desktop/my_data/CameraCalibration/data/car_cam_data/charuco/inside/Stereo'
name = 'inside'
calibrator = StereoCharuco_Calibrator(images, name=name)
calibrator.createCalibrationBoard()
calibrator.readMonoData()
calibrator.read_images(test=False)
calibrator.calibrationReport()
# calibrator.stereoCalibrate(save=True)
# calibrator.readStereoData()
# Calibrate outside--------
print('Stereo Calibrate outside')
stereo_calibrate(outside=True)
# Calibrate inside---------
print('Stereo Calibrate inside')
stereo_calibrate(outside=False)
def testCalibration(outside=True):
if outside:
images = '/home/eugeniu/Desktop/my_data/CameraCalibration/data/car_cam_data/charuco/outside/Stereo'
name = 'outside'
else:
images = '/home/eugeniu/Desktop/my_data/CameraCalibration/data/car_cam_data/charuco/inside/Stereo'
name = 'inside'
images = '/home/eugeniu/Desktop/Stereo_test'
calibrator = StereoCharuco_Calibrator(images, name=name)
calibrator.createCalibrationBoard()
calibrator.readMonoData()
calibrator.read_images(test=True)
calibrator.readStereoData()
calibrator.depth()
# Test calibration outside-----
print('Test calibration outside')
# testCalibration(outside=True)
# Test calibration inside
print('Test calibration inside')
# testCalibration(outside=False)
def visualize3Dpoints():
import open3d
def visualize_model(file):
for i, file_path in enumerate(file):
print("{} Load a ply point cloud, print it, and render it".format(file_path))
pcd = open3d.io.read_point_cloud(file_path)
open3d.visualization.draw_geometries([pcd])
# open3d.io.write_point_cloud("file{}.pcd".format(i), pcd)
points = glob.glob('/home/eugeniu/Desktop/my_data/CameraCalibration/data/saved_files/*.ply')
# points = glob.glob('/home/eugeniu/Desktop/my_data/*.ply')
visualize_model(points)
def combinedChess_and_Charuco():
calibrator = MonoCombinedCalibration()
'''names = ['outside', 'inside']
#names = ['outside']
for name in names:
calibrator = CombinedCalibration(name=name, flipVertically=True)
calibrator.readMonoData()
# read stereo images & stereo calibrate
imgChess = '/home/eugeniu/Desktop/my_data/CameraCalibration/data/car_cam_data/chess/{}/Stereo'.format(name)
imgCharuco = '/home/eugeniu/Desktop/my_data/CameraCalibration/data/car_cam_data/charuco/{}/Stereo'.format(name)
# calibrator.calibrateStereo(imgChess, imgCharuco, see=True)
# calibrator.calibrationReport(imgChess, imgCharuco)
calibrator.readStereoData()
print('Test combined stereo calibration')
images = '/home/eugeniu/Desktop/Stereo_test'
calibrator.depth(testImages=images, second=False)
# calibrator.gridSearchdepth(testImages=images)'''
def GridSearchforDisparity():
names = ['outside', 'inside']
names['outside']
for name in names:
calibrator = CombinedCalibration(name=name)
calibrator.readMonoData()
# read stereo images & stereo calibrate
imgChess = '/home/eugeniu/Desktop/my_data/CameraCalibration/data/car_cam_data/chess/{}/Stereo'.format(name)
imgCharuco = '/home/eugeniu/Desktop/my_data/CameraCalibration/data/car_cam_data/charuco/{}/Stereo'.format(name)
# calibrator.calibrateStereo(imgChess, imgCharuco, see=False)
# calibrator.calibrationReport(imgChess, imgCharuco)
calibrator.readStereoData()
print('Test combined stereo calibration')
images = '/home/eugeniu/Desktop/Stereo_test'
calibrator.depth(testImages=images)
if __name__ == '__main__':
print('Main')
# Mono calibration with chessboard----------------------------------
#Chess_MonoCalibration()
# Stereo calibration with chessboard--------------------------------
# Chess_StereoCalibration()
# Mono calibration with charuco----------------------------------
# Charuco_MonoCalibration()
# Stereo calibration with charuco--------------------------------
# Charuco_StereoCalibration()
# combined chess & charuco images
combinedChess_and_Charuco()
# View pointcloud-------------------------------------------------
#visualize3Dpoints()