forked from chrisadamsonmcri/CCSegThickness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CCSegManedit
executable file
·867 lines (642 loc) · 31.7 KB
/
CCSegManedit
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
#!/usr/bin/python
import os
import sys
import numpy
import getopt
import errno
import h5py
import pygtk
pygtk.require('2.0')
import gtk
import gobject
import copy
import CCSegUtils
import scipy
import scipy.ndimage
import CCSegPipeSeg
import pylab
def usage():
print "Usage: " + sys.argv[0] + " [options] <output directory>"
print
print "\tDESCRIPTION"
print
print "\t\tSegmentation manual editor for CC Segmentation and Thickness profile pipeline"
print
print "\tARGUMENTS"
print "\t\t<output directory>: the directory containing all the output from CCSegProcess, the user must have run CCSegProcess --do-seg"
print
print "\tOPTIONS"
print "\t\t--start-subject=<start subject>: Start on this subject"
print "\t\t-h: This message"
class UIManagerExample:
WIDTH = 1000
HEIGHT = 800
# ui = '''<ui>
# <toolbar name="BrushToolbar">
# <toolitem action="BrushSquare"/>
# <toolitem action="BrushCircle"/>
# </toolbar>
# <toolbar name="MorphToolbar">
# <toolitem action="MorphErode"/>
# <toolitem action="MorphDilate"/>
# <toolitem action="MorphClose"/>
# <toolitem action="MorphOpen"/>
# </toolbar>
# </ui>'''
#
def delete_event(self, widget, event, data=None):
gtk.main_quit()
return False
def ask_to_save_segs(self):
dialog = gtk.MessageDialog(parent=None, flags=0, type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_YES_NO, message_format="There are unsaved changes, do you wish to save the changes?")
response = dialog.run()
dialog.destroy()
return response
def window_deleting_cb(self, widget, event, data = None):
if numpy.any(self.subjectsToSave):
response = self.ask_to_save_segs()
if response == gtk.RESPONSE_YES:
self.saveSegs()
return False
def quit_cb(self, b):
if numpy.any(self.subjectsToSave):
response = self.ask_to_save_segs()
if response == gtk.RESPONSE_YES:
self.saveSegs()
gtk.main_quit()
def layout_resize(self, widget, event):
x, y, width, height = widget.get_allocation()
if width > self.lwidth or height > self.lheight:
self.lwidth = max(width, self.lwidth)
self.lheight = max(height, self.lheight)
widget.set_size(self.lwidth, self.lheight)
# callbacks
def brush_cb(self, widget, data=None):
self.brushType = data
def undo_cb(self, b):
if not self.curUndoIMG is None:
self.CCSegs[self.curUndoSubjectID]['finalSegManEdit'] = numpy.array(self.curUndoIMG)
if self.curSubjectID == self.curUndoSubjectID:
self.updateSubject()
self.curUndoIMG = None
self.curUndoSubjectID = None
def morph_cb(self, widget, data=None):
#print "morph_cb %s" % data
self.initSegChanging()
xx = numpy.arange(-self.morphSizeScale.get_value(), self.morphSizeScale.get_value() + 1)
yy = numpy.array(xx)
xx = numpy.atleast_2d(xx)
yy = numpy.atleast_2d(yy).T
morphMask = numpy.sqrt(xx * xx + yy * yy) <= self.morphSizeScale.get_value()
del xx
del yy
if data == "dilate":
self.CCSegs[self.curSubjectID]['finalSegManEdit'] = scipy.ndimage.morphology.binary_dilation(self.CCSegs[self.curSubjectID]['finalSegManEdit'], structure = morphMask)
elif data == "erode":
self.CCSegs[self.curSubjectID]['finalSegManEdit'] = scipy.ndimage.morphology.binary_erosion(self.CCSegs[self.curSubjectID]['finalSegManEdit'], structure = morphMask)
elif data == "close":
self.CCSegs[self.curSubjectID]['finalSegManEdit'] = scipy.ndimage.morphology.binary_closing(self.CCSegs[self.curSubjectID]['finalSegManEdit'], structure = morphMask)
elif data == "open":
self.CCSegs[self.curSubjectID]['finalSegManEdit'] = scipy.ndimage.morphology.binary_opening(self.CCSegs[self.curSubjectID]['finalSegManEdit'], structure = morphMask)
self.updateSubject()
def morphother_cb(self, widget, data=None):
self.initSegChanging()
if data == 'fillholes':
self.CCSegs[self.curSubjectID]['finalSegManEdit'] = scipy.ndimage.morphology.binary_fill_holes(self.CCSegs[self.curSubjectID]['finalSegManEdit'])
elif data == 'largestcomponent':
CCSegLabels, numCCSegLabels = scipy.ndimage.measurements.label(self.CCSegs[self.curSubjectID]['finalSegManEdit'], structure = numpy.ones([3, 3]))
if numCCSegLabels > 1:
M = CCSegLabels[numpy.nonzero(CCSegLabels)]
H, bin_edges = numpy.histogram(M, bins = numpy.arange(1, numCCSegLabels + 2))
I = numpy.argmax(H)
self.CCSegs[self.curSubjectID]['finalSegManEdit'] = (CCSegLabels == bin_edges[I])
pass
elif data == 'watershed':
watershedSeg, watershedMarkers, watershedTransform, edgeMAG = CCSegPipeSeg.watershedTransformSeg(self.CCSegs[self.curSubjectID]['IMG'], self.CCSegs[self.curSubjectID]['finalSegManEdit'])
# print numpy.count_nonzero(self.CCSegs[self.curSubjectID]['finalSegManEdit'])
# print numpy.mean(self.CCSegs[self.curSubjectID]['IMG'])
# pylab.clf()
# pylab.subplot(2, 2, 1)
# CCSegUtils.showIMG(watershedMarkers)
# pylab.subplot(2, 2, 2)
# CCSegUtils.showIMG(watershedTransform)
# pylab.subplot(2, 2, 3)
# CCSegUtils.showIMG(edgeMAG)
# pylab.subplot(2, 2, 4)
# CCSegUtils.showIMG(watershedSeg)
# pylab.gcf().set_size_inches((20, 10), forward = True)
# pylab.show()
self.CCSegs[self.curSubjectID]['finalSegManEdit'] = numpy.array(watershedSeg)
pass
self.updateSubject()
def saveSegs(self):
#print "Saving segmentations"
I = numpy.where(self.subjectsToSave)[0]
for z in I:
curSubjectID = self.CCSegSubjectIDs[z]
FIDManedit = h5py.File(os.path.join(self.inputDirectory, curSubjectID + "_seg_manedit.hdf5"), 'w')
FIDManedit.create_dataset("finalSegManEdit", data = numpy.uint8(self.CCSegs[curSubjectID]['finalSegManEdit']), compression = 'gzip')
FIDManedit.close()
#self.CCSegs[self.CCSegSubjectIDs[z]]['finalSegManEdit']
#print z
# make all subjects unmodified
self.subjectsToSave.fill(False)
def segsave_cb(self, widget, data=None):
#print "segsave_cb %s" % data
self.saveSegs()
self.segSaveButton.set_sensitive(False)
def initSegChanging(self):
if self.CCSegs[self.curSubjectID]['finalSegManEdit'] is None:
self.CCSegs[self.curSubjectID]['finalSegManEdit'] = numpy.array(self.CCSegs[self.curSubjectID]['originalSeg'])
# copy the current segmentation to the undo image
self.curUndoIMG = numpy.array(self.CCSegs[self.curSubjectID]['finalSegManEdit'])
# set the current undo subject ID
self.curUndoSubjectID = self.curSubjectID
# set the current subject ID "to save" to true, indicating it has been modified
self.subjectsToSave[int(self.curSubjectSlider.get_value()) - 1] = True
# enable the save segmentation button
self.segSaveButton.set_sensitive(True)
def segColourButtonPixmap(self, curColour):
buttonIMGShape = (16, 16)
T = numpy.tile(numpy.reshape(numpy.atleast_3d(curColour), (1, 1, 3)), (buttonIMGShape[0], buttonIMGShape[1], 1))
#rint T
#rint T.shape
return gtk.gdk.pixbuf_new_from_array(T, gtk.gdk.COLORSPACE_RGB, 8)
def subjectButtonClicked_cb(self, widget, data=None):
#self.curSubjectID = self.CCSegSubjectIDs[int(self.curSubjectSlider.get_value()) - 1]
curSubjectIDX = int(self.curSubjectSlider.get_value())
if data == "previous":
self.curSubjectSlider.set_value(curSubjectIDX - 1)
elif data == "next":
self.curSubjectSlider.set_value(curSubjectIDX + 1)
self.updateSubject()
def colourchanged_cb(self, widget, data = None):
T = widget.get_color()
RGB = numpy.array([T.red, T.green, T.blue], dtype = numpy.uint16)
RGB = numpy.uint8(RGB / 256)
self.segColours[data] = numpy.array(RGB)
self.updateSubject()
def subjectchanged_cb(self, widget, data = None):
self.updateSubject()
def RGBtoGDKColor(self, RGB):
T = numpy.double(RGB) * 256
T = numpy.ravel(numpy.uint16(T))
return gtk.gdk.Color(red = T[0], green = T[1], blue = T[2])
def on_key_press_event(self, widget, event):
keyname = gtk.gdk.keyval_name(event.keyval)
#print "Key %s (%d) was pressed" % (keyname, event.keyval)
curSubjectIDX = int(self.curSubjectSlider.get_value())
if keyname == 'space':
if curSubjectIDX < len(self.CCSegSubjectIDs):
self.curSubjectSlider.set_value(curSubjectIDX + 1)
self.updateSubject()
if keyname == 'BackSpace':
if curSubjectIDX > 1:
self.curSubjectSlider.set_value(curSubjectIDX - 1)
self.updateSubject()
def __init__(self):
# Create the toplevel window
window = gtk.Window()
window.connect('destroy', lambda w: gtk.main_quit())
window.set_size_request(self.WIDTH, self.HEIGHT)
window.set_title("CCSegPipe manual editing tool")
window.connect("delete-event", self.window_deleting_cb)
window.connect('key_press_event', self.on_key_press_event)
self.lwidth = self.WIDTH
self.lheight = self.HEIGHT
#elf.window.connect("delete_event", self.delete_event)
#self.window.set_border_width(10)
# A gtk.VBox is a container that allows you to insert objects
# vertically
bgBox = gtk.VBox()
window.add(bgBox)
mainHBox = gtk.HBox()
bgBox.pack_end(mainHBox, True, True)
leftVBox = gtk.VBox()
mainHBox.pack_start(leftVBox, expand = False, fill = True)
rightVBox = gtk.VBox()
mainHBox.pack_start(rightVBox, expand = True, fill = True)
# Create a UIManager instance
uimanager = gtk.UIManager()
accelgroup = uimanager.get_accel_group()
window.add_accel_group(accelgroup)
self.actiongroup = gtk.ActionGroup('UIManagerExample')
self.actiongroup.add_actions([
('Save', gtk.STOCK_SAVE, '_Save segmentations', "<CTRL>S", 'Save segmentations', self.segsave_cb),
('Quit', gtk.STOCK_QUIT, '_Quit', None, 'Quit the Program', self.quit_cb),
('Undo', gtk.STOCK_UNDO, '_Undo', "<CTRL>Z", 'Undo last edit', self.undo_cb),
('File', None, '_File'), ('Edit', None, '_Edit')])
self.actiongroup.get_action('Save').set_property('short-label', '_Save')
self.actiongroup.get_action('Quit').set_property('short-label', '_Quit')
self.actiongroup.get_action('Undo').set_property('short-label', '_Undo')
ui = '''<ui>
<menubar name="MenuBar">
<menu action="File">
<menuitem action="Save"/>
<menuitem action="Quit"/>
</menu>
<menu action="Edit">
<menuitem action="Undo"/>
</menu>
</menubar>
</ui>'''
uimanager.insert_action_group(self.actiongroup, 0)
uimanager.add_ui_from_string(ui)
menubar = uimanager.get_widget('/MenuBar')
bgBox.pack_start(menubar, False)
#leftVBox.pack_start(gtk.Label("Brush"), expand = False, fill = False)
#brushToolbar = uimanager.get_widget('/BrushToolbar')
#leftVBox.pack_start(brushToolbar, False)
brushFrame = gtk.Frame("Brush")
leftVBox.pack_start(brushFrame, expand = False, fill = False)
brushButtonBox = gtk.HButtonBox()
brushButtonBox.set_layout(gtk.BUTTONBOX_SPREAD)
brushButtonBox.set_spacing(5)
brushButtonBox.set_border_width(5)
brushFrameBox = gtk.VBox()
brushFrame.add(brushFrameBox)
#brushFrame.add(brushButtonBox)
brushFrameBox.pack_start(brushButtonBox, False, True)
self.brushSizeScale = gtk.HScale(gtk.Adjustment(value=1, lower=1, upper=10, step_incr=10, page_incr=1, page_size=0))
self.brushSizeScale.set_digits(0)
brushFrameBox.pack_start(gtk.Label("Brush size"))
brushFrameBox.pack_start(self.brushSizeScale, False, True)
#imageSquare = gtk.Image()
#imageSquare.set_from_stock(gtk.STOCK_OPEN, gtk.ICON_SIZE_BUTTON)
brushCircleButton = gtk.RadioButton(None, "Circle")
brushSquareButton = gtk.RadioButton(brushCircleButton, "Square")
brushSquareButton.connect("toggled", self.brush_cb, "square")
brushCircleButton.connect("toggled", self.brush_cb, "circle")
self.brushType = 'circle'
#brushSquareButton.set_image(imageSquare)
#imageCircle = gtk.Image()
#imageCircle.set_from_stock(gtk.STOCK_OPEN, gtk.ICON_SIZE_BUTTON)
brushButtonBox.add(brushSquareButton)
brushButtonBox.add(brushCircleButton)
morphFrame = gtk.Frame("Morphology")
morphFrameBox = gtk.VBox()
morphFrame.add(morphFrameBox)
morphButtonBox = gtk.HButtonBox()
morphButtonBox.set_layout(gtk.BUTTONBOX_SPREAD)
morphButtonBox.set_spacing(5)
morphButtonBox.set_border_width(5)
morphButtonTable = gtk.Table(rows = 2, columns = 2, homogeneous = False)
morphDilateButton = gtk.Button(label = "Dilate")
morphDilateButton.connect("clicked", self.morph_cb, "dilate")
#morphButtonBox.add(morphDilateButton)
morphButtonTable.attach(morphDilateButton, 0, 1, 0, 1, xoptions=gtk.EXPAND|gtk.FILL, yoptions=gtk.EXPAND|gtk.FILL, xpadding=5, ypadding=5)
morphErodeButton = gtk.Button(label = "Erode")
morphErodeButton.connect("clicked", self.morph_cb, "erode")
morphButtonTable.attach(morphErodeButton, 1, 2, 0, 1, xoptions=gtk.EXPAND|gtk.FILL, yoptions=gtk.EXPAND|gtk.FILL, xpadding=5, ypadding=5)
#morphButtonBox.add(morphErodeButton)
morphCloseButton = gtk.Button(label = "Close")
morphCloseButton.connect("clicked", self.morph_cb, "close")
#morphButtonBox.add(morphCloseButton)
morphButtonTable.attach(morphCloseButton, 0, 1, 1, 2, xoptions=gtk.EXPAND|gtk.FILL, yoptions=gtk.EXPAND|gtk.FILL, xpadding=5, ypadding=5)
morphOpenButton = gtk.Button(label = "Open")
morphOpenButton.connect("clicked", self.morph_cb, "open")
#morphButtonBox.add(morphOpenButton)
morphButtonTable.attach(morphOpenButton, 1, 2, 1, 2, xoptions=gtk.EXPAND|gtk.FILL, yoptions=gtk.EXPAND|gtk.FILL, xpadding=5, ypadding=5)
#morphFrameBox.pack_start(morphButtonBox, expand = False, fill = False)
morphFrameBox.pack_start(morphButtonTable, expand = False, fill = False)
self.morphSizeScale = gtk.HScale(gtk.Adjustment(value=1, lower=1, upper=10, step_incr=10, page_incr=1, page_size=0))
self.morphSizeScale.set_digits(0)
morphFrameBox.pack_start(gtk.Label("Structural element size"))
morphFrameBox.pack_start(self.morphSizeScale, False, True)
morphFrameBox.pack_start(gtk.Label("Other"))
morphLargestComponentButton = gtk.Button(label = "Largest component")
morphLargestComponentButton.connect("clicked", self.morphother_cb, 'largestcomponent')
morphFrameBox.pack_start(morphLargestComponentButton, expand = False, fill = False)
morphFillHolesButton = gtk.Button(label = "Fill holes")
morphFillHolesButton.connect("clicked", self.morphother_cb, 'fillholes')
morphFrameBox.pack_start(morphFillHolesButton, expand = False, fill = False)
watershedHolesButton = gtk.Button(label = "Watershed")
watershedHolesButton.connect("clicked", self.morphother_cb, 'watershed')
morphFrameBox.pack_start(watershedHolesButton, expand = False, fill = False)
leftVBox.pack_start(morphFrame, expand = False, fill = False)
coloursFrame = gtk.Frame("Colours")
leftVBox.pack_start(coloursFrame, expand = False, fill = False)
segColourTable = gtk.Table(rows = 3, columns = 2, homogeneous = False)
coloursFrame.add(segColourTable)
self.segColours = dict()
self.segColours['seg'] = numpy.array([128, 0, 0], dtype = numpy.uint8)
self.segColours['add'] = numpy.array([0, 128, 0], dtype = numpy.uint8)
self.segColours['del'] = numpy.array([0, 0, 128], dtype = numpy.uint8)
segColourSegButton = gtk.ColorButton(color = self.RGBtoGDKColor(self.segColours['seg']))
segColourAddButton = gtk.ColorButton(color = self.RGBtoGDKColor(self.segColours['add']))
segColourDelButton = gtk.ColorButton(color = self.RGBtoGDKColor(self.segColours['del']))
segColourSegButton.connect('color-set', self.colourchanged_cb, "seg")
segColourAddButton.connect('color-set', self.colourchanged_cb, "add")
segColourDelButton.connect('color-set', self.colourchanged_cb, "del")
#segColourSegButton = gtk.Button()
#segColourAddButton = gtk.Button()
#segColourDelButton = gtk.Button()
# box1 = gtk.HBox(False, 0)
# self.segColourSegButtonIMG = gtk.Image()
# self.segColourSegButtonIMG.set_from_pixbuf(self.segColourButtonPixmap(self.segColours['seg']))
# box1.pack_start(self.segColourSegButtonIMG, True, True)
# segColourSegButton.add(box1)
#
# box1 = gtk.HBox(False, 0)
# self.segColourAddButtonIMG = gtk.Image()
# self.segColourAddButtonIMG.set_from_pixbuf(self.segColourButtonPixmap(self.segColours['add']))
# box1.pack_start(self.segColourAddButtonIMG, True, True)
# segColourAddButton.add(box1)
#
# box1 = gtk.HBox(False, 0)
# self.segColourDelButtonIMG = gtk.Image()
# self.segColourDelButtonIMG.set_from_pixbuf(self.segColourButtonPixmap(self.segColours['del']))
# box1.pack_start(self.segColourDelButtonIMG, True, True)
# segColourDelButton.add(box1)
#
segColourTable.attach(segColourSegButton, 0, 1, 0, 1, xoptions=gtk.FILL, yoptions=gtk.FILL, xpadding=5, ypadding=0)
segColourTable.attach(segColourAddButton, 0, 1, 1, 2, xoptions=gtk.FILL, yoptions=gtk.FILL, xpadding=5, ypadding=0)
segColourTable.attach(segColourDelButton, 0, 1, 2, 3, xoptions=gtk.FILL, yoptions=gtk.FILL, xpadding=5, ypadding=0)
T = gtk.Label('Segmentation'); T.set_alignment(xalign=0.0, yalign=0.5)
segColourTable.attach(T, 1, 2, 0, 1, xoptions=gtk.EXPAND|gtk.FILL, yoptions=gtk.EXPAND|gtk.FILL, xpadding=5, ypadding=0)
T = gtk.Label('Adding'); T.set_alignment(xalign=0.0, yalign=0.5)
segColourTable.attach(T, 1, 2, 1, 2, xoptions=gtk.EXPAND|gtk.FILL, yoptions=gtk.EXPAND|gtk.FILL, xpadding=5, ypadding=0)
T = gtk.Label('Deleting'); T.set_alignment(xalign=0.0, yalign=0.5)
segColourTable.attach(T, 1, 2, 2, 3, xoptions=gtk.EXPAND|gtk.FILL, yoptions=gtk.EXPAND|gtk.FILL, xpadding=5, ypadding=0)
#segColourSegButton.connect("clicked", self.colourchange_cb, 'seg')
#segColourAddButton.connect("clicked", self.colourchange_cb, 'add')
#segColourDelButton.connect("clicked", self.colourchange_cb, 'del')
#leftVBox.pack_start(segColourTable, expand = False, fill = False)
leftVBox.pack_start(gtk.Label("Segmentation opacity"), expand = False, fill = False)
self.segOpacity = gtk.HScale(gtk.Adjustment(value=0.7, lower=0, upper=1, step_incr=1, page_incr=1, page_size=0))
self.segOpacity.connect("value-changed", self.subjectchanged_cb, "")
leftVBox.pack_start(self.segOpacity, expand = False, fill = False)
self.segSaveButton = gtk.Button(label = "Save segmentations")
self.segSaveButton.connect("clicked", self.segsave_cb, "")
self.segSaveButton.set_sensitive(False)
leftVBox.pack_start(self.segSaveButton, expand = False, fill = False, padding = 10)
rightTopBox = gtk.VBox()
self.curSubjectLabel = gtk.Label('Current subject')
rightTopBox.pack_start(self.curSubjectLabel, expand = False, fill = True, padding = 5)
self.curSubjectSlider = gtk.HScale(gtk.Adjustment(value = 0, lower = 0, upper = 1, step_incr = 1, page_incr = 1, page_size=0))
self.curSubjectSlider.set_digits(0)
self.curSubjectSlider.connect("value-changed", self.subjectchanged_cb, "")
rightTopBox.pack_start(self.curSubjectSlider, expand = False, fill = True, padding = 5)
rightSubjectButtons = gtk.HButtonBox()
rightSubjectButtons.set_layout(gtk.BUTTONBOX_SPREAD)
rightSubjectButtons.set_spacing(50)
self.prevSubjectButton = gtk.Button(label = "Previous")
self.prevSubjectButton.connect("clicked", self.subjectButtonClicked_cb, "previous")
self.prevSubjectButton.set_sensitive(False)
self.nextSubjectButton = gtk.Button(label = "Next")
self.nextSubjectButton.connect("clicked", self.subjectButtonClicked_cb, "next")
rightSubjectButtons.add(self.prevSubjectButton)
rightSubjectButtons.add(self.nextSubjectButton)
rightTopBox.pack_start(rightSubjectButtons, expand = False, fill = False, padding = 0)
self.mainImageEventBox = gtk.EventBox()
self.mainImage = gtk.Image()
self.mainImage.connect('expose-event', self.onImageResize, window)
self.mainImageEventBox.connect('button_press_event', self.imageButtonClicked, "pressed")
self.mainImageEventBox.connect('button_release_event', self.imageButtonClicked, "released")
self.mainImageEventBox.connect('motion_notify_event', self.imageButtonClicked, "dragged")
self.mainImage.show()
self.mainImageEventBox.add(self.mainImage)
#gtk.gdk.pixbuf_new_from_array(, gtk.gdk.COLORSPACE_RGB, 8)
rightVBox.pack_start(rightTopBox, expand = True, fill = True, padding = 5)
rightVBox.pack_start(self.mainImageEventBox, expand = True, fill = True, padding = 5)
#eftVBox.pack_start(morphFrame, expand = False, fill = False)
#leftVBox.pack_start(gtk.Label("Morphological operations"))
#morphToolbar = uimanager.get_widget('/MorphToolbar')
#leftVBox.pack_start(morphToolbar, expand = False, fill = False)
#scrolled_window = gtk.ScrolledWindow()
#scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
# the main image's allocation size
self.mainImageAllocationHeight = None
self.mainImageAllocationWidth = None
# the main image's actual size after resizing
self.resizeWidth = None
self.resizeHeight = None
self.curImageWithSegRGB = None
self.curIMGRGB = None
self.curSegChangeIMG = None
self.curSegModColour = None
self.curUndoIMG = None
self.curUndoSubjectID = None
self.curSegModMode = None
window.show_all()
def resetSizeOfMainImage(self, doNotCheckAllocation = False):
allocation = self.mainImage.get_allocation()
#if self.mainImageAllocationHeight != allocation.height or self.mainImageAllocationWidth != allocation.width or doNotCheckAllocation:
IMGAspect = float(self.CCSegs[self.curSubjectID]['IMG'].shape[1]) / float(self.CCSegs[self.curSubjectID]['IMG'].shape[0])
# self.mainImageAllocationHeight = allocation.height
# self.mainImageAllocationWidth = allocation.width
allocationAspect = float(allocation.width) / float(allocation.height)
if allocationAspect > IMGAspect:
self.resizeHeight = allocation.height
self.resizeWidth = int(IMGAspect * self.resizeHeight)
else:
self.resizeWidth = allocation.width
self.resizeHeight = int(self.resizeWidth / IMGAspect)
#print "resized image dims: " + str(self.resizeWidth) + " " + str(self.resizeHeight)
# transform the clicked coordinate into the space of the original, unscaled image
def imageCoordsFromEventBoxCoords(self, eventBoxCoords):
return ( (eventBoxCoords[0] - (self.mainImageAllocationWidth - self.resizeWidth) / 2.0) * (float(self.curIMGShape[1]) / self.resizeWidth), (eventBoxCoords[1] - (self.mainImageAllocationHeight - self.resizeHeight) / 2.0) * (float(self.curIMGShape[0]) / self.resizeHeight))
# returns a mask the size of IMGShape that contains the affected pixels of a mouse click
# use
def maskFromClick(self, imgCoords):
#print "MaskFromClick"
M = numpy.zeros(self.curIMGShape, dtype = numpy.bool)
brushSize = self.brushSizeScale.get_value()
xx = numpy.int64(numpy.arange(numpy.maximum(numpy.round(imgCoords[0] - brushSize), 0), numpy.minimum(numpy.round(imgCoords[0] + brushSize) + 1, self.curIMGShape[1])))
yy = numpy.int64(numpy.arange(numpy.maximum(numpy.round(imgCoords[1] - brushSize), 0), numpy.minimum(numpy.round(imgCoords[1] + brushSize) + 1, self.curIMGShape[0])))
if self.brushType == 'square':
T = numpy.ones((numpy.size(yy), numpy.size(xx)), dtype = numpy.bool)
elif self.brushType == 'circle':
xc = xx - imgCoords[0]
yc = yy - imgCoords[1]
xc = numpy.atleast_2d(xc)
yc = numpy.atleast_2d(yc).T
T = numpy.sqrt(xc * xc + yc * yc) < brushSize
X, Y = numpy.meshgrid(xx, yy)
M[(Y, X)] = T
return M
def imageButtonClicked(self, widget, event, data = None):
curCoords = event.get_coords()
imgCoords = self.imageCoordsFromEventBoxCoords(curCoords)
# do a bounding box test to see if the click is inside the image
if imgCoords[0] >= 0 and imgCoords[0] <= self.curIMGShape[1] - 1 and imgCoords[1] >= 0 and imgCoords[1] <= self.curIMGShape[0] - 1:
# store the current segmentation for undo purposes
if data == "pressed":
if event.button == 1 or event.button == 3:
# we are modifying the segmentation
# initialise the segmentation change image
self.curSegChangeIMG = numpy.zeros(self.curIMGShape, dtype = numpy.bool)
self.initSegChanging()
if event.button == 1:
self.curSegModColour = numpy.array(self.segColours['add'])
self.curSegModMode = 'add'
elif event.button == 3:
self.curSegChangeIMG = numpy.zeros(self.curIMGShape, dtype = numpy.bool)
self.curSegModColour = numpy.array(self.segColours['del'])
self.curSegModMode = 'del'
else:
self.curSegChangeIMG = None
self.curSegModColour = None
self.curSegModMode = None
if not self.curSegModColour is None:
M = self.maskFromClick(imgCoords)
self.curSegChangeIMG = numpy.logical_or(self.curSegChangeIMG, M)
if self.CCSegs[self.curSubjectID]['finalSegManEdit'] is None:
self.CCSegs[self.curSubjectID]['finalSegManEdit'] = numpy.zeros(self.curIMGShape, dtype = numpy.bool)
T = numpy.array(self.curIMGRGB)
T = self.addSegToRGBIMG(T, self.CCSegs[self.curSubjectID]['finalSegManEdit'], self.segColours['seg'])
T = self.addSegToRGBIMG(T, self.curSegChangeIMG, self.curSegModColour)
T = numpy.transpose(T, [1, 2, 0])
A = gtk.gdk.pixbuf_new_from_array(T, gtk.gdk.COLORSPACE_RGB, 8)
A = A.scale_simple(self.resizeWidth, self.resizeHeight, gtk.gdk.INTERP_BILINEAR)
self.mainImage.set_from_pixbuf(A)
if data == "released":
# commit the changes to the segmentation
if self.curSegModMode == "add":
self.CCSegs[self.curSubjectID]['finalSegManEdit'] = numpy.logical_or(self.CCSegs[self.curSubjectID]['finalSegManEdit'], self.curSegChangeIMG)
elif self.curSegModMode == "del":
self.CCSegs[self.curSubjectID]['finalSegManEdit'] = numpy.logical_and(self.CCSegs[self.curSubjectID]['finalSegManEdit'], numpy.logical_not(self.curSegChangeIMG))
# update the image
if not self.curSegModMode is None:
T = numpy.array(self.curIMGRGB)
T = self.addSegToRGBIMG(T, self.CCSegs[self.curSubjectID]['finalSegManEdit'], self.segColours['seg'])
# = self.addSegToRGBIMG(T, self.curSegChangeIMG, self.curSegModColour)
T = numpy.transpose(T, [1, 2, 0])
A = gtk.gdk.pixbuf_new_from_array(T, gtk.gdk.COLORSPACE_RGB, 8)
A = A.scale_simple(self.resizeWidth, self.resizeHeight, gtk.gdk.INTERP_BILINEAR)
self.mainImage.set_from_pixbuf(A)
self.curSegChangeIMG = None
#self.curUndoIMG = None
#self.curUndoSubjectID = None
def onImageResize(self, widget, event, data = None):
#print "on_image_resize(self, widget, event, data = None)"
oldResizeWidth = self.resizeWidth
oldResizeHeight = self.resizeHeight
self.resetSizeOfMainImage(doNotCheckAllocation = False)
allocation = widget.get_allocation()
#print str(allocation.width) + " " + str(allocation.height)
if self.mainImageAllocationHeight != allocation.height or self.mainImageAllocationWidth != allocation.width or oldResizeWidth != self.resizeWidth or oldResizeHeight != self.resizeHeight:
#print "resizing"
self.mainImageAllocationHeight = allocation.height
self.mainImageAllocationWidth = allocation.width
T = numpy.array(self.curIMGRGB)
if not self.CCSegs[self.curSubjectID]['finalSegManEdit'] is None:
T = self.addSegToRGBIMG(T, self.CCSegs[self.curSubjectID]['finalSegManEdit'], self.segColours['seg'])
else:
T = self.addSegToRGBIMG(T, self.CCSegs[self.curSubjectID]['originalSeg'], self.segColours['seg'])
T = numpy.transpose(T, [1, 2, 0])
A = gtk.gdk.pixbuf_new_from_array(T, gtk.gdk.COLORSPACE_RGB, 8)
A = A.scale_simple(self.resizeWidth, self.resizeHeight, gtk.gdk.INTERP_BILINEAR)
widget.set_from_pixbuf(A)
def addSegToRGBIMG(self, IMG, SEG, segColour):
assert(IMG.shape[0] == 3),"IMG must be a 3 row array"
assert(IMG.ndim == 3),"IMG must be a 3 dimension array"
assert(numpy.array_equal(SEG.shape, (IMG.shape[1], IMG.shape[2]))),"SEG must have the same shape as IMG[1, 2]"
alphaChannel = numpy.zeros(SEG.shape)
alphaChannel[numpy.where(SEG)] = self.segOpacity.get_value()
T = numpy.double(IMG)
for z in range(3):
T[z] = (1 - alphaChannel) * T[z] + alphaChannel * segColour[z]
T = numpy.uint8(T)
return T
def updateSubject(self):
curSubjectIDX = int(self.curSubjectSlider.get_value()) - 1
self.curSubjectID = self.CCSegSubjectIDs[curSubjectIDX]
self.curSubjectLabel.set_text("Current subject: " + self.curSubjectID)
if curSubjectIDX == 0:
self.prevSubjectButton.set_sensitive(False)
self.nextSubjectButton.set_sensitive(True)
elif curSubjectIDX == len(self.CCSegSubjectIDs) - 1:
self.prevSubjectButton.set_sensitive(True)
self.nextSubjectButton.set_sensitive(False)
else:
self.prevSubjectButton.set_sensitive(True)
self.nextSubjectButton.set_sensitive(True)
self.curIMGShape = self.CCSegs[self.curSubjectID]['IMG'].shape
T = self.CCSegs[self.curSubjectID]['IMG']
T = (T - numpy.min(T)) / (numpy.max(T) - numpy.min(T))
T = numpy.uint8(numpy.round(T * 255))
T = numpy.expand_dims(T, axis = 0)
T = numpy.tile(T, (3, 1, 1))
self.curIMGRGB = numpy.array(T)
if not self.CCSegs[self.curSubjectID]['finalSegManEdit'] is None:
T = self.addSegToRGBIMG(T, self.CCSegs[self.curSubjectID]['finalSegManEdit'], self.segColours['seg'])
else:
T = self.addSegToRGBIMG(T, self.CCSegs[self.curSubjectID]['originalSeg'], self.segColours['seg'])
T = numpy.transpose(T, [1, 2, 0])
#self.imageWithSegRGB = numpy.array(T)
self.resetSizeOfMainImage(doNotCheckAllocation = True)
A = gtk.gdk.pixbuf_new_from_array(T, gtk.gdk.COLORSPACE_RGB, 8)
A = A.scale_simple(self.resizeWidth, self.resizeHeight, gtk.gdk.INTERP_BILINEAR)
self.mainImage.set_from_pixbuf(A)
def setSegs(self, CCSegs, inputDirectory):
self.CCSegs = copy.deepcopy(CCSegs)
self.CCSegSubjectIDs = sorted(CCSegs.keys())
self.subjectsToSave = numpy.zeros(len(self.CCSegSubjectIDs), dtype = numpy.bool)
self.inputDirectory = inputDirectory
self.curSubjectSlider.set_adjustment(gtk.Adjustment(value = 1, lower = 1, upper = len(self.CCSegs), step_incr = 1, page_incr = 1, page_size=0))
self.updateSubject()
def setStartSubject(self, startSubjectIDX):
self.curSubjectSlider.set_value(startSubjectIDX)
self.updateSubject()
def main():
opts, args = getopt.getopt(sys.argv[1:], "h", ['start-subject='])
# initialise options
# mandatory arguments
inputDirectory = None
# do the mandatory arguments first
if len(args) != 1:
print "The number of arguments must be 1"
usage()
exit()
numpy.set_printoptions(precision = 3, formatter = {'float':lambda x: "%.3f" % x})
startSubject = None
for o, a in opts:
if o == '--start-subject':
startSubject = a
inputDirectory = args[0]
# get a list of nifti files in the input directory
inputDirectoryFileList = os.listdir(inputDirectory)
inputSegHDF5Files = [x for x in sorted(inputDirectoryFileList) if x.endswith("_seg.hdf5")]
inputSegHDF5FilesPrefixes = [x[:-len("_seg.hdf5")] for x in inputSegHDF5Files]
if startSubject is None:
startSubjectIDX = 0
else:
try:
startSubjectIDX = inputSegHDF5FilesPrefixes.index(startSubject) + 1
print "Start subject found at index: " + str(startSubjectIDX)
except ValueError:
print "Warning: start subject not found, starting at first subject"
startSubjectIDX = 0
pass
#inputSegHDF5Files = [x + "_seg.hdf5" for x in inputThicknessHDF5FilesPrefixes]
#inputSegManeditHDF5Files = [x + "_seg_manedit.hdf5" for x in inputThicknessHDF5FilesPrefixes]
CCSegs = dict()
for curFile in inputSegHDF5FilesPrefixes:
CCSegs[curFile] = dict()
FID = h5py.File(os.path.join(inputDirectory, curFile + "_seg.hdf5"), 'r')
CCSegs[curFile]['IMG'] = numpy.array(FID['IMG'])
if os.path.isfile(os.path.join(inputDirectory, curFile + "_seg_manedit.hdf5")):
#print curFile + "_seg_manedit.hdf5"
FIDManedit = h5py.File(os.path.join(inputDirectory, curFile + "_seg_manedit.hdf5"), 'r')
CCSegs[curFile]['finalSegManEdit'] = numpy.array(FIDManedit['finalSegManEdit']) > 0
FIDManedit.close()
else:
#print curFile + "_seg.hdf5"
try:
CCSegs[curFile]['originalSeg'] = numpy.array(FID['watershedSeg'])
except Exception:
CCSegs[curFile]['originalSeg'] = numpy.array(FID['finalSegNoArtefacts'])
CCSegs[curFile]['originalSeg'] = numpy.array(CCSegs[curFile]['originalSeg']) > 0
CCSegs[curFile]['finalSegManEdit'] = None
FID.close()
#print CCSegs
if len(CCSegs) == 0:
print "No segmentation hdf5 files found"
quit()
UIExample = UIManagerExample()
UIExample.setSegs(CCSegs, inputDirectory)
UIExample.setStartSubject(startSubjectIDX)
gtk.main()
if __name__ == "__main__":
main()