-
Notifications
You must be signed in to change notification settings - Fork 3
/
Simulation.py
350 lines (299 loc) · 17.5 KB
/
Simulation.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import numpy as np
import sys
import argparse
from PyQt5.QtCore import pyqtSignal, QThread, Qt, pyqtSlot
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtWidgets import QApplication, QWidget, QDialog, QLabel, QVBoxLayout, QHBoxLayout, QMainWindow, QPushButton
from aruco_tracker import relativePosition, draw, calibrate, saveCoefficients, loadCoefficients
import cv2
import cv2.aruco as aruco
import pickle
cap = cv2.VideoCapture(0)
calibrationMarkerID = None
needleMarkerID = None
ultraSoundMarkerID = None
mtx, dist = None, None # Camera parameters
pressedKey = None
save_Name = "savedCalibration.pkl" # File to store calibration info
needleComposeRvec, needleComposeTvec = None, None # Composed for needle
ultraSoundComposeRvec, ultraSoundComposeTvec = None, None # Composed for ultrasound
class Thread(QThread):
# Slots for the gui
changePixmap = pyqtSignal(QImage)
changeXDiff = pyqtSignal(str)
changeYDiff = pyqtSignal(str)
changeZDiff = pyqtSignal(str)
calibrationType = pyqtSignal(str)
# Thread function
def run(self):
self.track(mtx, dist)
# Does all the tracking and updates the gui with slots
def track(self, matrix_coefficients, distortion_coefficients):
global pressedKey, needleComposeRvec, needleComposeTvec, ultraSoundComposeRvec, ultraSoundComposeTvec
""" Real time ArUco marker tracking. """
TcomposedRvecNeedle, TcomposedTvecNeedle = None, None
TcomposedRvecUltrasound, TcomposedTvecUltrasound = None, None
# Behaviour is a key between calibration types.
# No simulation is equal to 0
# Needle Calibration is equal to 1
# Ultrasound Calibration is equal to 2
# Press
behaviour = 0
try:
while True:
# No marker detected
isCalibrationMarkerDetected = False
isNeedleDetected = False
isUltraSoundDetected = False
# Get the next frame to process
ret, frame = cap.read()
# operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Change grayscale
aruco_dict = aruco.Dictionary_get(aruco.DICT_5X5_250) # Use 5x5 dictionary to find markers
parameters = aruco.DetectorParameters_create() # Marker detection parameters
# lists of ids and the corners beloning to each id
corners, ids, rejected_img_points = aruco.detectMarkers(gray, aruco_dict,
parameters=parameters,
cameraMatrix=matrix_coefficients,
distCoeff=distortion_coefficients)
# Get the behaviour and update the gui
if behaviour == 0:
self.calibrationType.emit('Simulation')
elif behaviour == 1:
self.calibrationType.emit('Needle calibration')
else:
self.calibrationType.emit('Ultrasound calibration')
pass
if np.all(ids is not None): # If there are markers found by detector
# sort the markers
zipped = zip(ids, corners)
ids, corners = zip(*(sorted(zipped)))
# 4 axis for ultrasound, 2axis for needle
axisForFourPoints = np.float32(
[[-0.02, -0.02, 0], [-0.02, 0.02, 0], [0.02, -0.02, 0], [0.02, 0.02, 0]]).reshape(-1,3) # axis for a plan
axisForTwoPoints = np.float32([[0.01, 0, 0], [-0.01, 0, 0]]).reshape(-1, 3) # axis for a line
for i in range(0, len(ids)): # Iterate in markers
# Estimate pose of each marker and return the values rvec and tvec---different from camera coefficients
rvec, tvec, markerPoints = aruco.estimatePoseSingleMarkers(corners[i], 0.02, matrix_coefficients,
distortion_coefficients)
if ids[i] == calibrationMarkerID:
calibrationRvec = rvec
calibrationTvec = tvec
isCalibrationMarkerDetected = True
calibrationMarkerCorners = corners[i]
elif ids[i] == needleMarkerID:
needleRvec = rvec
needleTvec = tvec
isNeedleDetected = True
needleCorners = corners[i]
elif ids[i] == ultraSoundMarkerID:
ultraSoundRvec = rvec
ultraSoundTvec = tvec
isUltraSoundDetected = True
ultrasoundCorners = corners[i]
(rvec - tvec).any() # get rid of that nasty numpy value array error
# aruco.drawAxis(frame, matrix_coefficients, distortion_coefficients, rvec, tvec, 0.01)
frame = aruco.drawDetectedMarkers(frame, corners) # Draw A square around the markers
if isNeedleDetected and needleComposeRvec is not None and needleComposeTvec is not None:
info = cv2.composeRT(needleComposeRvec, needleComposeTvec, needleRvec.T, needleTvec.T)
TcomposedRvecNeedle, TcomposedTvecNeedle = info[0], info[1]
imgpts, jac = cv2.projectPoints(axisForTwoPoints, TcomposedRvecNeedle, TcomposedTvecNeedle, matrix_coefficients,
distortion_coefficients)
frame = draw(frame, imgpts, (200, 200, 220))
if isUltraSoundDetected and ultraSoundComposeRvec is not None and ultraSoundComposeTvec is not None:
info = cv2.composeRT(ultraSoundComposeRvec, ultraSoundComposeTvec, ultraSoundRvec.T, ultraSoundTvec.T)
TcomposedRvecUltrasound, TcomposedTvecUltrasound = info[0], info[1]
imgpts, jac = cv2.projectPoints(axisForFourPoints, TcomposedRvecUltrasound, TcomposedTvecUltrasound, matrix_coefficients,
distortion_coefficients)
frame = draw(frame, imgpts, (60, 200, 50))
# If the both markers can be seen by the camera, print the xyz differences between them
# So it will guide the user
if isNeedleDetected and needleComposeRvec is not None and needleComposeTvec is not None and \
isUltraSoundDetected and ultraSoundComposeRvec is not None and ultraSoundComposeTvec is not None:
xdiff = round((TcomposedTvecNeedle[0] - TcomposedTvecUltrasound[0])[0], 3)
ydiff = round((TcomposedTvecNeedle[1] - TcomposedTvecUltrasound[1])[0], 3)
zdiff = round((TcomposedTvecNeedle[2] - TcomposedTvecUltrasound[2])[0], 3)
self.changeXDiff.emit('X difference:' + str(xdiff))
self.changeYDiff.emit('Y difference:' + str(ydiff))
self.changeZDiff.emit('Z difference:' + str(zdiff))
# Display the resulting frame
rgbImage = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
convertToQtFormat = QImage(rgbImage.data, rgbImage.shape[1], rgbImage.shape[0], QImage.Format_RGB888)
p = convertToQtFormat.scaled(640, 480, Qt.KeepAspectRatio)
self.changePixmap.emit(p)
# Key handling. For marker calibration, saving marker etc.
if pressedKey is None: # No key pressed
pass
elif pressedKey == Qt.Key_C: # Button for calibration.
if ids is not None and len(ids) > 1: # If there are two markers, reverse the second and get the difference
if isNeedleDetected and isCalibrationMarkerDetected and behaviour == 1:
needleComposeRvec, needleComposeTvec = relativePosition(calibrationRvec, calibrationTvec,
needleRvec, needleTvec)
savedNeedleRvec, savedNeedleTvec = needleComposeRvec, needleComposeTvec
elif isUltraSoundDetected and isCalibrationMarkerDetected and behaviour == 2:
ultraSoundComposeRvec, ultraSoundComposeTvec = relativePosition(calibrationRvec,
calibrationTvec,
ultraSoundRvec,
ultraSoundTvec)
savedUltraSoundRvec, savedUltraSoundTvec = ultraSoundComposeRvec, ultraSoundComposeTvec
elif pressedKey == Qt.Key_U: # Button for moving z axis 1 mm
if behaviour == 1 and needleComposeTvec is not None:
needleComposeTvec = needleComposeTvec + [[0], [0], [0.001]]
elif behaviour == 2 and ultraSoundComposeTvec is not None:
ultraSoundComposeTvec = ultraSoundComposeTvec + [[0], [0], [0.001]]
elif pressedKey == Qt.Key_D: # Button for moving z axis -1 mm
if behaviour == 1 and needleComposeTvec is not None:
needleComposeTvec = needleComposeTvec + [[0], [0], [-0.001]]
elif behaviour == 2 and ultraSoundComposeTvec is not None:
ultraSoundComposeTvec = ultraSoundComposeTvec + [[0], [0], [-0.001]]
elif pressedKey == Qt.Key_R: # Button for moving x axis 1 mm
if behaviour == 1 and needleComposeTvec is not None:
needleComposeTvec = needleComposeTvec + [[0.001], [0], [0]]
elif behaviour == 2 and ultraSoundComposeTvec is not None:
ultraSoundComposeTvec = ultraSoundComposeTvec + [[0.001], [0], [0]]
elif pressedKey == Qt.Key_L: # Button for moving x axis -1 mm
if behaviour == 1 and needleComposeTvec is not None:
needleComposeTvec = needleComposeTvec + [[-0.001], [0], [0]]
elif behaviour == 2 and ultraSoundComposeTvec is not None:
ultraSoundComposeTvec = ultraSoundComposeTvec + [[-0.001], [0], [0]]
elif pressedKey == Qt.Key_B: # Button for moving y axis -1 mm
if behaviour == 1 and needleComposeTvec is not None:
needleComposeTvec = needleComposeTvec + [[0], [-0.001], [0]]
elif behaviour == 2 and ultraSoundComposeTvec is not None:
ultraSoundComposeTvec = ultraSoundComposeTvec + [[0], [-0.001], [0]]
elif pressedKey == Qt.Key_F: # Button for moving y axis 1 mm
if behaviour == 1 and needleComposeTvec is not None:
needleComposeTvec = needleComposeTvec + [[0], [0.001], [0]]
elif behaviour == 2 and ultraSoundComposeTvec is not None:
ultraSoundComposeTvec = ultraSoundComposeTvec + [[0], [0.001], [0]]
elif pressedKey == Qt.Key_P: # print necessary information here, for debug
pass # Insert necessary print here
elif pressedKey == Qt.Key_S: # Save the calibration vectors to a file.
if(needleComposeRvec is not None and needleComposeTvec is not None and ultraSoundComposeRvec is not None and ultraSoundComposeTvec is not None):
print(needleComposeRvec, needleComposeTvec, ultraSoundComposeRvec, ultraSoundComposeTvec)
fileObject = open(save_Name, 'wb')
data = [needleComposeRvec, needleComposeTvec, ultraSoundComposeRvec, ultraSoundComposeTvec]
pickle.dump(data, fileObject)
fileObject.close()
elif pressedKey == Qt.Key_X: # change simulation type, "Simulation, needle calibration, ultrasound calibration"
behaviour = (behaviour + 1) % 3
# print(behaviour)
pressedKey = None
except Exception:
pass
finally:
# When everything done, release the capture
cap.release()
return frame
class App(QWidget):
def __init__(self):
super().__init__()
self.title = "Augmented Biopsy"
# Start points and the resolution
self.left = 40
self.top = 40
self.width = 1024
self.height = 768
self.initUI()
# slot for setting the image.
@pyqtSlot(QImage)
def setImage(self, image):
self.streamLabel.setPixmap(QPixmap.fromImage(image))
# slot for the label that shows which type is on. Simulation, needle calibration, ultrasound calibration.
@pyqtSlot(str)
def setCalibrationTypeLabel(self, labelText):
self.calibrationType.setText(labelText)
# slot for x diff label.
@pyqtSlot(str)
def setXDiffLabel(self, labelText):
self.xDifflabel.setText(labelText)
# slot for y diff label.
@pyqtSlot(str)
def setYDiffLabel(self, labelText):
self.yDifflabel.setText(labelText)
# slot for z diff label.
@pyqtSlot(str)
def setZDiffLabel(self, labelText):
self.zDifflabel.setText(labelText)
# Key press event to get the keys.
def keyPressEvent(self, event):
global pressedKey
pressedKey = event.key()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
# create a label for image
self.streamLabel = QLabel(self)
self.streamLabel.resize(640, 480)
self.streamLabel.setAlignment(Qt.AlignCenter)
# create a label for showing the type of the program, calibration or simulation
self.calibrationType = QLabel(self)
self.calibrationType.setAlignment(Qt.AlignCenter)
# create a label for x diff
self.xDifflabel = QLabel(self)
self.xDifflabel.setAlignment(Qt.AlignCenter)
# create a label for y diff
self.yDifflabel = QLabel(self)
self.yDifflabel.setAlignment(Qt.AlignCenter)
# create a label for z diff
self.zDifflabel = QLabel(self)
self.zDifflabel.setAlignment(Qt.AlignCenter)
# self.run_button = QPushButton('Start')
displayVbox = QVBoxLayout()
displayVbox.addWidget(self.streamLabel)
displayVbox.addWidget(self.calibrationType)
displayVbox.addWidget(self.xDifflabel)
displayVbox.addWidget(self.yDifflabel)
displayVbox.addWidget(self.zDifflabel)
self.setLayout(displayVbox)
th = Thread(self)
th.changePixmap.connect(self.setImage)
th.changeXDiff.connect(self.setXDiffLabel)
th.changeYDiff.connect(self.setYDiffLabel)
th.changeZDiff.connect(self.setZDiffLabel)
th.calibrationType.connect(self.setCalibrationTypeLabel)
th.start()
return
''' Main Function '''
if __name__ == '__main__':
global window
parser = argparse.ArgumentParser(description='Aruco Marker Tracking')
parser.add_argument('--coefficients', metavar='bool', required=True,
help='File name for matrix coefficients and distortion coefficients')
parser.add_argument('--calibrationMarker', metavar='int', required=True,
help='Marker ID for the calibration marker')
parser.add_argument('--needleMarker', metavar='int', required=True,
help='Marker ID for the needle\'s marker')
parser.add_argument('--ultrasoundMarker', metavar='int', required=True,
help='Marker ID for the needle\'s marker')
parser.add_argument('--savedCalibration', metavar='int', required=False,
help='Calibration saves')
# Parse the arguments and take action for that.
args = parser.parse_args()
calibrationMarkerID = int(args.calibrationMarker)
needleMarkerID = int(args.needleMarker)
ultraSoundMarkerID = int(args.ultrasoundMarker)
# If marker calibration is already done and saved, you can load it.
if args.savedCalibration == '1':
fileObject = open(save_Name, 'rb')
saved = pickle.load(fileObject)
needleComposeRvec, needleComposeTvec, ultraSoundComposeRvec, ultraSoundComposeTvec = saved
fileObject.close()
# If camera calibration is done and saved, you can load it.
if args.coefficients == '1':
mtx, dist = loadCoefficients("calib_images/calibrationCoefficients.yaml")
ret = True
else: # No calibration, use "calib_images" as directory.
ret, mtx, dist, rvecs, tvecs = calibrate("calib_images")
saveCoefficients(mtx, dist, "calib_images/calibrationCoefficients.yaml")
print("Calibration is completed. Starting tracking sequence.")
# If everything is okay, we can init the gui and start the stream.
if ret:
app = QApplication(sys.argv)
main_window = QMainWindow()
widget = App()
widget.show()
app.exec_()
# Release the camera
cap.release()
cv2.destroyAllWindows()