-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalibration.py
More file actions
311 lines (199 loc) · 10.4 KB
/
calibration.py
File metadata and controls
311 lines (199 loc) · 10.4 KB
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
import config
import camera_status
import save_data
import ctypes as C
import tisgrabber as IC
import numpy as np
import sys
import os
import cv2
import matplotlib.pyplot as plt
import math
from datetime import datetime as DateTime
lWidth = C.c_long()
lHeight = C.c_long()
iBitsPerPixel = C.c_int()
COLORFORMAT = C.c_int()
#Erstelle Kameraobjekt
Camera = IC.TIS_CAM()
#Terminierungs Kriterium
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
#Felder des Schachbrettmusters
#Breite x Höhe
pattern_size = (config.chessboard_w, config.chessboard_h)
#Bereite Objektpunkte vor, gemäß (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((np.prod(pattern_size),3), np.float32)
objp[:, :2] = np.indices(pattern_size).T.reshape(-1,2)
#Arrays für Objektpunkte und Bildpunkte
objpoints = [] # 3d-Punkte im Realraum
imgpoints = [] # 2d-Punkte im BIld
camera_status.status(Camera)
if Camera.IsDevValid() == 1:
#lege Videoformat (Breite x Höhe) fest
device_videoFormat = 'Y800 ('+str(config.DEVICE_WIDTH)+'x'+str(config.DEVICE_HEIGHT)+')'
Camera.SetVideoFormat(device_videoFormat)
print('Kamera: ', device_videoFormat, config.DEVICE_FPS, ' fps')
#Bildrate
Camera.SetFrameRate(config.DEVICE_FPS)
#Farbtiefe 8bit
Camera.SetFormat(IC.SinkFormats.Y800)
#Starte Videostream
Camera.StartLive(0)
print('Zum Kalibrierung', config.key_calibrate, 'drücken')
run = True
try:
while ( run ):
#zeichne Bild auf
Camera.SnapImage()
uncalibrated_img_stream = Camera.GetImageEx()
uncalibrated_img_stream = uncalibrated_img_stream.astype('uint8')
#Skaliere Bild
uncalibrated_img_stream = cv2.resize(uncalibrated_img_stream, (0,0), fx=.25, fy=.25)
#make a copy to draw the overlay on
#Bildkopie zum
uncalibrated_img_stream_view = uncalibrated_img_stream.copy()
#RGB Version (farbige Darstellung)
uncalibrated_img_stream_view = cv2.cvtColor(uncalibrated_img_stream_view, cv2.COLOR_GRAY2RGB)
#Höhe, Breite des Videostreams
h, w = uncalibrated_img_stream.shape[:2]
#Echtzeit Kantendetektion
margin = 25
rect_startpoint = (margin, margin)
rect_endpoint = (w - margin, h - margin)
#Kante detektiert: grün
color_detected = (0, 255, 0)
#Kante nicht detektiert: rot
color_undetected = (0, 0, 255)
#Linienstärke
thickness = 1
#finde und speichere gefundene Ecken in Array
corner_is_found, corners = cv2.findChessboardCorners(uncalibrated_img_stream, (pattern_size), None)
if corner_is_found == True:
objpoints = [objp]
#speichere letzten Frame (mit gefundenen Kanten)
dev1_uncalibrated = uncalibrated_img_stream.copy()
corners2 = cv2.cornerSubPix(uncalibrated_img_stream,corners,(11,11),(-1,-1),criteria)
imgpoints = [corners2]
#zeichne Fenster für detektierte Kanten
cv2.drawChessboardCorners(uncalibrated_img_stream_view, (pattern_size), corners2, corner_is_found)
cv2.putText(uncalibrated_img_stream_view,'Muster detektiert',(margin, h-5), cv2.FONT_HERSHEY_SIMPLEX, .5, (color_detected), 1, cv2.LINE_AA)
cv2.rectangle(uncalibrated_img_stream_view, rect_startpoint, rect_endpoint, color_detected, thickness)
else:
#zeichne Fenster für nicht detektierte Kanten
cv2.putText(uncalibrated_img_stream_view,'Muster nicht gefunden, bitte Position korrigieren',(margin, h-5), cv2.FONT_HERSHEY_SIMPLEX, .5, (color_undetected), 1, cv2.LINE_AA)
cv2.rectangle(uncalibrated_img_stream_view, rect_startpoint, rect_endpoint, color_undetected, thickness)
#draw window content
cv2.putText(uncalibrated_img_stream_view,'Wenn sich das Muster innerhalb des Rahmens befindet, drücke \'' + config.key_calibrate + '\' zum kalibrieren',(margin, margin-5), cv2.FONT_HERSHEY_SIMPLEX, .5, (color_detected), 1, cv2.LINE_AA)
cv2.imshow('Echtzeit Kamerakalibrierung: ' + str(config.DEVICE), uncalibrated_img_stream_view)
key = cv2.waitKey(1)
#press c to go on
if key == ord(config.key_calibrate):
print('Starte Kalibrierung ...')
#save uncalibrated frame
cv2.imwrite('calibration/dev1_uncalibrated.bmp', dev1_uncalibrated)
print('Frame gespeichert')
else:
continue
cv2.destroyAllWindows()
print('Berechne Verzerrung ...')
ret, camera_matrix, dist_coef, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, (w, h), None, None)
#Kalibrierungsinformationen
#print('ret ', ret)
#print('camera_matrix ', camera_matrix)
#print('dist ', dist_coef)
#print('rvecs ', rvecs)
#print('tvecs ', tvecs)
newcameramtx, roi = cv2.getOptimalNewCameraMatrix(camera_matrix, dist_coef, (w,h), 1, (w,h))
#Kalibrierter Frame
dev1_calibrated = cv2.undistort(dev1_uncalibrated, camera_matrix, dist_coef, None, newcameramtx)
#speichere Kalibrierung
cv2.imwrite('calibration/dev1_calibrated.bmp', dev1_calibrated)
#zeige unkalibrierte Version 50%
overlay = cv2.addWeighted(dev1_uncalibrated, 0.5, dev1_calibrated, 1, 0)
cv2.imshow('overlay: calibrated - uncalibrated', overlay)
cv2.imwrite('calibration/overlay.bmp', overlay)
cv2.waitKey(500)
cv2.destroyAllWindows()
run = False
#Berechne Skalierung des Musters
#sortiere BIldpunkte in 2d-Array gemäß [x0, y0], [x1, y1], [xn, yn]
square_dist_uncal = np.reshape(imgpoints, (42,2), order='C')
square_dist_pyt_uncal = []
for i in range(1, np.size(square_dist_uncal, 0)):
dx = square_dist_uncal[i,0] - square_dist_uncal[i-1, 0]
dy = square_dist_uncal[i,1] - square_dist_uncal[i-1, 1]
square_dist_pyt_uncal.append(math.sqrt(dx**2 + dy**2))
#print('dx: ',i, dx, 'dy ', dy, '\t', square_dist_pyt_uncal[i-1])
max_square_dist_uncal = square_dist_pyt_uncal[0]*2
for el in square_dist_pyt_uncal:
if el > max_square_dist_uncal:
square_dist_pyt_uncal.remove(el)
else:
pass
#####################################
#Unkalibrierte Vermessung
#####################################
#erstelle Werte für Diagramm
x = np.arange(len(square_dist_pyt_uncal))
y = square_dist_pyt_uncal
sq_mittelwert_uncal = sum(square_dist_pyt_uncal)/len(square_dist_pyt_uncal)
print('Mittelwert (unkalibrierte Kamera) :\t', sq_mittelwert_uncal, ' px')
#fülle Liste mit Mittelwerten
sq_mittelwert_container_uncal = []
for i in range(0, len(square_dist_pyt_uncal)):
sq_mittelwert_container_uncal.append(sq_mittelwert_uncal)
fig, ax = plt.subplots()
#x: Menge
#y: Distanzen
#sq_mittelwert_container: Mittelwert
ax.plot(x, y, 'k')
ax.plot(x, sq_mittelwert_container_uncal, 'r')
ax.set(xlabel='Anzahl vermessener Quadrate', ylabel='Gemessene Länge des Quadrates [Pixel]', title='Unkalibriert: Maßstab aus Schachbrettmuster')
ax.grid()
fig.savefig("calibration/uncalibrated_maßstab.pdf")
plt.show()
###############################
#Kalibrierte Vermessung
###############################
ret, corners_cal = cv2.findChessboardCorners(dev1_calibrated, (pattern_size), None)
if ret == True:
corners3 = cv2.cornerSubPix(dev1_calibrated, corners_cal,(3,3),(-1,-1),criteria)
imgp = [corners3]
square_dist_cal = np.reshape(imgp, (42,2), order='C')
square_dist_pyt_cal = []
for i in range(1, np.size(square_dist_cal, 0)):
dx = square_dist_cal[i,0] - square_dist_cal[i-1, 0]
dy = square_dist_cal[i,1] - square_dist_cal[i-1, 1]
square_dist_pyt_cal.append(math.sqrt(dx**2 + dy**2))
#print('dx: ',i, dx, 'dy ', dy, '\t', square_dist_pyt_cal[i-1])
max_square_dist_cal = square_dist_pyt_cal[0]*2
for el in square_dist_pyt_cal:
if el > max_square_dist_cal:
square_dist_pyt_cal.remove(el)
else:
pass
#erstelle Werte für Diagramm
x_cal = np.arange(len(square_dist_pyt_cal))
y_cal = square_dist_pyt_cal
sq_mittelwert_cal = sum(square_dist_pyt_cal)/len(square_dist_pyt_cal)
print('Mittelwert (kalibrierte Kamera) :\t', sq_mittelwert_cal, ' px')
#fülle Liste mit Mittelwerten
sq_mittelwert_container_cal = []
for i in range(0, len(square_dist_pyt_cal)):
sq_mittelwert_container_cal.append(sq_mittelwert_cal)
fig, ax = plt.subplots()
#x: Anzahl
#y: Distanzen
#sq_mittelwert_container: Mittelwert
ax.plot(x_cal, y_cal, 'k')
ax.plot(x_cal, sq_mittelwert_container_cal, 'r')
ax.set(xlabel='Anzahl vermessener Quadrate', ylabel='Gemessene Länge des Quadrates [Pixel]', title='Kalibriert: Maßstab aus Schachbrettmuster')
ax.grid()
fig.savefig("calibration/calibated_maßstab.pdf")
plt.show()
except KeyboardInterrupt:
Camera0.StopLive()
#schließe alle OpenCV-Fenster
cv2.destroyAllWindows()
else:
print( "Keine Kamera gefunden")