-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlutpanel.py
246 lines (195 loc) · 10.6 KB
/
lutpanel.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
#!/usr/bin/env python
import numpy as np
from PySide6.QtCore import QObject, Signal
from PySide6.QtWidgets import QGraphicsScene
from PySide6.QtGui import QColor
from image import lut
class LutPanel (QObject):
signal_current_lut_changed = Signal()
signal_reset_current_lut_range = Signal()
def __init__ (self, ui, parent = None):
super().__init__(parent)
self.ui = ui
self.ui.combo_lut.addItems([item for item in lut.lut_dict])
self.ui.combo_bits.addItems([item for item in lut.bit_dict])
def init_widgets (self, stack):
self.init_luts(stack)
self.init_boxes()
self.update_lut_panel_silently()
self.scene_lut = QGraphicsScene()
self.scene_lut.setBackgroundBrush(QColor('white'))
self.ui.gview_lut.setScene(self.scene_lut)
def init_luts (self, stack):
self.lut_list = []
if stack is None:
self.lut_list.append(lut.LUT())
elif stack.c_count == 1:
image_lut = lut.LUT(lut_name = "Gray", pixel_values = stack.image_array[:, 0])
self.lut_list.append(image_lut)
else:
for channel in range(stack.c_count):
lut_name = lut.lut_names[channel % len(lut.lut_names)]
image_lut = lut.LUT(lut_name = lut_name, pixel_values = stack.image_array[:, channel])
self.lut_list.append(image_lut)
def init_boxes (self):
self.ui.combo_channel.blockSignals(True)
self.ui.combo_channel.clear()
self.ui.combo_channel.addItems([f"Channel {i}" for i in range(len(self.lut_list))])
self.ui.combo_channel.blockSignals(False)
lut = self.lut_list[0]
self.ui.combo_lut.setCurrentText(lut.lut_name)
# enable/disable combo and check boxes
self.update_widget_activation_status()
self.ui.combo_bits.setCurrentText(lut.bit_mode)
self.ui.check_lut_invert.setChecked(lut.lut_invert)
self.ui.dspin_auto_cutoff.setValue(lut.auto_cutoff)
self.ui.check_auto_lut.setChecked(lut.auto_lut)
def connect_signals_to_slots (self):
self.ui.combo_channel.currentIndexChanged.connect(self.slot_lut_panel_changed)
self.ui.check_composite.stateChanged.connect(self.slot_lut_panel_changed)
self.ui.check_lut_grayscale.stateChanged.connect(self.slot_lut_panel_changed)
self.ui.check_lut_invert.stateChanged.connect(self.slot_lut_panel_changed)
self.ui.check_lut_blank.stateChanged.connect(self.slot_lut_panel_changed)
self.ui.combo_lut.currentIndexChanged.connect(self.slot_lut_panel_changed)
self.ui.combo_bits.currentIndexChanged.connect(self.slot_lut_panel_changed)
self.ui.check_auto_lut.stateChanged.connect(self.slot_lut_panel_changed)
self.ui.dspin_auto_cutoff.valueChanged.connect(self.slot_lut_panel_changed)
self.ui.dspin_auto_cutoff.editingFinished.connect(self.slot_lut_panel_changed)
self.ui.slider_lut_lower.valueChanged.connect(self.slot_lower_slider_changed)
self.ui.slider_lut_upper.valueChanged.connect(self.slot_upper_slider_changed)
self.ui.button_reset_lut_range.clicked.connect(self.slot_reset_current_lut_range)
def restore_lut_settings (self, settings_list = []):
for index in range(min(len(self.lut_list), len(settings_list))):
self.lut_list[index].load_settings(settings_list[index])
self.update_lut_panel_silently()
def set_current_channel (self, channel = 0):
self.ui.combo_channel.setCurrentIndex(channel)
def update_lut_panel_silently (self):
# do not send signals from this function
current_lut = self.lut_list[self.ui.combo_channel.currentIndex()]
lut_range = current_lut.lut_range()
# enable/disable combo and check boxes
self.update_widget_activation_status()
self.ui.combo_lut.blockSignals(True)
self.ui.combo_lut.setCurrentText(current_lut.lut_name)
self.ui.combo_lut.blockSignals(False)
self.ui.combo_bits.blockSignals(True)
self.ui.combo_bits.setCurrentText(current_lut.bit_mode)
self.ui.combo_bits.blockSignals(False)
# update lut_invert, lut_blank and auto_lut
self.ui.check_lut_invert.blockSignals(True)
self.ui.check_lut_invert.setChecked(current_lut.lut_invert)
self.ui.check_lut_invert.blockSignals(False)
self.ui.check_lut_blank.blockSignals(True)
self.ui.check_lut_blank.setChecked(current_lut.lut_blank)
self.ui.check_lut_blank.blockSignals(False)
self.ui.check_auto_lut.blockSignals(True)
self.ui.check_auto_lut.setChecked(current_lut.auto_lut)
self.ui.check_auto_lut.blockSignals(False)
self.ui.dspin_auto_cutoff.blockSignals(True)
self.ui.dspin_auto_cutoff.setValue(current_lut.auto_cutoff)
self.ui.dspin_auto_cutoff.releaseKeyboard()
self.ui.dspin_auto_cutoff.blockSignals(False)
# set sliders
self.ui.slider_lut_upper.blockSignals(True)
self.ui.slider_lut_lower.blockSignals(True)
self.ui.slider_lut_upper.setMinimum(lut_range[0])
self.ui.slider_lut_upper.setMaximum(lut_range[1])
self.ui.slider_lut_lower.setMinimum(lut_range[0])
self.ui.slider_lut_lower.setMaximum(lut_range[1])
self.ui.slider_lut_upper.setValue(current_lut.lut_upper)
self.ui.slider_lut_lower.setValue(current_lut.lut_lower)
self.ui.slider_lut_upper.blockSignals(False)
self.ui.slider_lut_lower.blockSignals(False)
# set labels
if current_lut.bit_mode == "Float":
self.ui.label_bitrange_lower.setText(f"{lut_range[0]:.2e}")
self.ui.label_bitrange_upper.setText(f"{lut_range[1]:.2e}")
self.ui.label_lut_lower.setText(f"Lower limit: {current_lut.lut_lower:.2e}")
self.ui.label_lut_upper.setText(f"Upper limit: {current_lut.lut_upper:.2e}")
else:
self.ui.label_bitrange_lower.setText(f"{lut_range[0]:.2f}")
self.ui.label_bitrange_upper.setText(f"{lut_range[1]:.2f}")
self.ui.label_lut_lower.setText(f"Lower limit: {current_lut.lut_lower:.2f}")
self.ui.label_lut_upper.setText(f"Upper limit: {current_lut.lut_upper:.2f}")
def update_widget_activation_status (self):
# enable/disable combo and check boxes
self.ui.combo_lut.setEnabled(self.ui.check_composite.isChecked())
self.ui.check_lut_grayscale.setEnabled(not self.ui.check_composite.isChecked())
def update_lut_range_if_auto (self, image):
if self.ui.check_auto_lut.isChecked():
current_lut = self.lut_list[self.ui.combo_channel.currentIndex()]
current_lut.set_range_by_image(image, self.ui.dspin_auto_cutoff.value())
self.ui.slider_lut_upper.blockSignals(True)
self.ui.slider_lut_lower.blockSignals(True)
self.ui.slider_lut_upper.setValue(current_lut.lut_upper)
self.ui.slider_lut_lower.setValue(current_lut.lut_lower)
self.ui.slider_lut_upper.blockSignals(False)
self.ui.slider_lut_lower.blockSignals(False)
def reset_current_lut_range (self, pixel_values):
current_lut = self.lut_list[self.ui.combo_channel.currentIndex()]
current_lut.reset_lut_range(pixel_values)
self.update_lut_panel_silently()
def update_lut_view (self, image):
current_lut = self.lut_list[self.ui.combo_channel.currentIndex()]
lut_range = current_lut.lut_range()
width = self.ui.gview_lut.width()
height = self.ui.gview_lut.height()
self.scene_lut.clear()
self.scene_lut.setSceneRect(0, 0, width, height)
hists, bins = np.histogram(image, bins = int(width), range = lut_range)
max_hist = np.max(hists)
for index, hist in enumerate(hists):
x = width * (bins[index] - np.min(bins)) / np.ptp(bins)
y_bottom = height
y_top = height * (1 - float(hist) / max_hist)
self.scene_lut.addLine(x, y_bottom, x, y_top, QColor('gray'))
if np.isclose(lut_range[0], lut_range[1]) == False:
x_lower = width * (current_lut.lut_lower - lut_range[0]) / (lut_range[1] - lut_range[0])
x_upper = width * (current_lut.lut_upper - lut_range[0]) / (lut_range[1] - lut_range[0])
self.scene_lut.addLine(x_upper, 0, x_upper, height, QColor('black'))
self.scene_lut.addLine(x_lower, height, x_upper, 0)
def update_current_lut (self):
current_lut = self.lut_list[self.ui.combo_channel.currentIndex()]
current_lut.lut_name = self.ui.combo_lut.currentText()
current_lut.lut_lower = self.ui.slider_lut_lower.value()
current_lut.lut_upper = self.ui.slider_lut_upper.value()
current_lut.lut_invert = self.ui.check_lut_invert.isChecked()
current_lut.lut_blank = self.ui.check_lut_blank.isChecked()
current_lut.bit_mode = self.ui.combo_bits.currentText()
current_lut.auto_lut = self.ui.check_auto_lut.isChecked()
current_lut.auto_cutoff = self.ui.dspin_auto_cutoff.value()
def slot_upper_slider_changed (self):
self.ui.check_auto_lut.blockSignals(True)
self.ui.check_auto_lut.setChecked(False)
self.ui.check_auto_lut.blockSignals(False)
self.ui.slider_lut_upper.blockSignals(True)
self.ui.slider_lut_upper.setValue(max(self.ui.slider_lut_upper.value(), self.ui.slider_lut_lower.value()))
self.ui.slider_lut_upper.blockSignals(False)
self.update_current_lut()
self.update_lut_panel_silently()
self.signal_current_lut_changed.emit()
def slot_lower_slider_changed (self):
self.ui.check_auto_lut.blockSignals(True)
self.ui.check_auto_lut.setChecked(False)
self.ui.check_auto_lut.blockSignals(False)
self.ui.slider_lut_lower.blockSignals(True)
self.ui.slider_lut_lower.setValue(min(self.ui.slider_lut_upper.value(), self.ui.slider_lut_lower.value()))
self.ui.slider_lut_lower.blockSignals(False)
self.update_current_lut()
self.update_lut_panel_silently()
self.signal_current_lut_changed.emit()
def slot_lut_panel_changed (self):
self.update_current_lut()
self.update_lut_panel_silently()
self.signal_current_lut_changed.emit()
def slot_reset_current_lut_range (self):
self.signal_reset_current_lut_range.emit()
def current_channel (self):
return self.ui.combo_channel.currentIndex()
def is_lut_grayscale (self):
return self.ui.check_lut_grayscale.isChecked()
def is_composite (self):
return self.ui.check_composite.isChecked()
def is_auto_lut (self):
return self.ui.check_auto_lut.isChecked()