-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathframeSeg.py
More file actions
1427 lines (1200 loc) · 61.3 KB
/
frameSeg.py
File metadata and controls
1427 lines (1200 loc) · 61.3 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
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
# -*- coding: utf-8 -*-
import cv2
import os
import numpy as np
import pickle
import sys
import time
import threading
import json
import wx
import wx.xrc
"""
ids
76,77 - left/right
78 - slider slice
79 - num slice combo box
80 - erase
81 - remove last vertex
82 - contrast slider
83 - save all
84 - save current polygon/contour
85 - contour drawing style
86 - brush drawing style
87 - brush size
88 - view axis
89 - delete brush
90-xxx classes
201 - send to back
202 - undo last polygon
203 - select previous polygon
204 - select next polygon
205 - back to segmentation
206 - change class dropdown
207 - change class button
208 - Previous
209 - Next
210 - Hide segmentation
"""
# global vars
SEG_PATH_IN = ''
SEG_PATH_OUT = ''
FILE_PATH = []
PREV_FILE_PATH = []
IMAGE_WIDTH = 0
IMAGE_HEIGHT = 0
IMAGE_EXTENSION = ''
NUM_SLICES = 0
current_image = []
image_np_all_slices = []
image_np_all_slices_normed = []
image_np_all_slices_segmentation = []
image_np_all_slices_normed_plot = []
image_np_all_slices_segmentation_plot = []
polygons = []
polygons_to_be_deleted = []
current_polygon_to_be_deleted_idx = 0
num_polygons = 0
old_area_list = []
current_operation = []
operation_changed = True
START_BUTTON_ID_CLASSES = 90
FIRST_CLICK = False
HIDE_SEGMENTATION = False
image_previous_segmentation_overlaid = []
try:
strings = json.load(open("languageConfig.yaml"))
except:
pass
def _(s):
try:
return strings[s]
except Exception as e:
return s
try:
classConfigLines = open("classConfig.yaml").read().splitlines()
except Exception as e:
print('No/corruped classConfig.yaml file. Correct format: {className} {R} {G} {B} , where R,G,B range from 0..255')
app = wx.App(None)
style = wx.OK
dialog = wx.MessageDialog(None, 'Missing/corrupted classConfig.yaml file. Correct format: {className} {R} {G} {B} , where R,G,B range from 0..255', 'Error', style)
dialogResult = dialog.ShowModal()
dialog.destroy()
sys.exit(1)
#dialog.SetYesNoCancelLabels(_("Segment image"), _("Edit segmented image"), _("Cancel"))
CLASS_NAMES = []
CLASS_COLORS_RGB = []
for line in classConfigLines:
x=line.split(' ')
CLASS_NAMES.append(x[0])
CLASS_COLORS_RGB.append((int(x[1]), int(x[2]), int(x[3])))
operations_list = ['segment', 'edit']
operations_list_translated = [_('segment'), _('edit')]
BORDER_PADDING = 70
NUM_CLASSES = len(CLASS_NAMES)
# helpers / GUI
def selectOperation():
# style = wx.YES_NO | wx.ICON_QUESTION | wx.CANCEL | wx.HELP
style = wx.YES_NO | wx.ICON_QUESTION | wx.CANCEL
dialog = wx.MessageDialog(None, _('Select operation'), _('Image segmentation'), style)
# argh, wx 2.9+ :/
try:
dialog.SetYesNoCancelLabels(_("Segment image"), _("Edit segmented image"), _("Cancel"))
dialog.SetHelpLabel(_("Quit"))
except:
dialog.setMessage(_("Cancel"))
dialogResult = dialog.ShowModal()
result = []
if dialogResult == wx.ID_YES:
result = 'segment'
elif dialogResult == wx.ID_NO:
result = 'edit'
else:
result = 'quit'
dialog.Destroy()
return result
def selectFolder():
style = wx.DD_DIR_MUST_EXIST | wx.DD_CHANGE_DIR
# dialog = wx.DirDialog(None, "Please choose image directory", style)
dialog = wx.DirDialog(None, "Please choose image directory", style=style)
if dialog.ShowModal() == wx.ID_OK:
result = dialog.GetPath()
else:
result = ""
dialog.Destroy()
return result
def getSingleFilePath(wildcard):
style = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST
dialog = wx.FileDialog(None, _('Open image'), wildcard=wildcard, style=style)
if dialog.ShowModal() == wx.ID_OK:
path = dialog.GetPath()
else:
path = None
dialog.Destroy()
return path
def bbox_np(img):
rows = np.any(img, axis=1)
cols = np.any(img, axis=0)
rmin, rmax = np.where(rows)[0][[0, -1]]
cmin, cmax = np.where(cols)[0][[0, -1]]
return rmin, rmax, cmin, cmax
def drawSegmentationPolys(fromArea=0, current_seg=0, current_class=0, send_to_back=False, limits=(0, 0, 0, 0)):
polygons_area = [item[0] for item in polygons]
min_area = min(polygons_area)
if min_area >= fromArea:
# print('here!', min_area, fromArea)
# print(polygons)
# burn in only last one
for idx_class in range(NUM_CLASSES):
image_np_all_slices_segmentation[limits[0]:limits[1], limits[2]:limits[3], idx_class][
np.where(current_seg[limits[0]:limits[1], limits[2]:limits[3]] > 0)] = 0
image_np_all_slices_segmentation[limits[0]:limits[1], limits[2]:limits[3], current_class] = np.logical_or(
image_np_all_slices_segmentation[limits[0]:limits[1], limits[2]:limits[3], current_class],
current_seg[limits[0]:limits[1], limits[2]:limits[3]])
else: # redraw from fromArea
for poly in polygons:
if poly[0] > fromArea:
continue
bw_poly = cv2.fillPoly(np.zeros((poly[3][1] - poly[3][0], poly[3][3] - poly[3][2])), [poly[2]], 255, 8, 0,
(-poly[3][2], -poly[3][0]))
for idx_class in range(NUM_CLASSES):
image_np_all_slices_segmentation[poly[3][0]:poly[3][1], poly[3][2]:poly[3][3], idx_class][
np.where(bw_poly > 0)] = 0
image_np_all_slices_segmentation[:, :, poly[1]][poly[3][0]:poly[3][1],
poly[3][2]:poly[3][3]] = np.logical_or(
image_np_all_slices_segmentation[poly[3][0]:poly[3][1], poly[3][2]:poly[3][3], poly[1]], bw_poly)
def redrawSegmentationPolys(polygonss, highlighted=None):
global image_np_all_slices_segmentation
highlighted_poly = []
if highlighted != None:
highlighted_poly = polygonss[highlighted]
polygonss = sorted(polygonss, reverse=True, key=lambda x: x[0])
if highlighted != None:
highlighted = polygonss.index(highlighted_poly)
image_np_all_slices_segmentation = np.zeros((IMAGE_HEIGHT, IMAGE_WIDTH, NUM_CLASSES), dtype=np.bool_)
for idx_current_poly, poly in enumerate(polygonss):
if highlighted != None:
if idx_current_poly == highlighted:
bw_poly = cv2.fillPoly(np.zeros((poly[3][1] - poly[3][0], poly[3][3] - poly[3][2])), [poly[2]], 255, 8,
0,
(-poly[3][2], -poly[3][0]))
else:
bw_poly = cv2.drawContours(np.zeros((poly[3][1] - poly[3][0], poly[3][3] - poly[3][2])), [poly[2]], 0,
255, 5, 8, 0, 0, (-poly[3][2], -poly[3][0]))
else:
bw_poly = cv2.fillPoly(np.zeros((poly[3][1] - poly[3][0], poly[3][3] - poly[3][2])), [poly[2]], 255, 8, 0,
(-poly[3][2], -poly[3][0]))
#for idx_class in range(NUM_CLASSES):
image_np_all_slices_segmentation[poly[3][0]:poly[3][1], poly[3][2]:poly[3][3], :][np.where(bw_poly > 0)] = 0
image_np_all_slices_segmentation[:, :, poly[1]][poly[3][0]:poly[3][1], poly[3][2]:poly[3][3]] = np.logical_or(
image_np_all_slices_segmentation[poly[3][0]:poly[3][1], poly[3][2]:poly[3][3], poly[1]], bw_poly)
def plotAnnotationsOverlay():
rgb_overlay = plotAnnotations()
img_out = np.zeros_like(rgb_overlay)
if not HIDE_SEGMENTATION:
cv2.addWeighted(rgb_overlay, 0.5, image_np_all_slices_normed, 1, 0, img_out)
else:
img_out = image_np_all_slices_normed
return img_out
def plotAnnotations():
rgb_overlay = np.zeros_like(image_np_all_slices_normed)
for idx_class in range(NUM_CLASSES):
rgb_overlay[image_np_all_slices_segmentation[:, :, idx_class] > 0] = CLASS_COLORS_RGB[idx_class]
# print(np.count_nonzero(rgb_overlay))
return rgb_overlay
def plotAnnotationsOverlay2(image_np_all_slices_normed_inside, image_np_all_slices_segmentation_inside):
rgb_overlay = plotAnnotations2(image_np_all_slices_normed_inside, image_np_all_slices_segmentation_inside)
img_out = np.zeros_like(rgb_overlay)
cv2.addWeighted(rgb_overlay, 0.5, image_np_all_slices_normed_inside, 1, 0, img_out)
return img_out
def plotAnnotations2(image_np_all_slices_normed_inside, image_np_all_slices_segmentation_inside):
rgb_overlay = np.zeros_like(image_np_all_slices_normed_inside)
labels_for_drawing = []
for idx_class in range(NUM_CLASSES):
rgb_overlay[image_np_all_slices_segmentation_inside[:, :, idx_class] > 0] = CLASS_COLORS_RGB[idx_class][::-1]
return rgb_overlay
# other helpers
def make_unique(original_list):
unique_list = []
map(lambda x: unique_list.append(x) if (x not in unique_list) else False, original_list)
return unique_list
def removePolygonDuplicates(old_polygons):
# add : polygons.append([current_area, self.selected_class, approxContourPoints, (rmin, rmax, cmin, cmax)])
new_polygons = []
for current_old_polygon in old_polygons:
is_duplicate = False
for current_new_polygon in new_polygons:
if current_new_polygon[0] == current_old_polygon[0] and current_new_polygon[3][0] == current_old_polygon[3][0] and current_new_polygon[3][1] == current_old_polygon[3][1] and current_new_polygon[3][2] == current_old_polygon[3][2] and current_new_polygon[3][3] == current_old_polygon[3][3]:
is_duplicate = True
break
if not is_duplicate:
new_polygons.append(current_old_polygon)
new_polygons = sorted(new_polygons, key=lambda x: x[0])
return new_polygons
###########################################################################
## Python code generated with wxFormBuilder (version Jan 23 2018)
## http://www.wxformbuilder.org/
##
## PLEASE DO *NOT* EDIT THIS FILE!
###########################################################################
class PleaseWaitFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, id=wx.ID_ANY, title=wx.EmptyString, pos=wx.DefaultPosition,
size=wx.Size(500, 300), style=0 | wx.TAB_TRAVERSAL)
self.SetSizeHints(wx.DefaultSize, wx.DefaultSize)
self.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNHIGHLIGHT))
bSizer2 = wx.BoxSizer(wx.VERTICAL)
bSizer2.Add((0, 0), 1, wx.EXPAND, 5)
self.m_staticText2 = wx.StaticText(self, wx.ID_ANY, u"Așteptați...", wx.DefaultPosition, wx.DefaultSize, 0)
self.m_staticText2.Wrap(-1)
bSizer2.Add(self.m_staticText2, 0, wx.ALL | wx.ALIGN_CENTER_HORIZONTAL, 5)
bSizer2.Add((0, 0), 1, wx.EXPAND, 5)
self.SetSizer(bSizer2)
self.Layout()
self.Centre(wx.BOTH)
def __del__(self):
pass
###########################################################################
## Class drawing_panel
###########################################################################
class drawing_panel(wx.Panel):
def __init__(self, parent, original_image, drawing_style, brush_size):
global operation_changed
operation_changed = False
self.parent = parent
self.brushSize = brush_size
# self.original_image = original_image.Rotate90(False)
self.original_image = original_image
self.resized_bitmap = original_image.ConvertToBitmap()
self.contourPoints = []
self.old_area = 0
wx.Panel.__init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition, size=wx.Size(800, 800),
style=wx.TAB_TRAVERSAL)
self.isDrawing = False
self.drawingStyle = drawing_style
self.dc = wx.ClientDC(self)
self.gc = wx.GraphicsContext.Create(self.dc)
self.selected_class = 0
self.Bind(wx.EVT_MOVE, self.OnMove)
self.Bind(wx.EVT_MOTION, self.OnMouseMove)
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_SIZE, self.OnResize)
self.Bind(wx.EVT_LEFT_DOWN, self.startDrawing)
self.Bind(wx.EVT_LEFT_UP, self.endDrawing)
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
def paint(self, event=None):
# print("paint")
dc = wx.PaintDC(self)
dc.DrawBitmap(self.resized_bitmap, 0, 0, False);
def OnMove(self, e):
x, y = e.GetPosition()
# print("current window position x = ", x, " y= ", y)
def OnMouseMove(self, e):
if current_operation != 'edit':
x, y = e.GetPosition()
if self.isDrawing and self.drawingStyle == 'contour':
self.contourPoints.append([x, y])
if len(self.contourPoints) > 1:
for idx, point in enumerate(self.contourPoints):
if idx == len(self.contourPoints) - 1: # last point
continue
# self.gc.DrawLines(self.contourPoints[-2:-1])
self.dc.DrawLine(self.contourPoints[-1][0], self.contourPoints[-1][1],
self.contourPoints[-2][0], self.contourPoints[-2][1])
elif self.isDrawing and self.drawingStyle == 'brush':
self.contourPoints.append([x, y])
# dc.SetBrush(wx.Brush("red", wx.SOLID))
# dc.SetPen(wx.Pen("red", self.brushSize, style=wx.SOLID))
if len(self.contourPoints) > 1:
self.dc.DrawLine(self.contourPoints[-1][0], self.contourPoints[-1][1],
self.contourPoints[-2][0], self.contourPoints[-2][1])
# write seg @ mouse up
elif self.isDrawing and self.drawingStyle == 'brushDelete':
self.contourPoints.append([x, y])
if len(self.contourPoints) > 1:
# self.dc.DrawLine(self.contourPoints[-1][0], self.contourPoints[-1][1],
# self.contourPoints[-2][0], self.contourPoints[-2][1])
# self.dc = wx.ClientDC(self) #batman
self.gc.DrawLines(self.contourPoints)
# write seg @ mouse up
# current_segmentation = self.getCurrentSegmentation()
# print(np.count_nonzero(current_segmentation == [255,255,255]))
# dc.DrawCircle(x, y, 1)
# print("current panel mouse position x = ", x, " y= ", y)
def changeImage(self, new_image):
self.dc = wx.ClientDC(self) # batman
self.gc = wx.GraphicsContext.Create(self.dc) # batman
self.original_image = new_image
# self.original_image = new_image
self.redrawImage()
def OnPaint(self, event):
self.redrawImage()
def OnResize(self, event):
self.redrawImage()
self.dc.SetPen(wx.Pen(wx.Colour(CLASS_COLORS_RGB[self.selected_class]), style=wx.SOLID))
self.gc.SetBrush(wx.Brush(
wx.Colour(CLASS_COLORS_RGB[self.selected_class][0], CLASS_COLORS_RGB[self.selected_class][1],
CLASS_COLORS_RGB[self.selected_class][2], 125), wx.SOLID))
new_w, new_h = self.dc.GetSize()
rescaled_contour_points = []
for point in self.contourPoints:
rescaled_contour_points.append([int(point[0] * IMAGE_WIDTH / new_w), int(point[1] * IMAGE_HEIGHT / new_h)])
self.contourPoints = rescaled_contour_points
def OnCloseWindow(self, e):
global operation_changed
operation_changed = False
def redrawImage(self):
self.dc = wx.ClientDC(self) # batman
new_w, new_h = self.dc.GetSize()
if new_w != 0 and new_h != 0:
self.resized_bitmap = self.original_image.Scale(new_w, new_h).ConvertToBitmap()
self.dc.DrawBitmap(self.resized_bitmap, 0, 0);
def startDrawing(self, e):
if current_operation != 'edit':
x, y = e.GetPosition()
self.isDrawing = True
self.contourPoints.append([x, y])
if self.isDrawing and self.drawingStyle == 'contour':
self.dc.SetPen(wx.Pen(wx.Colour(CLASS_COLORS_RGB[self.selected_class]), style=wx.SOLID))
self.gc.SetBrush(wx.Brush(
wx.Colour(CLASS_COLORS_RGB[self.selected_class][0], CLASS_COLORS_RGB[self.selected_class][1],
CLASS_COLORS_RGB[self.selected_class][2], 125), wx.SOLID))
# self.gc.BeginLayer(125)
elif self.isDrawing and self.drawingStyle == 'brushDelete':
self.dc.SetBrush(wx.Brush("white", wx.SOLID))
self.dc.SetPen(wx.Pen("white", self.brushSize, style=wx.SOLID))
elif self.isDrawing and self.drawingStyle == 'brush':
self.dc.SetBrush(wx.Brush("red", wx.SOLID))
self.dc.SetPen(wx.Pen("red", self.brushSize, style=wx.SOLID))
def endDrawing(self, e=None):
global FIRST_CLICK
global polygons_to_be_deleted
if FIRST_CLICK:
FIRST_CLICK = False
return
if current_operation != 'edit':
# x, y = e.GetPosition()
self.isDrawing = False
# this should change depending on the drawing type but, hell, pre-alpha
if self.drawingStyle == 'points':
# draw points
self.redrawImage()
dc = wx.ClientDC(self)
# dc.BeginDrawing()
dc.SetPen(wx.Pen("green", style=wx.SOLID))
dc.SetBrush(wx.Brush("green", wx.SOLID))
for point in self.contourPoints:
dc.DrawCircle(point[0], point[1], 3)
if len(self.contourPoints) > 2:
# ch = ConvexHull(np.array(self.contourPoints))
# Get the indices of the hull points.
# hull_indices = ch.vertices
# These are the actual points.
# hull_pts = self.contourPoints[hull_indices, :]
# draw lines
dc.SetPen(wx.Pen(CLASS_COLORS_RGB[self.selected_class], style=wx.SOLID))
for idx, current_hull_index in enumerate(hull_indices):
if idx == len(hull_indices) - 1:
dc.DrawLine(self.contourPoints[hull_indices[idx]][0],
self.contourPoints[hull_indices[idx]][1],
self.contourPoints[hull_indices[0]][0], self.contourPoints[hull_indices[0]][1])
else:
dc.DrawLine(self.contourPoints[hull_indices[idx]][0],
self.contourPoints[hull_indices[idx]][1],
self.contourPoints[hull_indices[idx + 1]][0],
self.contourPoints[hull_indices[idx + 1]][1])
# plt.plot(pts[:, 0], pts[:, 1], 'ko', markersize=10)
# plt.fill(hull_pts[:, 0], hull_pts[:, 1], fill=False, edgecolor='b')
elif self.drawingStyle == 'contour':
# draw last point
self.redrawImage()
# dc = wx.ClientDC(self)
# dc.SetPen(wx.Pen("red", style=wx.SOLID))
# dc.DrawLine(self.contourPoints[-1][0], self.contourPoints[-1][1],
# self.contourPoints[0][0], self.contourPoints[0][1])
# fill contour
# self.dc = wx.ClientDC(self)
# self.gc = wx.GraphicsContext.Create(self.dc)
self.dc.SetPen(wx.Pen(CLASS_COLORS_RGB[self.selected_class], style=wx.SOLID))
self.gc.SetBrush(wx.Brush(
wx.Colour(CLASS_COLORS_RGB[self.selected_class][0], CLASS_COLORS_RGB[self.selected_class][1],
CLASS_COLORS_RGB[self.selected_class][2], 125), wx.SOLID))
if len(self.contourPoints) > 1:
self.gc.DrawLines(self.contourPoints)
self.dc.DrawLines(self.contourPoints)
# self.gc.DrawLines(self.contourPoints)
# self.dc.DrawLines(self.contourPoints)
elif current_operation == 'edit':
x, y = e.GetPosition()
print('here', x, y)
new_w, new_h = self.dc.GetSize()
x_scaled, y_scaled = (int(x * IMAGE_WIDTH / new_w), int(y * IMAGE_HEIGHT / new_h))
#if polygons_to_be_deleted == []:
for current_polygon in polygons:
if cv2.pointPolygonTest(current_polygon[2], (x_scaled, y_scaled), True) > 0:
polygons_to_be_deleted.append(current_polygon)
#polygons_to_be_deleted = removePolygonDuplicates(polygons_to_be_deleted)
print('appended')
redrawSegmentationPolys(polygons_to_be_deleted, highlighted=0)
self.parent.PrepAndChangeImage(plotAnnotationsOverlay())
if len(polygons_to_be_deleted) > 0:
self.parent.m_button31b.Enable()
self.parent.m_button3.Enable()
self.parent.m_choiceClass.Enable()
self.parent.m_choiceClass.SetSelection(polygons_to_be_deleted[0][1])
self.parent.m_button31c.Enable()
if len(polygons_to_be_deleted) > 1:
self.parent.m_button31p.Enable()
self.parent.m_button31n.Enable()
# self.clear()
# self.parent.PrepAndChangeImage(self.parent.current_slice)
def deleteLastVertex(self):
self.contourPoints = self.contourPoints[:-1]
self.endDrawing()
def setDrawingStyle(self, drawing_style):
self.drawingStyle = drawing_style
def clear(self):
dc = wx.ClientDC(self)
dc.Clear()
self.contourPoints = []
self.redrawImage()
def getCurrentSegmentation(self):
dc = wx.ClientDC(self)
img = self.saveSnapshot(dc)
buf = img.GetDataBuffer() # use img.GetAlphaBuffer() for alpha data
arr = np.frombuffer(buf, dtype='uint8')
arr = np.reshape(arr, (dc.Size.height, dc.Size.width, 3)) # hardcoded num channels :/
image = np.zeros_like(arr)
image[np.where((arr == [255, 0, 0]).all(axis=2))] = [255, 255, 255]
image[np.where((arr == [255, 255, 255]).all(axis=2))] = [255, 255, 255]
image = cv2.rotate(image, 0)
image_resized = cv2.resize(image, (self.original_image.Height, self.original_image.Width))
return image_resized
def savePolygon(self):
global polygons
if len(self.contourPoints) == 0:
return
# self.dc = wx.ClientDC(self) #batman
new_w, new_h = self.dc.GetSize()
rescaled_contour_points = []
for point in self.contourPoints:
rescaled_contour_points.append([int(point[0] * IMAGE_WIDTH / new_w), int(point[1] * IMAGE_HEIGHT / new_h)])
if self.drawingStyle == 'contour':
print('saved contour')
send_to_back = self.parent.checkBox1.GetValue()
print(image_np_all_slices_segmentation[:, :, self.selected_class].shape)
approxContourPoints = cv2.approxPolyDP(np.asarray(rescaled_contour_points), 1, closed=True)
bw_poly = cv2.fillPoly(np.zeros_like(image_np_all_slices_normed[:, :, 0]), [approxContourPoints], 255,
8) > 0
current_area = np.count_nonzero(bw_poly)
rmin, rmax, cmin, cmax = bbox_np(bw_poly)
self.old_area = current_area
old_area_list.append(current_area)
if send_to_back:
for idx_class in range(NUM_CLASSES):
bw_poly[np.where(image_np_all_slices_segmentation[:, :, idx_class] > 0)] = 0
# cv2.imwrite('out_bw_poly.png', np.array(bw_poly*255, dtype=np.uint8))
contours, hierarchy = cv2.findContours(np.array(bw_poly, dtype=np.uint8), cv2.RETR_LIST,
cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=True)
for cnt in contours:
approxContourPoints = cv2.approxPolyDP(np.asarray(cnt), 1, closed=True)
print(cnt)
print('problem here')
break
# check dealbreaker
"""
if self.selected_class != 4 and self.selected_class != 7:
avg_p_x = 0
avg_p_y = 0
for p in approxContourPoints:
avg_p_x+=p[0][0]
avg_p_y+=p[0][1]
avg_p_x /=len(approxContourPoints)
avg_p_y /=len(approxContourPoints)
if cv2.pointPolygonTest(approxContourPoints, (avg_p_x, avg_p_y), True) < 0:
style = wx.ICON_ERROR | wx.OK
dialog = wx.MessageDialog(None, 'Polygon is not convex', 'Error', style)
dialogResult = dialog.ShowModal()
dialog.Destroy()
self.contourPoints = []
self.parent.PrepAndChangeImage(plotAnnotationsOverlay())
return
"""
polygons.append([current_area, self.selected_class, approxContourPoints, (rmin, rmax, cmin, cmax)])
polygons = sorted(polygons, reverse=True, key=lambda x: x[0])
drawSegmentationPolys(fromArea=current_area, current_seg=bw_poly, current_class=self.selected_class,
limits=(rmin, rmax, cmin, cmax))
self.parent.PrepAndChangeImage(plotAnnotationsOverlay())
self.contourPoints = []
elif self.drawingStyle == 'brush':
# approximate curve -- closed does NOT work, for whatever reason
approxContourPoints = cv2.approxPolyDP(np.asarray(self.contourPoints), 1, closed=True)
def saveSnapshot(self, dcSource):
# based largely on code posted to wxpython-users by Andrea Gavana 2006-11-08
size = dcSource.Size
# Create a Bitmap that will later on hold the screenshot image
# Note that the Bitmap must have a size big enough to hold the screenshot
# -1 means using the current default colour depth
bmp = wx.Bitmap(size.width, size.height) #
# Create a memory DC that will be used for actually taking the screenshot
memDC = wx.MemoryDC()
# Tell the memory DC to use our Bitmap
# all drawing action on the memory DC will go to the Bitmap now
memDC.SelectObject(bmp)
# Blit (in this case copy) the actual screen on the memory DC
# and thus the Bitmap
memDC.Blit(0, # Copy to this X coordinate
0, # Copy to this Y coordinate
size.width, # Copy this width
size.height, # Copy this height
dcSource, # From where do we copy?
0, # What's the X offset in the original DC?
0 # What's the Y offset in the original DC?
)
# Select the Bitmap out of the memory DC by selecting a new
# uninitialized Bitmap
memDC.SelectObject(wx.NullBitmap)
img = bmp.ConvertToImage()
return img
def __del__(self):
pass
###########################################################################
## Class seg_frame
###########################################################################
class seg_frame(wx.Frame):
def __init__(self, parent, title):
self._current_slice = 0
self.num_slices_current_axis = NUM_SLICES
wx.Frame.__init__(self, parent, id=wx.ID_ANY, title=title, pos=wx.DefaultPosition,
size=wx.Size(1280, 768), style=wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL, name=u"Seg")
self.SetSizeHints(wx.DefaultSize, wx.DefaultSize)
bSizer1 = wx.BoxSizer(wx.VERTICAL)
bSizer2 = wx.BoxSizer(wx.HORIZONTAL)
self.bSizer2Ext = bSizer2
self.m_button5 = wx.Button(self, 83, _("Save frame"), wx.DefaultPosition, wx.DefaultSize, 0)
bSizer2.Add(self.m_button5, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
# if current_operation == 'segment':
self.m_button6 = wx.Button(self, 84, _("Save current polygon"), wx.DefaultPosition, wx.DefaultSize, 0)
self.m_button6.Hide()
bSizer2.Add(self.m_button6, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
# if current_operation == 'segment':
self.m_button31 = wx.Button(self, 81, _("Delete last vertex"), wx.DefaultPosition, wx.DefaultSize, 0)
self.m_button31.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT))
self.m_button31.Hide()
bSizer2.Add(self.m_button31, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
self.m_button3 = wx.Button(self, 80, _("Delete current polygon"), wx.DefaultPosition, wx.DefaultSize, 0)
if current_operation == 'edit':
self.m_button3.Disable()
self.m_button3.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT))
bSizer2.Add(self.m_button3, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
# if current_operation == 'segment':
self.checkBox1 = wx.CheckBox(self, 201, _("Send to back"))
self.checkBox1.Hide()
bSizer2.Add(self.checkBox1, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
self.checkBox2 = wx.CheckBox(self, 210, _("Hide seg"))
self.checkBox2.Hide()
bSizer2.Add(self.checkBox2, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
self.m_button31x = wx.Button(self, 202, _("Undo polygon"), wx.DefaultPosition, wx.DefaultSize, 0)
self.m_button31x.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT))
self.m_button31x.Hide()
bSizer2.Add(self.m_button31x, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
# edit mode
# if current_operation == 'edit':
self.m_button31p = wx.Button(self, 203, _("Previous polygon"), wx.DefaultPosition, wx.DefaultSize, 0)
self.m_button31p.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT))
self.m_button31p.Disable()
self.m_button31p.Hide()
bSizer2.Add(self.m_button31p, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
self.m_button31n = wx.Button(self, 204, _("Next polygon"), wx.DefaultPosition, wx.DefaultSize, 0)
self.m_button31n.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT))
self.m_button31n.Disable()
self.m_button31n.Hide()
bSizer2.Add(self.m_button31n, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
self.m_choiceClass = wx.Choice(self, 206, choices=CLASS_NAMES);
self.m_choiceClass.Disable()
self.m_choiceClass.Hide()
bSizer2.Add(self.m_choiceClass, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
self.m_button31c = wx.Button(self, 207, _("Change class"), wx.DefaultPosition, wx.DefaultSize, 0)
self.m_button31c.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT))
self.m_button31c.Disable()
self.m_button31c.Hide()
bSizer2.Add(self.m_button31c, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
self.m_button31b = wx.Button(self, 205, _("Back to segmentation"), wx.DefaultPosition, wx.DefaultSize, 0)
self.m_button31b.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT))
self.m_button31b.Disable()
self.m_button31b.Hide()
bSizer2.Add(self.m_button31b, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
self.rbox = wx.RadioBox(self, label=_("Mode"), size= wx.DefaultSize, choices=operations_list_translated, majorDimension=1,
style=wx.RA_SPECIFY_ROWS)
self.rbox.SetSelection(operations_list.index(current_operation))
self.rbox.Bind(wx.EVT_RADIOBOX, self.OnRadioBox)
bSizer2.Add(self.rbox, 0, wx.ALL | wx.ALIGN_LEFT, 5)
self.m_button111 = wx.Button(self, 208, _("Previous"), wx.DefaultPosition, wx.DefaultSize, 0)
bSizer2.Add(self.m_button111, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
self.m_button112 = wx.Button(self, 209, _("Next"), wx.DefaultPosition, wx.DefaultSize, 0)
bSizer2.Add(self.m_button112, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
bSizer1.Add(bSizer2, 0, wx.EXPAND, 5)
self.bSizer81 = wx.BoxSizer(wx.HORIZONTAL)
# if current_operation == 'segment':
self.m_staticText121 = wx.StaticText(self, wx.ID_ANY, _("Draw class"), wx.DefaultPosition, wx.DefaultSize, 0)
self.m_staticText121.Wrap(-1)
self.bSizer81.Add(self.m_staticText121, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
self.buttons_classes = []
for idxx in range(START_BUTTON_ID_CLASSES, START_BUTTON_ID_CLASSES + NUM_CLASSES):
current_button = wx.Button(self, idxx, CLASS_NAMES[idxx - START_BUTTON_ID_CLASSES], wx.DefaultPosition,
wx.DefaultSize, 0)
self.buttons_classes.append(current_button)
self.bSizer81.Add(self.buttons_classes[-1], 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
# if current_operation == 'segment':
self.bSizer81.ShowItems(show=False)
bSizer1.Add(self.bSizer81, 0, wx.ALL, 5)
bSizer3 = wx.BoxSizer(wx.HORIZONTAL)
bSizer4 = wx.BoxSizer(wx.VERTICAL)
bSizer4.Add((0, 0), 1, wx.EXPAND, 5)
# self.m_bpButton1 = wx.BitmapButton(self, 76, wx.Bitmap(u"arrow_left_half.png", wx.BITMAP_TYPE_ANY),
# wx.DefaultPosition, wx.DefaultSize, wx.BU_AUTODRAW)
bSizer4.Add((0, 0), 1, wx.EXPAND, 5)
# self.m_button11 = wx.Button(self, wx.ID_ANY, u"Previous CT", wx.DefaultPosition, wx.DefaultSize, 0)
# bSizer4.Add(self.m_button11, 0, wx.ALL, 5)
bSizer3.Add(bSizer4, 0, wx.EXPAND, 5)
bSizer8 = wx.BoxSizer(wx.VERTICAL)
image = wx.Image(IMAGE_WIDTH, IMAGE_HEIGHT) # EmptyImage
# image.SetData(np_prep_image.tostring())
#
self.drawPane = drawing_panel(self, image, 'contour', 20) # brush size
# bSizer8.Add(self.drawPane, 1, wx.ALL | wx.EXPAND, 5)
bSizer8.Add(self.drawPane, 1, wx.EXPAND, 5)
# if current_operation == 'segment':
self.buttons_classes[self.drawPane.selected_class].SetBackgroundColour(
wx.Colour(CLASS_COLORS_RGB[self.drawPane.selected_class][0],
CLASS_COLORS_RGB[self.drawPane.selected_class][1],
CLASS_COLORS_RGB[self.drawPane.selected_class][2],
125))
bSizer3.Add(bSizer8, 1, wx.EXPAND, 5)
bSizer41 = wx.BoxSizer(wx.VERTICAL)
bSizer41.Add((0, 0), 1, wx.EXPAND, 5)
bSizer41.Add((0, 0), 1, wx.EXPAND, 5)
# self.m_button111 = wx.Button(self, wx.ID_ANY, u"Next", wx.DefaultPosition, wx.DefaultSize, 0)
# bSizer41.Add(self.m_button111, 0, wx.ALL | wx.ALIGN_RIGHT, 5)
bSizer3.Add(bSizer41, 0, wx.EXPAND, 5)
bSizer7 = wx.BoxSizer(wx.VERTICAL)
bSizer3.Add(bSizer7, 1, wx.EXPAND, 5)
bSizer1.Add(bSizer3, 1, wx.EXPAND, 5)
self.initOperation()
self.SetSizer(bSizer1)
self.Layout()
self.Centre(wx.BOTH)
self.Bind(wx.EVT_MOVE, self.OnMove)
self.Bind(wx.EVT_BUTTON, self.OnButtonClicked)
self.Bind(wx.EVT_SCROLL, self.OnScrollChanged)
self.Bind(wx.EVT_COMBOBOX, self.OnComboboxChanged)
self.Bind(wx.EVT_CHAR_HOOK, self.OnKeyDown)
self.Bind(wx.EVT_CHECKBOX, self.OnCheckBoxChanged)
# self.m_comboBox11.Bind(wx.EVT_COMBOBOX, self.OnComboboxChanged)
# this isn't supposed to be here, but otherwise it fails to draw the segmentation for the 1st slice
self.PrepAndChangeImage(plotAnnotationsOverlay())
@property
def current_slice(self):
return self._current_slice
@current_slice.setter
def current_slice(self, value):
if value < 0:
self._current_slice = 0
elif value > self.num_slices_current_axis - 1:
self._current_slice = self.num_slices_current_axis - 1
else:
self._current_slice = value
self.m_comboBox1.Value = str(self._current_slice + 1)
def initOperation(self):
if current_operation == 'segment':
# show
self.m_button6.Show()
self.m_button31.Show()
self.checkBox1.Show()
self.checkBox2.Show()
self.m_button31x.Show()
self.bSizer81.ShowItems(show=True)
# hide
self.m_button31p.Hide()
self.m_button31n.Hide()
self.m_button31c.Hide()
self.m_button31b.Hide()
self.m_choiceClass.Hide()
# enable
self.m_button3.Enable()
# bug
#self.rbox.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHT))
self.rbox.Refresh()
#self.bSizer2Ext.Layout()
if current_operation == 'edit':
# hide
self.m_button6.Hide()
self.m_button31.Hide()
self.checkBox1.Hide()
self.checkBox2.Hide()
self.m_button31x.Hide()
self.bSizer81.ShowItems(show=False)
# show
self.m_button31p.Show()
self.m_button31n.Show()
self.m_button31c.Show()
self.m_button31b.Show()
self.m_choiceClass.Show()
# disable
self.m_button3.Disable()
# delete
self.drawPane.contourPoints = []
# bug
self.rbox.Refresh()
#self.rbox.Layout()
def OnRadioBox(self, e):
global current_operation
global operation_changed
#operation_changed = True
current_operation = operations_list[self.rbox.GetSelection()]
# self.__init__(self, "segmentation")
self.initOperation()
# self.mainLogin.Hide()
self.Layout()
# self.__init__(None, title="lala")
# self.Close()
def OnMove(self, e):
x, y = e.GetPosition()
# print("current window position x = ", x, " y= ", y)
def OnKeyDown(self, e):
# print(e.GetKeyCode())
if e.GetKeyCode() == 32:
# space -- save polygon
self.drawPane.savePolygon()
elif e.GetKeyCode() == 8:
# backspace -- delete current vertex
self.drawPane.deleteLastVertex()
def OnButtonClicked(self, e):
global polygons
global polygons_to_be_deleted
global current_polygon_to_be_deleted_idx
global image_np_all_slices_segmentation
global old_area_list
global current_operation
global operation_changed
global FILE_PATH
if e.Id == 76: # load left slice
if self.current_slice > 0:
# self.SaveCurrentSegmentation()
self.current_slice -= 1
self.m_slider1.Value = self.current_slice
self.PrepAndChangeImage(self.current_slice)
elif e.Id == 77: # load right slice
if self.current_slice < self.num_slices_current_axis - 1:
# self.SaveCurrentSegmentation()
self.current_slice += 1
self.m_slider1.Value = self.current_slice
self.PrepAndChangeImage(self.current_slice)
elif e.Id == 80: # proper erase?
if current_operation == 'edit':
oal = polygons_to_be_deleted[current_polygon_to_be_deleted_idx]
polygons_new = []
for elem in polygons:
if elem[0] != oal[0]:
polygons_new.append(elem)
polygons_to_be_deleted = []
current_polygon_to_be_deleted_idx = 0
polygons = polygons_new.copy()
self.drawPane.contourPoints = []
image_np_all_slices_segmentation = np.zeros((IMAGE_HEIGHT, IMAGE_WIDTH, NUM_CLASSES), dtype=np.bool_)
redrawSegmentationPolys(polygons)
self.PrepAndChangeImage(plotAnnotationsOverlay())
self.m_button3.Disable()
self.m_button31b.Disable()
self.m_button31p.Disable()
self.m_button31n.Disable()
self.m_choiceClass.Disable()
self.m_button31c.Disable()
else:
# self.drawPane.clear()
# self.EraseCurrentSegmentation()
self.drawPane.contourPoints = []
self.PrepAndChangeImage(plotAnnotationsOverlay())
"""
if self.m_button3.GetForegroundColour().RGB == 0:
self.m_button3.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHT))
else:
self.m_button3.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT))
"""
elif e.Id == 81: # delete last vertex
self.drawPane.deleteLastVertex()
elif e.Id == 83: # save all
self.SaveSegmentationToFile()