-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathDolPSegmentation.py
More file actions
317 lines (258 loc) · 10.2 KB
/
DolPSegmentation.py
File metadata and controls
317 lines (258 loc) · 10.2 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
312
313
314
315
316
317
import PySide6.QtCore
from PySide6.QtWidgets import (
QApplication,
QWidget,
QMainWindow,
QHBoxLayout,
QVBoxLayout,
QPushButton,
QMessageBox,
)
from PySide6.QtCore import QStandardPaths, QDir, QFileInfo, QEvent
import PySide6
import time
import numpy as np
import imagingcontrol4 as ic4
import sliderctrl as sldctrl
WINDOW_TITLE = "DoLP Segmentation"
class Listener(ic4.QueueSinkListener):
# Listener to demonstrate processing and displaying received images
def __init__(self, display: ic4.Display):
self.display = display
self.buffer_pool = ic4.BufferPool()
self.threshold_dolp = 30
self.threshold_intensity = 30
def sink_connected(
self,
sink: ic4.QueueSink,
image_type: ic4.ImageType,
min_buffers_required: int,
) -> bool:
sink.alloc_and_queue_buffers(min_buffers_required + 1)
return True
def frames_queued(self, sink: ic4.QueueSink):
# Get the new buffer from the sink
buffer = sink.pop_output_buffer()
src_wrap = buffer.numpy_wrap()
dest_buffer = self.buffer_pool.get_buffer(
buffer.image_type.with_pixel_format(ic4.PixelFormat.BGR8)
)
dest_wrap = dest_buffer.numpy_wrap()
if buffer.image_type.pixel_format == ic4.PixelFormat.PolarizedADIMono8:
# Extract DoLP and intensity channels and replicate across RGB
dolp = np.repeat(src_wrap[:, :, 1:2], 3, axis=2)
intensity = np.repeat(src_wrap[:, :, 2:3], 3, axis=2)
dest_wrap[:, :, 0:3] = np.where(
np.logical_and(
dolp >= self.threshold_dolp,
intensity >= self.threshold_intensity,
),
[0, 0, 255],
intensity,
)
else:
# Use polarization channels 1–3 for thresholding and 6–4 (reversed)
dest_wrap[:, :, 0:3] = np.where(
np.logical_and(
src_wrap[:, :, 1:4] >= self.threshold_dolp,
src_wrap[:, :, 6:3:-1] >= self.threshold_intensity,
),
[[[0, 0, 255]]],
src_wrap[:, :, 6:3:-1],
)
self.display.display_buffer(dest_buffer)
class MainWindow(QMainWindow):
# The main window for the simple test application
def __init__(self):
QMainWindow.__init__(self)
self.setWindowTitle(WINDOW_TITLE)
self.create_gui()
# Create a Grabber object to communicate with a video capture device
self.grabber = ic4.Grabber()
self.listener = Listener(self.display)
self.sink = ic4.QueueSink(self.listener)
# Show a warning, if there are no devices. May no GenTL
# producer is installed.
self.check_for_devices()
self.on_select_device()
self.update_controls()
def create_gui(self) -> None:
"""Create the user interface"""
main_widget = QWidget()
mainlayout = QHBoxLayout()
btn_layout = QVBoxLayout()
# Create a widget to use as the target for video display
self.video_widget = ic4.pyside6.DisplayWidget()
self.display = self.video_widget.as_display()
self.display.set_render_position(
ic4.DisplayRenderPosition.STRETCH_CENTER
)
mainlayout.addWidget(self.video_widget)
self.btn_device = QPushButton("Device")
self.btn_properties = QPushButton("Properties")
self.btn_start = QPushButton("Start")
self.sld_threshold_dolp = sldctrl.SliderControlInt(
"DoLP Threshold", 30, 0, 255
)
self.sld_threshold_dolp.value_changed.connect(
self.on_threshold_dolp_changed
)
self.sld_threshold_dolp.setMaximumWidth(250)
self.sld_threshold_intensity = sldctrl.SliderControlInt(
"Intensity Threshold", 10, 0, 255
)
self.sld_threshold_intensity.value_changed.connect(
self.on_threshold_intensity_changed
)
self.sld_threshold_intensity.setMaximumWidth(250)
self.btn_device.setMaximumWidth(100)
self.btn_properties.setMaximumWidth(100)
self.btn_start.setMaximumWidth(100)
self.btn_device.clicked.connect(self.on_select_device)
self.btn_properties.clicked.connect(self.on_device_properties)
self.btn_start.clicked.connect(self.on_start)
btn_layout.setAlignment(PySide6.QtCore.Qt.AlignTop)
btn_layout.addWidget(self.btn_device)
btn_layout.addWidget(self.btn_properties)
btn_layout.addWidget(self.btn_start)
btn_layout.addWidget(self.sld_threshold_dolp)
btn_layout.addWidget(self.sld_threshold_intensity)
mainlayout.addLayout(btn_layout)
main_widget.setLayout(mainlayout)
self.setCentralWidget(main_widget)
self.resize(1024, 768)
def check_for_devices(self) -> None:
"""Show a warning, if no interfaces are found by
IC Imaging Control 4. This could mean that no IC4
GenTL Producers are installed.
"""
if len(ic4.DeviceEnum.interfaces()) == 0:
QMessageBox.warning(
self,
WINDOW_TITLE,
"No interfaces found.\nIs an IC4 GenTL Producer from\n"
+ "https://www.theimagingsource.com/en-us/support/download/\n"
+ "installed?",
QMessageBox.StandardButton.Ok,
)
def update_controls(self) -> None:
"""Enable or disable the controls depending on the device status"""
if self.grabber.is_device_valid:
self.btn_properties.setEnabled(True)
self.btn_start.setEnabled(True)
if self.grabber.is_streaming:
self.btn_start.setText("Stop")
else:
self.btn_start.setText("Start")
else:
self.btn_properties.setEnabled(False)
self.btn_start.setEnabled(False)
self.btn_start.setText("Start")
def is_polarization_camera(self) -> bool:
"""Check, whether a polarization camera is selected.
Polarization camera names start with "DYK" or "DZK"
If the current camera is not a polarization camera, a
warning is shown and the camera is closed.
Returns:
bool: True, if a polarization camera is used.
"""
if self.grabber.is_device_valid:
if self.grabber.device_info.model_name.startswith(
"DYK"
) or self.grabber.device_info.model_name.startswith("DZK"):
self.setWindowTitle(
WINDOW_TITLE + " " + self.grabber.device_info.model_name
)
return True
else:
QMessageBox.warning(
self,
WINDOW_TITLE,
"No polarization camera (DYK or DZK) selected.",
QMessageBox.StandardButton.Ok,
)
self.setWindowTitle(WINDOW_TITLE)
self.grabber.device_close()
return False
def set_polarization_format(self) -> bool:
"""Try to set the mono polarization format first. If this fails
then there is color polarization format and the RGB polarization
format is set.
Returns:
bool: True on success, False on any error.
"""
self.grabber.device_property_map.set_value(
"ProcessedPixelFormatsEnable", True
)
try:
self.grabber.device_property_map.set_value(
ic4.PropId.PIXEL_FORMAT, ic4.PixelFormat.PolarizedADIMono8
)
return True
except ic4.IC4Exception:
try:
self.grabber.device_property_map.set_value(
ic4.PropId.PIXEL_FORMAT, ic4.PixelFormat.PolarizedADIRGB8
)
return True
except ic4.IC4Exception as e:
print(e.message)
return False
def on_select_device(self) -> None:
"""Show the IC 4 device selection dialog."""
dlg = ic4.pyside6.DeviceSelectionDialog(self.grabber, self)
if dlg.exec() == PySide6.QtWidgets.QDialog.Accepted:
if self.is_polarization_camera():
self.set_polarization_format()
self.on_start()
self.update_controls()
def on_device_properties(self) -> None:
"""Show the property dialog for the selected device."""
if self.grabber.is_device_valid:
dlg = ic4.pyside6.PropertyDialog(self.grabber, self)
if dlg.exec() == PySide6.QtWidgets.QDialog.Accepted:
return
def on_start(self) -> None:
"""Start and stop the live video."""
if self.grabber.is_device_valid:
if self.grabber.is_streaming:
self.btn_start.setText("Stop")
self.grabber.stream_stop()
else:
self.btn_start.setText("Start")
self.grabber.stream_setup(self.sink)
self.update_controls()
def on_threshold_dolp_changed(self, value: int):
"""Event handler of the DoLP threshold slider. Pass the new
DoLP Threshold to the self.listener
Args:
value (int): The new value from by the slider
"""
self.listener.threshold_dolp = value
def on_threshold_intensity_changed(self, value: int):
"""Event handler of the intensity threshold slider. Pass the new
DoLP Threshold to the self.listener
Args:
value (int): The new value from by the slider
"""
self.listener.threshold_intensity = value * 3
def closeEvent(self, event: QEvent) -> None:
"""Called by Pyside6, when the window is closed.
The video stream is stopped and the grabber's
device state is saved, so the camera will be
opened automatically at next program start.
Args:
event (QEvent): Event.
"""
if self.grabber.is_streaming:
self.grabber.stream_stop()
if __name__ == "__main__":
from sys import argv
app = QApplication(argv)
app.setApplicationName(WINDOW_TITLE)
app.setStyle("fusion")
ic4.Library.init()
wnd = MainWindow()
wnd.show()
app.exec()
ic4.Library.exit()