Slicer  4.11
Slicer is a multi-platform, free and open source software package for visualization and medical image computing
SegmentEditorGrowFromSeedsEffect.py
Go to the documentation of this file.
1 import os
2 import vtk, qt, ctk, slicer
3 import logging
4 import time
5 from SegmentEditorEffects import *
6 
8  """ AutoCompleteEffect is an effect that can create a full segmentation
9  from a partial segmentation (not all slices are segmented or only
10  part of the target structures are painted).
11  """
12 
13  def __init__(self, scriptedEffect):
14  AbstractScriptedSegmentEditorAutoCompleteEffect.__init__(self, scriptedEffect)
15  scriptedEffect.name = 'Grow from seeds'
17  self.clippedMasterImageDataRequired = True # master volume intensities are used by this effect
18  self.clippedMaskImageDataRequired = True # masking is used
19  self.growCutFilter = None
20 
21  def clone(self):
22  import qSlicerSegmentationsEditorEffectsPythonQt as effects
23  clonedEffect = effects.qSlicerSegmentEditorScriptedEffect(None)
24  clonedEffect.setPythonSource(__file__.replace('\\','/'))
25  return clonedEffect
26 
27  def icon(self):
28  iconPath = os.path.join(os.path.dirname(__file__), 'Resources/Icons/GrowFromSeeds.png')
29  if os.path.exists(iconPath):
30  return qt.QIcon(iconPath)
31  return qt.QIcon()
32 
33  def helpText(self):
34  return """<html>Growing segments to create complete segmentation<br>.
35 Location, size, and shape of initial segments and content of master volume are taken into account.
36 Final segment boundaries will be placed where master volume brightness changes abruptly. Instructions:<p>
37 <ul style="margin: 0">
38 <li>Use Paint or other offects to draw seeds in each region that should belong to a separate segment.
39 Paint each seed with a different segment. Minimum two segments are required.</li>
40 <li>Click <dfn>Initialize</dfn> to compute preview of full segmentation.</li>
41 <li>Browse through image slices. If previewed segmentation result is not correct then switch to
42 Paint or other effects and add more seeds in the misclassified region. Full segmentation will be
43 updated automatically within a few seconds</li>
44 <li>Click <dfn>Apply</dfn> to update segmentation with the previewed result.</li>
45 </ul><p>
46 If segments overlap, segment higher in the segments table will have priority.
47 The effect uses <a href="http://interactivemedical.org/imic2014/CameraReadyPapers/Paper%204/IMIC_ID4_FastGrowCut.pdf">fast grow-cut method</a>.
48 <p></html>"""
49 
50 
51  def reset(self):
52  self.growCutFilter = None
53  AbstractScriptedSegmentEditorAutoCompleteEffect.reset(self)
54  self.updateGUIFromMRML()
55 
56  def setupOptionsFrame(self):
57  AbstractScriptedSegmentEditorAutoCompleteEffect.setupOptionsFrame(self)
58 
59  # Object scale slider
60  self.seedLocalityFactorSlider = slicer.qMRMLSliderWidget()
61  self.seedLocalityFactorSlider.setMRMLScene(slicer.mrmlScene)
62  self.seedLocalityFactorSlider.minimum = 0
63  self.seedLocalityFactorSlider.maximum = 10
64  self.seedLocalityFactorSlider.value = 0.0
65  self.seedLocalityFactorSlider.decimals = 1
66  self.seedLocalityFactorSlider.singleStep = 0.1
67  self.seedLocalityFactorSlider.pageStep = 1.0
68  self.seedLocalityFactorSlider.setToolTip('Increasing this value makes the effect of seeds more localized,'
69  ' thereby reducing leaks, but requires seed regions to be more evenly distributed in the image.'
70  ' The value is specified as an additional "intensity level difference" per "unit distance."')
71  self.scriptedEffect.addLabeledOptionsWidget("Seed locality:", self.seedLocalityFactorSlider)
72  self.seedLocalityFactorSlider.connect('valueChanged(double)', self.updateAlgorithmParameterFromGUI)
73 
74  def setMRMLDefaults(self):
75  AbstractScriptedSegmentEditorAutoCompleteEffect.setMRMLDefaults(self)
76  self.scriptedEffect.setParameterDefault("SeedLocalityFactor", 0.0)
77 
78  def updateGUIFromMRML(self):
79  AbstractScriptedSegmentEditorAutoCompleteEffect.updateGUIFromMRML(self)
80  if self.scriptedEffect.parameterDefined("SeedLocalityFactor"):
81  seedLocalityFactor = self.scriptedEffect.doubleParameter("SeedLocalityFactor")
82  else:
83  seedLocalityFactor = 0.0
84  wasBlocked = self.seedLocalityFactorSlider.blockSignals(True)
85  self.seedLocalityFactorSlider.value = abs(seedLocalityFactor)
86  self.seedLocalityFactorSlider.blockSignals(wasBlocked)
87 
88  def updateMRMLFromGUI(self):
89  AbstractScriptedSegmentEditorAutoCompleteEffect.updateMRMLFromGUI(self)
90  self.scriptedEffect.setParameter("SeedLocalityFactor", self.seedLocalityFactorSlider.value)
91 
93  self.updateMRMLFromGUI()
94 
95  # Trigger preview update
96  if self.getPreviewNode():
97  self.delayedAutoUpdateTimer.start()
98 
99  def computePreviewLabelmap(self, mergedImage, outputLabelmap):
100  import vtkSlicerSegmentationsModuleLogicPython as vtkSlicerSegmentationsModuleLogic
101 
102  if not self.growCutFilter:
103  self.growCutFilter = vtkSlicerSegmentationsModuleLogic.vtkImageGrowCutSegment()
104  self.growCutFilter.SetIntensityVolume(self.clippedMasterImageData)
105  self.growCutFilter.SetMaskVolume(self.clippedMaskImageData)
106  maskExtent = self.clippedMaskImageData.GetExtent() if self.clippedMaskImageData else None
107  if maskExtent is not None and maskExtent[0] <= maskExtent[1] and maskExtent[2] <= maskExtent[3] and maskExtent[4] <= maskExtent[5]:
108  # Mask is used.
109  # Grow the extent more, as background segment does not surround region of interest.
110  self.extentGrowthRatio = 0.50
111  else:
112  # No masking is used.
113  # Background segment is expected to surround region of interest, so narrower margin is enough.
114  self.extentGrowthRatio = 0.20
115 
116  if self.scriptedEffect.parameterDefined("SeedLocalityFactor"):
117  seedLocalityFactor = self.scriptedEffect.doubleParameter("SeedLocalityFactor")
118  else:
119  seedLocalityFactor = 0.0
120  self.growCutFilter.SetDistancePenalty(seedLocalityFactor)
121  self.growCutFilter.SetSeedLabelVolume(mergedImage)
122  startTime = time.time()
123  self.growCutFilter.Update()
124  logging.info('Grow-cut operation on volume of {0}x{1}x{2} voxels was completed in {3:3.1f} seconds.'.format(
125  self.clippedMasterImageData.GetDimensions()[0],
126  self.clippedMasterImageData.GetDimensions()[1],
127  self.clippedMasterImageData.GetDimensions()[2],
128  time.time() - startTime))
129 
130  outputLabelmap.DeepCopy( self.growCutFilter.GetOutput() )