/
SegmentEditorErodeEffect.py
71 lines (58 loc) · 2.48 KB
/
SegmentEditorErodeEffect.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
import os
import vtk, qt, ctk, slicer
import logging
from SegmentEditorEffects import *
class SegmentEditorErodeEffect(AbstractScriptedSegmentEditorMorphologyEffect):
""" ErodeEffect is an MorphologyEffect to
erode a layer of pixels from a segment
"""
# Necessary static member to be able to set python source to scripted subject hierarchy plugin
filePath = __file__
def __init__(self, scriptedEffect):
scriptedEffect.name = 'Erode'
AbstractScriptedSegmentEditorMorphologyEffect.__init__(self, scriptedEffect)
def clone(self):
import qSlicerSegmentationsEditorEffectsPythonQt as effects
clonedEffect = effects.qSlicerSegmentEditorScriptedMorphologyEffect(None)
clonedEffect.setPythonSource(SegmentEditorErodeEffect.filePath)
return clonedEffect
def icon(self):
iconPath = os.path.join(os.path.dirname(__file__), 'Resources/Icons/Erode.png')
if os.path.exists(iconPath):
return qt.QIcon(iconPath)
return qt.QIcon()
def helpText(self):
return "Remove pixels from the boundary of selected segment."
def setupOptionsFrame(self):
self.applyButton = qt.QPushButton("Apply")
self.applyButton.objectName = self.__class__.__name__ + 'Apply'
self.applyButton.setToolTip("Erode selected segment")
self.scriptedEffect.addOptionsWidget(self.applyButton)
self.applyButton.connect('clicked()', self.onApply)
def createCursor(self, widget):
# Turn off effect-specific cursor for this effect
return slicer.util.mainWindow().cursor
def onApply(self):
# Get parameters
neighborMode = self.scriptedEffect.integerParameter("NeighborMode")
iterations = self.scriptedEffect.integerParameter("Iterations")
# Get edited labelmap
editedLabelmap = self.scriptedEffect.editedLabelmap()
selectedSegmentLabelmap = self.scriptedEffect.selectedSegmentLabelmap()
# Perform erosion
eroder = slicer.vtkImageErode()
eroder.SetInputData(selectedSegmentLabelmap)
eroder.SetForeground(1)
eroder.SetBackground(0)
if neighborMode == 8:
eroder.SetNeighborTo8()
elif neighborMode == 4:
eroder.SetNeighborTo4()
else:
logging.error("Invalid neighbor mode!")
for i in xrange(iterations):
eroder.Update()
editedLabelmap.DeepCopy(eroder.GetOutput())
# Notify editor about changes.
# This needs to be called so that the changes are written back to the edited segment
self.scriptedEffect.apply()