-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mesh_Labeler.py
1618 lines (1427 loc) · 72.7 KB
/
Mesh_Labeler.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys, os
# from PyQt5 import *
from PyQt5 import Qt
from PyQt5.QtGui import QKeySequence
from PyQt5.QtWidgets import QFileDialog, QMessageBox, QShortcut, QAbstractItemView
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
import pandas as pd
import numpy as np
import vtk
from vtk.util import numpy_support
from vedo import *
import vtkmodules
# from UI_files.MainWindow import *
from UI_files.MainWindow_v4 import *
class Mesh_Labeler(QtWidgets.QMainWindow, Ui_MainWindow):
"""
05/21/2024
Mesh Labeler v4.3.1
vedo: 2023.4.6
vtk: 9.2.6
chagned:
1. added filling function ("shift" + left click)
2. added flagepole-type "show label" function (key: "s")
"""
def __init__(self, parent=None):
super(Mesh_Labeler, self).__init__(parent)
self.setupUi(self)
# enable drag and drop operation
self.setAcceptDrops(True)
##################
# set a vtk Widget
##################
sizePolicy = QtWidgets.QSizePolicy(
QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum
)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.frame.sizePolicy().hasHeightForWidth())
self.vtkWidget = QVTKRenderWindowInteractor(self.frame)
self.vtkWidget.setSizePolicy(sizePolicy)
self.vtkWidget.setMinimumSize(QtCore.QSize(580, 430))
self.vtk_verticalLayout = QtWidgets.QVBoxLayout(self.frame)
self.vtk_verticalLayout.addWidget(self.vtkWidget)
self.vp = Plotter(qt_widget=self.vtkWidget)
###################################################################################
# read colormap.csv, resize self.tableWidget, and show colormap in self.tableWidget
###################################################################################
if os.path.exists(os.path.join(os.path.dirname(os.path.abspath(__file__)), "colormap.csv")):
csv_relative_path = "colormap.csv" # launch when developing
elif os.path.exists(os.path.join(os.path.dirname(os.path.abspath(__file__)), "../colormap.csv")):
csv_relative_path = "../colormap.csv" # launch when pynsist
self.colormap_csv = pd.read_csv(
os.path.join(os.path.dirname(os.path.abspath(__file__)), csv_relative_path)
).values # used when pynsist
self.label_id = self.colormap_csv[:, 0] # label value (i.e., int number)
self.label_colormap = self.colormap_csv[:, 1:4] # colormap
self.label_description = self.colormap_csv[:, 4] # label description
self.tableWidget.setColumnWidth(0, 35) # label
self.tableWidget.setColumnWidth(1, 35) # color
self.tableWidget.setColumnWidth(2, 110) # description
self.tableWidget.setRowCount(len(self.label_id)) # update tableWidget
self.vedo_colorlist = []
for i_row in range(len(self.label_id)):
newItem = QtWidgets.QTableWidgetItem("{}".format(self.label_id[i_row]))
self.tableWidget.setItem(i_row, 0, newItem) # column 0: label ID
self.tableWidget.setItem(
i_row, 1, QtWidgets.QTableWidgetItem("")
) # column 1: empty
self.tableWidget.item(i_row, 1).setBackground(
QtGui.QColor(
self.label_colormap[i_row, 0],
self.label_colormap[i_row, 1],
self.label_colormap[i_row, 2],
)
) # column 1: change background based on colormap
self.tableWidget.setItem(
i_row,
2,
QtWidgets.QTableWidgetItem("{}".format(self.label_description[i_row])),
) # column 2: description
self.vedo_colorlist.append(
(self.label_id[i_row], self.label_colormap[i_row])
)
self.vedo_colorlist.append(
(np.max(self.label_id) + 1, [169, 169, 169])
) # add one more label which represents selected cells
self.lut = build_lut(
colorlist=self.vedo_colorlist,
# vmin=0,
# vmax=np.max(self.label_id)+2, # start from zero
# above_color="black",
# below_color="white",
interpolate=False,
)
###########################
# initialize some variables
###########################
self.mesh_exist = False
self.mesh_wireframe_show = False # no wireframe as default
self.opened_mesh_path = os.getcwd()
self.existed_opened_mesh_path = os.getcwd()
self.reset_plotters()
self.spinBox_brush_active_label.setRange(
min(self.label_id), max(self.label_id)
) # set min and max label id for spinBox
self.spinBox_swap_original_label.setRange(
min(self.label_id), max(self.label_id)
)
self.spinBox_swap_new_label.setRange(min(self.label_id), max(self.label_id))
# default active label = 0
self.brush_active_label = [0]
self.swap_original_label = [0]
self.swap_new_label = [0]
# brush method variables
self.brush_mode = False # un-do enable
self.brush_clicked = False
self.brush_selected_pts = [] # un-do enable
self.brush_erased_pts = [] # un-do enable
self.flattened_selected_pt_ids = [] # un-do enable
self.flattened_erased_pt_ids = [] # un-do enable
self.brush_erased_pts = [] # un-do enable
self.selected_cell_ids = [] # un-do enable
self.undo_brush_mode = False
self.undo_brush_selected_pts = [] # un-do enable
self.undo_brush_erased_pts = [] # un-do enable
self.undo_flattened_selected_pt_ids = [] # un-do enable
self.undo_flattened_erased_pt_ids = [] # un-do enable
self.undo_brush_erased_pts = [] # un-do enable
self.undo_selected_cell_ids = [] # un-do enable
self.caption_mode = False
self.caption_meshes = []
self.tooth_legend = []
self.brush_radius = 1.0
self.brush_ball_switch = False # this switch to control to show a transparent ball or not (default false in v4.0)
self.brush_ball = Sphere(
pos=[0, 0, 0], r=self.brush_radius, c="grey", alpha=0.0
).pickable(False)
self.ctrl_pressed = False
# 04/23/2021; new for v3.0 (landmarking)
self.doubleSpinBox_R.setValue(0.5) # default radius = 0.5
self.landmark_radius = self.doubleSpinBox_R.value()
self.landmark_color = "red"
self.selected_landmark_color = "blue"
self.landmark_exist = False
self.vedo_landmarks = []
self.landmarks = []
self.landmark_names = []
self.selected_lmk_idx = None
self.opened_landmarking_path = os.getcwd()
self.existed_opened_landmarking_path = os.getcwd()
self.landmark_selecting = False
self.vedo_landmarks = []
self.tableWidget_landmarking_focus = False
########################
# setup signals (button, tab, and spinBox)
########################
self.pushButton_load.setToolTip("Load a mesh (.vtp, .stl, .obj, or .ply)")
self.pushButton_load.setStyleSheet(
"QPushButton{font: 75 20px}, QToolTip{color: k ; font: 12px}"
) # set different font on tool tip
self.pushButton_save.setToolTip("Save a mesh (.vtp, .stl, or .obj)")
self.pushButton_save.setStyleSheet(
"QPushButton{font: 75 20px}, QToolTip{color: k ; font: 12px}"
) # set different font on tool tip
self.pushButton_load.clicked.connect(self.load_mesh)
self.pushButton_save.clicked.connect(self.save_mesh)
# if changing tab, show tab information
self.tabWidget.currentChanged.connect(
self.show_tab_info
)
self.spinBox_brush_active_label.setKeyboardTracking(False)
self.spinBox_swap_original_label.setKeyboardTracking(False)
self.spinBox_swap_new_label.setKeyboardTracking(False)
self.spinBox_brush_active_label.valueChanged.connect(
self.brush_active_label_changed
)
self.spinBox_swap_original_label.valueChanged.connect(
self.swap_original_label_changed
)
self.spinBox_swap_new_label.valueChanged.connect(self.swap_new_label_changed)
self.pushButton_swap_change.clicked.connect(self.swap_assign_new_label)
# 04/23/2021; new for v3.0
self.pushButton_load_fcsv.clicked.connect(self.load_landmarking)
self.pushButton_save_fcsv.clicked.connect(self.save_landmarking)
self.pushButton_add_landmark.clicked.connect(self.add_new_landmark)
self.pushButton_delete_landmark.clicked.connect(self.delete_landmark)
self.pushButton_rename.clicked.connect(self.rename_landmark)
self.pushButton_relocate.clicked.connect(self.relocate_landmark)
self.pushButton_reset.clicked.connect(self.reset_landmark)
self.tableWidget_landmakring.itemSelectionChanged.connect(
self.selected_landmark_column
)
self.pushButton_move_right.setIcon(
self.style().standardIcon(getattr(QtWidgets.QStyle, "SP_ArrowRight"))
)
self.pushButton_move_right.clicked.connect(self.landmark_order_move_right)
self.pushButton_move_left.setIcon(
self.style().standardIcon(getattr(QtWidgets.QStyle, "SP_ArrowLeft"))
)
self.pushButton_move_left.clicked.connect(self.landmark_order_move_left)
self.doubleSpinBox_R.valueChanged.connect(self.spin_R_changed)
# # get rendering and add callback functions
self.add_callbacks()
self.shortcut_lmk_relocation = QShortcut(QKeySequence("Ctrl+r"), self)
self.shortcut_lmk_relocation.activated.connect(self.shortcut_function_landmark_relocation)
self.shortcut_fcsv_save = QShortcut(QKeySequence("Ctrl+s"), self)
self.shortcut_fcsv_save.activated.connect(self.shortcut_function_fcsv_save)
self.shortcut_toggle_wireframe = QShortcut(QKeySequence("l"), self)
self.shortcut_toggle_wireframe.activated.connect(self.shortcut_function_toggle_wireframe)
self.shortcut_click_right = QShortcut(QKeySequence("right"), self)
self.shortcut_click_right.activated.connect(self.shortcut_function_select_right_landmark)
self.shortcut_click_left = QShortcut(QKeySequence("left"), self)
self.shortcut_click_left.activated.connect(self.shortcut_function_select_left_landmark)
#############
# show window
#############
self.showMaximized()
# self.show()
########################
# general functions
########################
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
def dropEvent(self, event):
files = [u.toLocalFile() for u in event.mimeData().urls()]
for f in files:
self.opened_mesh_path = f
try:
if self.opened_mesh_path[-4:].lower() in [
".vtp",
".stl",
".obj",
".ply",
]: # check file extension for mesh
self.reset_plotters()
self.plot_mesh()
self.existed_opened_mesh_path = self.opened_mesh_path
self.setWindowTitle(
"Mesh Labeler (open source) -- {}".format(
self.existed_opened_mesh_path
)
)
self.vtkWidget.setFocus()
elif self.opened_mesh_path[-5:].lower() in [
".fcsv"
]: # check file extension for landmarking
self.tabWidget.setCurrentIndex(2)
if self.mesh_exist: # only enable when mesh exists
self.opened_landmarking_path = self.opened_mesh_path
# read fcsv file
self.load_fcsv()
if self.landmark_exist: # fcsv load successfullly
self.existed_opened_landmarking_path = (
self.opened_landmarking_path
)
# display landmarking info on tableWidget
self.display_landmark_table()
self.tableWidget_landmakring.setCurrentCell(0, 0) # deafult select the first ladnmark
# plot landmarks
self.vp.remove(
self.vedo_landmarks
) # clean previous landmarks if existing
self.vedo_landmarks = []
for i_landmark in range(len(self.landmarks)):
# self.vedo_landmarks.append(Point(self.landmarks[i_landmark], r=self.landmark_radius).c(self.landmark_color))
self.vedo_landmarks.append(
Sphere(
self.landmarks[i_landmark],
r=self.landmark_radius,
).c(self.landmark_color)
)
self.vp.add(self.vedo_landmarks)
self.setWindowTitle(
"Mesh Labeler (open source) -- {} -- {}".format(
self.existed_opened_mesh_path,
self.existed_opened_landmarking_path,
)
)
self.vtkWidget.setFocus()
else:
self.show_messageBox(
"No mesh available! Please load a mesh first!"
)
except:
None # it won't happen, because we only allow to load four types of mesh file
def onClose(self):
# Disable the interactor before closing to prevent it
# from trying to act on already deleted items
self.vtkWidget.close()
def closeEvent(self, event): # new in v3.0
reply = QMessageBox.question(
self, "Quit", "Are you sure to quit?", QMessageBox.Yes | QMessageBox.No
)
if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()
def add_callbacks(self):
"""
all callback functions to create the GUI interaction
"""
# segmentation
self.vp.add_callback("KeyPressEvent", self.segmentation_keypress)
# in order to get both "press" and "release" events, need to use the original VTK function (i.e., AddObserver)
# self.vp.interactor.AddObserver("RightButtonPressEvent", self.brush_onRightClick)
self.vp.add_callback("RightButtonPressEvent", self.brush_onRightClick)
self.vp.add_callback("MouseMove", self.brush_dragging)
self.vp.interactor.AddObserver(
"RightButtonReleaseEvent", self.brush_onRightRelease
)
self.vp.add_callback("MouseWheelForwardEvent", self.brush_increase_radius)
self.vp.add_callback("MouseWheelBackwardEvent", self.brush_decrease_radius)
self.vp.add_callback("LeftButtonPressEvent", self.brush_filling)
self.vp.interactor.AddObserver("KeyPressEvent", self.press_ctrl)
self.vp.interactor.AddObserver("KeyReleaseEvent", self.release_ctrl)
self.vp.interactor.AddObserver("KeyPressEvent", self.press_shift)
self.vp.interactor.AddObserver("KeyReleaseEvent", self.release_shift)
# landmarking
self.vp.add_callback("LMK_LeftButtonPressEvent", self.landmarking_onLeftClick)
self.vp.add_callback("LMK_RightButtonPressEvent", self.landmarking_onRightClick)
# general
# self.vp.interactor.AddObserver("KeyPressEvent", self.toggle_wireframe)
# self.vp.interactor.AddObserver("KeyPressEvent", self.shortcut_keypress)
def set_mesh_color(self):
self.mesh.cmap(
input_cmap=self.lut,
input_array=self.mesh.celldata["Label"],
on="cells",
n_colors=len(self.vedo_colorlist),
)#.add_scalarbar()
self.mesh.mapper().SetScalarRange(0, np.max(self.label_id) + 2) # keep "np.max(self.label_id) + 1 " for selected color
def plot_mesh(self):
"""
call in load_mesh, using vedo to read mesh file
"""
self.mesh_exist = True # new in v3.0
self.mesh = load(self.opened_mesh_path)
# self.mesh = load("Example_01.vtp")
self.mesh_cms = Points(self.mesh.cell_centers())
self.mesh.linecolor('black').linewidth(
0.0
) # .lighting('off')#.lineColor('black')#.lineWidth(0.1)
self.normals = self.mesh.compute_normals()
# check if the input mesh has cell array 'Label'; if not, assign zero for all cells
if not "Label" in self.mesh.celldata.keys():
self.mesh.celldata["Label"] = np.zeros(
[
self.mesh.ncells,
],
dtype=np.int32,
)
self.set_mesh_color()
# self.mesh.mapper().SetScalarRange(0, len(self.vedo_colorlist))
self.temp_labels = self.mesh.clone().celldata[
"Label"
] # save new labels to backup
self.undo_labels = self.mesh.clone().celldata[
"Label"
] # another backup label for un-do procedure
self.undo_temp_labels = self.mesh.clone().celldata[
"Label"
] # another backup label for un-do procedure
# added by 06/21/2022; fixed the bug if there is another cell array prior to 'Label'
self.mesh.celldata.select("Label")
# self.vp.show(self.mesh, interactorStyle=0)
self.vp.show(self.mesh, interactive=False)
self.show_tab_info() # show tab information in status bar
def reset_plotters(self):
# self.vp.close()
# self.vp = Plotter(qt_widget=self.vtkWidget)
self.vp.clear()
self.brush_mode = False
self.caption_mode = False
self.selected_cell_ids = []
def selected_pt_ids_to_cell_ids(self, selected_ids):
"""
General funciton that return cell_ids when pt_ids are given
"""
selected_cell_ids = []
for i in selected_ids:
selected_cell_ids.append(self.mesh.connected_cells(i, return_ids=True))
flat_list = []
for sublist in selected_cell_ids:
for item in sublist:
flat_list.append(item)
selected_cell_ids = np.unique(np.asarray(flat_list))
return selected_cell_ids
########################
# landmarking functions
########################
def load_fcsv(self): # new in v3.0
try:
LPS = False # LPS coordinate; default: False
# check if it's LPS coordinate system
with open(self.opened_landmarking_path, "r") as f:
line = f.readline()
line = f.readline()
if "LPS" in line:
LPS = True
# reader for RAS coordinate system
data_type = {0: object, 1: np.float32, 2: np.float32, 3: np.float32, 4: np.float32, 5: np.float32, 6: np.float32, 7: np.float32, 8: np.float32, 9: np.float32, 10: np.float32, 11: object, 12: object, 13: object}
header = None # according to 3D slicer format
skiprows = 3 # according to 3D Slicer format
lmk_df = pd.read_csv(
self.opened_landmarking_path, header=header, skiprows=skiprows, dtype=data_type
)
num_landmarks = len(lmk_df)
landmarks = np.zeros([num_landmarks, 3], dtype=np.float32)
landmarks = lmk_df[[1, 2, 3]].values
landmark_names = lmk_df[[11]].values.tolist()
if LPS:
landmarks[:, 0:2] *= -1 # flip the first two axes
self.landmarks = landmarks
self.landmark_names = landmark_names
self.landmark_exist = True
except:
self.show_messageBox(
"Check fcsv format!\nYou might see an example on the repository."
)
def display_landmark_table(self): # new in v3.0
self.tableWidget_landmakring.setRowCount(2) # update tableWidget
self.tableWidget_landmakring.setColumnCount(
len(self.landmarks)
) # update tableWidget
self.tableWidget_landmakring.setRowHeight(0, 30) # header
self.tableWidget_landmakring.setRowHeight(1, 30) # header
# self.tableWidget_landmakring.setItem(0, 0, QtWidgets.QTableWidgetItem("Name"))
# self.tableWidget_landmakring.setItem(1, 0, QtWidgets.QTableWidgetItem("Position"))
for i_col in range(len(self.landmarks)):
self.tableWidget_landmakring.setItem(
0,
i_col,
QtWidgets.QTableWidgetItem("{}".format(self.landmark_names[i_col][0])),
)
self.tableWidget_landmakring.setItem(
1,
i_col,
QtWidgets.QTableWidgetItem(
"({:.2f}, {:.2f}, {:.2f})".format(
self.landmarks[i_col, 0],
self.landmarks[i_col, 1],
self.landmarks[i_col, 2],
)
),
)
def selected_landmark_column(self): # new in v3.0
idx = self.tableWidget_landmakring.selectedIndexes()
try:
col_idx = idx[0].column()
self.selected_lmk_idx = col_idx
# reset to red for all landmarks
for i in range(len(self.vedo_landmarks)):
self.vedo_landmarks[i].c(self.landmark_color)
# show selection
if self.selected_lmk_idx != None:
self.vedo_landmarks[self.selected_lmk_idx].c(
self.selected_landmark_color
)
self.vp.render(resetcam=False)
except:
self.selected_lmk_idx = None # idx == []
def rename_landmark(self):
if self.landmark_exist:
if self.selected_lmk_idx != None: # selection cannot be empty
new_name, done = QtWidgets.QInputDialog.getText(
self, "Rename", "Enter the new landmark name:"
)
if done:
self.landmark_names[self.selected_lmk_idx] = [new_name]
self.display_landmark_table()
def relocate_landmark(self):
if self.landmark_exist:
if self.selected_lmk_idx != None: # selection cannot be empty
if self.tabWidget.currentIndex() == 2: # in landmarking mode
self.landmark_selecting = True
def reset_landmark(self):
if self.landmark_exist:
if self.selected_lmk_idx != None: # selection cannot be empty
if self.tabWidget.currentIndex() == 2: # in landmarking mode
self.landmarks[self.selected_lmk_idx] = np.array([np.nan, np.nan, np.nan])
# update landmark plot and table
self.display_landmark_table()
self.vp.remove(self.vedo_landmarks)
self.vedo_landmarks[self.selected_lmk_idx] = Sphere(
self.landmarks[self.selected_lmk_idx], r=self.landmark_radius
).c(self.selected_landmark_color)
self.vp.add(self.vedo_landmarks)
self.vp.render(resetcam=False)
def landmarking_onLeftClick(self, evt):
if self.tabWidget.currentIndex() == 2: # in landmarking mode
if self.landmark_selecting:
# assign new landmark position
self.landmarks[self.selected_lmk_idx] = self.mesh.picked3d
# update landmark plot and table
self.display_landmark_table()
self.vp.remove(self.vedo_landmarks)
self.vedo_landmarks[self.selected_lmk_idx] = Sphere(
self.landmarks[self.selected_lmk_idx], r=self.landmark_radius
).c(self.selected_landmark_color)
self.vp.add(self.vedo_landmarks)
self.vp.render(resetcam=False)
self.landmark_selecting = False
def landmarking_onRightClick(self, evt):
if self.tabWidget.currentIndex() == 2: # in landmarking mode
self.tableWidget_landmakring.clearSelection()
self.landmark_selecting = False
# reset to red for all landmarks
for i in range(len(self.vedo_landmarks)):
self.vedo_landmarks[i].c(self.landmark_color)
def add_new_landmark(self):
if self.landmark_exist: # add a new landmark on the existing landmark table
if len(self.landmarks) > 0:
new_name, done = QtWidgets.QInputDialog.getText(
self, "Add landmark", "Enter a name for the new landmark:"
)
if done:
# add new name
self.landmark_names.append([new_name])
# add new location
new_location = np.zeros([1, 3]) # default origin
self.landmarks = np.concatenate(
(self.landmarks, new_location), axis=0
)
# create new vedo_landmark
# self.vedo_landmarks.append(Point(self.landmarks[-1], r=self.landmark_radius).c(self.landmark_color))
self.vedo_landmarks.append(
Sphere(self.landmarks[-1], r=self.landmark_radius).c(
self.landmark_color
)
)
self.display_landmark_table()
self.tableWidget_landmakring.selectColumn(
len(self.landmarks) - 1
) # select last column (new)
else: # create a new landmark table
if self.mesh_exist: # lock this function until mesh is existed
new_name, done = QtWidgets.QInputDialog.getText(
self, "Add landmark", "Enter a name for the new landmark:"
)
if done:
# add new name
self.landmark_names.append([new_name])
# add new location
self.landmarks = np.zeros([1, 3]) # default origin
# create new vedo_landmark
# self.vedo_landmarks.append(Point(self.landmarks[-1], r=self.landmark_radius).c(self.landmark_color))
self.vedo_landmarks.append(
Sphere(self.landmarks[-1], r=self.landmark_radius).c(
self.landmark_color
)
)
self.landmark_exist = True # turn on landmark_exist
self.display_landmark_table()
self.tableWidget_landmakring.selectColumn(0) # select column 0
def delete_landmark(self):
if self.landmark_exist: # add a new landmark on the existing landmark table
if self.selected_lmk_idx != None: # selection cannot be empty
buttonReply = QMessageBox.question(
self,
"Delete",
"Do you want to delete?",
QMessageBox.Yes | QMessageBox.No,
)
if buttonReply == QMessageBox.Yes:
if len(self.landmark_names) > 1: # multiple landmarks
del self.landmark_names[
self.selected_lmk_idx
] # remove self.landmark_names
self.landmarks = np.delete(
self.landmarks, self.selected_lmk_idx, 0
) # remove self.landmarks
self.vp.remove(
self.vedo_landmarks[self.selected_lmk_idx]
) # remove plot
self.vp.render()
del self.vedo_landmarks[
self.selected_lmk_idx
] # remove vedo_landmarks
else: # only a landmark
self.vp.remove(
self.vedo_landmarks[self.selected_lmk_idx]
) # remove plot
self.vp.render()
# reset everything
self.landmark_exist = False
self.vedo_landmarks = []
self.landmarks = []
self.landmark_names = []
self.selected_lmk_idx = None
self.landmark_selecting = False
self.vedo_landmarks = []
self.display_landmark_table()
self.tableWidget_landmakring.clearSelection() # clean selection
self.selected_lmk_idx = None # reset to no select_idx
def landmark_order_move_right(self):
if self.landmark_exist: # add a new landmark on the existing landmark table
if self.selected_lmk_idx != None: # selection cannot be empty
if (
self.selected_lmk_idx != len(self.landmarks) - 1
): # not the most right
# list[pos1], list[pos2] = list[pos2], list[pos1]
(
self.landmark_names[self.selected_lmk_idx],
self.landmark_names[self.selected_lmk_idx + 1],
) = (
self.landmark_names[self.selected_lmk_idx + 1],
self.landmark_names[self.selected_lmk_idx],
)
self.landmarks[
[self.selected_lmk_idx, self.selected_lmk_idx + 1]
] = self.landmarks[
[self.selected_lmk_idx + 1, self.selected_lmk_idx]
]
(
self.vedo_landmarks[self.selected_lmk_idx],
self.vedo_landmarks[self.selected_lmk_idx + 1],
) = (
self.vedo_landmarks[self.selected_lmk_idx + 1],
self.vedo_landmarks[self.selected_lmk_idx],
)
self.selected_lmk_idx += 1
self.tableWidget_landmakring.selectColumn(self.selected_lmk_idx)
self.vp.render()
self.display_landmark_table()
def landmark_order_move_left(self):
if self.landmark_exist: # add a new landmark on the existing landmark table
if self.selected_lmk_idx != None: # selection cannot be empty
if self.selected_lmk_idx != 0: # not the most left
# list[pos1], list[pos2] = list[pos2], list[pos1]
(
self.landmark_names[self.selected_lmk_idx],
self.landmark_names[self.selected_lmk_idx - 1],
) = (
self.landmark_names[self.selected_lmk_idx - 1],
self.landmark_names[self.selected_lmk_idx],
)
self.landmarks[
[self.selected_lmk_idx, self.selected_lmk_idx - 1]
] = self.landmarks[
[self.selected_lmk_idx - 1, self.selected_lmk_idx]
]
(
self.vedo_landmarks[self.selected_lmk_idx],
self.vedo_landmarks[self.selected_lmk_idx - 1],
) = (
self.vedo_landmarks[self.selected_lmk_idx - 1],
self.vedo_landmarks[self.selected_lmk_idx],
)
self.selected_lmk_idx -= 1
self.tableWidget_landmakring.selectColumn(self.selected_lmk_idx)
self.vp.render()
self.display_landmark_table()
########################
# labeling functions
########################
def undo_backup(self):
"""
step 1 to perform undo process (ctrl + z)
"""
# save all current status into backup
self.undo_brush_mode = self.brush_mode
self.undo_brush_selected_pts = self.brush_selected_pts.copy()
self.undo_brush_erased_pts = self.brush_erased_pts.copy()
self.undo_flattened_selected_pt_ids = self.flattened_selected_pt_ids.copy()
self.undo_flattened_erased_pt_ids = self.flattened_erased_pt_ids.copy()
self.undo_brush_erased_pts = self.brush_erased_pts.copy()
self.undo_selected_cell_ids = self.selected_cell_ids.copy()
self.undo_labels = self.mesh.celldata[
"Label"
].copy() # backup label for un-do procedure
self.undo_temp_labels = (
self.temp_labels.copy()
) # backup temp_label for un-do procedure
def undo_recover(self):
"""
step 2 to perform undo process (ctrl + z)
"""
# recover all variables by the undo_backup
self.brush_mode = self.undo_brush_mode
self.brush_selected_pts = self.undo_brush_selected_pts.copy()
self.brush_erased_pts = self.undo_brush_erased_pts.copy()
self.flattened_selected_pt_ids = self.undo_flattened_selected_pt_ids.copy()
self.flattened_erased_pt_ids = self.undo_flattened_erased_pt_ids.copy()
self.brush_erased_pts = self.undo_brush_erased_pts.copy()
self.selected_cell_ids = self.undo_selected_cell_ids.copy()
self.mesh.celldata["Label"] = self.undo_labels.copy()
self.temp_labels = self.undo_temp_labels.copy()
self.vp.show(self.mesh, resetcam=False)
def assign_active_label_to_selection(self):
if self.tabWidget.currentIndex() == 0: # in segmentation mode
self.mesh.celldata["Label"][
self.selected_cell_ids
] = self.brush_active_label[0]
self.temp_labels = self.mesh.clone().celldata[
"Label"
] # save new labels to backup labels
self.set_mesh_color()
self.vp.show(self.mesh, resetcam=False)
def clean_segmentation_selection(self):
try:
if self.brush_ball_switch:
self.brush_ball.__init__(
pos=[0, 0, 0], r=self.brush_radius, c="grey", alpha=0.0
)
self.vp.remove("tmp_select")
self.vp.render(resetcam=False)
self.brush_selected_pts = []
self.brush_erased_pts = []
self.flattened_selected_pt_ids = []
self.flattened_erased_pt_ids = []
self.selected_cell_ids = []
self.brush_mode = False
self.mesh.celldata["Label"] = self.temp_labels # restore temp_labels
self.set_mesh_color()
self.vp.show(self.mesh, resetcam=False)
except:
None # to prevent crash before loading mesh and change to One-Way Swap Method
def segmentation_keypress(self, evt):
if self.tabWidget.currentIndex() == 0: # if in segmentation mode
if evt.keypress in ["b", "B"]:
self.brush_mode = not self.brush_mode # toggle brush mode
if self.brush_mode == True:
self.brush_selected_pts = []
self.flattened_selected_pt_ids = []
self.selected_cell_ids = []
self.ctrl_pressed = False
self.brush_erased_pts = []
self.flattened_erased_pt_ids = []
if self.brush_mode == False:
self.clean_segmentation_selection()
self.instruction = "Brush Mode: {}".format(self.brush_mode)
self.statusBar().showMessage(self.instruction)
elif evt.keypress in ["e", "E"]: # click 'e' to execute
if (
len(self.selected_cell_ids) != 0
): # as long as there has a selected cell
# undo backup
self.undo_backup()
self.assign_active_label_to_selection()
# reset everything, but still in brush mode
self.brush_clicked = False
self.ctrl_pressed = False
self.brush_selected_pts = []
self.brush_erased_pts = []
self.flattened_selected_pt_ids = []
self.flattened_erased_pt_ids = []
self.selected_cell_ids = []
self.mesh.celldata[
"Label"
] = self.temp_labels # restore temp_labels
self.set_mesh_color()
self.vp.show(self.mesh, resetcam=False)
# show instruction
self.instruction = "Execution is completed"
self.statusBar().showMessage(self.instruction)
elif evt.keypress in ["c", "C"]: # click c to clean all selection
# undo backup
self.undo_backup()
self.clean_segmentation_selection()
self.instruction = "Clean everything"
self.statusBar().showMessage(self.instruction)
elif evt.keypress in ["s", "S"]: # click s to show label
self.caption_mode = not self.caption_mode # toggle brush mode
if self.caption_mode:
unique_labels = np.unique(self.mesh.celldata["Label"])
for i_label in unique_labels:
if i_label != 0:
i_seg = self.mesh.clone().threshold('Label', above=i_label-0.5, below=i_label+0.5, on='cells').alpha(0)
self.caption_meshes.append(i_seg)
if i_seg.ncells > 0:
i_cap = i_seg.caption(
f"{int(i_label)}",
point=i_seg.center_of_mass(),
size=(0.3, 0.06),
padding=0,
font="VictorMono",
alpha=1,)
i_cap.name = f"cap_{i_label}" # <-- this is assigned name for i_seg instead of i_cap
self.tooth_legend.append(i_cap)
self.vp.add(self.tooth_legend).render()
else:
self.vp.remove(self.caption_meshes)
self.vp.remove(self.tooth_legend)
self.vp.render()
# reset
self.caption_meshes = []
self.tooth_legend = []
def brush_onRightClick(self, evt):
if self.tabWidget.currentIndex() == 0: # if in segmentation mode
if self.brush_mode == True:
if not self.brush_clicked:
self.brush_clicked = (
True # this will tricker mouse move call_back function
)
self.vp.show(resetcam=False)
# update and show brush ball (the first action when clicking)
p = evt.picked3d
if p is None:
return
tmp_pt = self.mesh.closest_point(
p, radius=self.brush_radius, return_point_id=True
)
tmp_cells = self.selected_pt_ids_to_cell_ids(tmp_pt)
if len(tmp_cells) > 0:
mesh_center_points = self.mesh.cell_centers()
tmp_cell_pts = mesh_center_points[tmp_cells]
tmp_pts = Points(tmp_cell_pts).c("red").pickable(False)
tmp_pts.name = "tmp_select"
self.vp.remove("tmp_select").add(tmp_pts).render()
if self.brush_ball_switch:
tmp_pt = self.mesh.closest_point(p, n=1)
self.brush_ball.__init__(
pos=tmp_pt, r=self.brush_radius, c="grey", alpha=0.3
)
self.vp.add(self.brush_ball).render()
# undo backup
self.undo_backup()
# show selected area
pt_ids = self.mesh.closest_point(
p, radius=self.brush_radius, return_point_id=True
)
# collect all selected pts
if self.ctrl_pressed == False: # if no ctrl pressed
self.brush_selected_pts = list(self.flattened_selected_pt_ids)
for i in pt_ids:
if i not in self.brush_selected_pts:
self.brush_selected_pts.append(i)
self.flattened_selected_pt_ids = np.asarray(
self.brush_selected_pts, dtype=np.int32
)
else: # if ctrl pressed
self.brush_erased_pts = list(self.flattened_erased_pt_ids)
for i in pt_ids:
if i not in self.brush_erased_pts:
self.brush_erased_pts.append(i)
self.flattened_erased_pt_ids = np.asarray(
self.brush_erased_pts, dtype=np.int32
)
# final selection = selected pts - erased pts
self.flattened_selected_pt_ids = np.asarray(
[
i
for i in list(self.flattened_selected_pt_ids)
if i not in list(self.flattened_erased_pt_ids)
],
dtype=np.int32,
)
# convert final selected pt_ids -> selected cell_ids
self.selected_cell_ids = self.selected_pt_ids_to_cell_ids(
self.flattened_selected_pt_ids
)
self.mesh.celldata["Label"] = self.temp_labels
if len(self.selected_cell_ids) > 0: # avoid IndexError
self.mesh.celldata["Label"][self.selected_cell_ids] = (
np.max(self.label_id) + 1
) # assign selection as the selection color (i.e., np.max(self.label_id) + 1)
self.set_mesh_color()
self.vp.show(self.mesh, resetcam=False)
def brush_onRightRelease(self, iren, evt):
if self.tabWidget.currentIndex() == 0: # if in segmentation mode
if self.brush_mode == True:
if self.brush_clicked:
# turn off brush_clicked
self.brush_clicked = (
False # this will stop mouse move call_back function
)
def brush_increase_radius(self, evt):
if self.tabWidget.currentIndex() == 0: # if in segmentation mode
if self.brush_mode == True:
if self.ctrl_pressed:
self.brush_radius += 0.1
self.vp.show(resetcam=False)
# show brush ball
p = evt.picked3d
if p is None:
return
tmp_pt = self.mesh.closest_point(
p, radius=self.brush_radius, return_point_id=True
)
tmp_cells = self.selected_pt_ids_to_cell_ids(tmp_pt)
if len(tmp_cells) > 0:
mesh_center_points = self.mesh.cell_centers()
tmp_cell_pts = mesh_center_points[tmp_cells]
tmp_pts = Points(tmp_cell_pts).c("red").pickable(False)
tmp_pts.name = "tmp_select"
self.vp.remove("tmp_select").add(tmp_pts).render()
if self.brush_ball_switch:
tmp_pt = self.mesh.closest_point(p, n=1)
self.brush_ball.__init__(
pos=tmp_pt, r=self.brush_radius, c="grey", alpha=0.3