Slicer  4.10
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 from SegmentEditorEffects import *
5 
7  """ AutoCompleteEffect is an effect that can create a full segmentation
8  from a partial segmentation (not all slices are segmented or only
9  part of the target structures are painted).
10  """
11 
12  def __init__(self, scriptedEffect):
13  AbstractScriptedSegmentEditorAutoCompleteEffect.__init__(self, scriptedEffect)
14  scriptedEffect.name = 'Grow from seeds'
16  self.clippedMasterImageDataRequired = True # master volume intensities are used by this effect
17  self.clippedMaskImageDataRequired = True # masking is used
18  self.growCutFilter = None
19 
20  def clone(self):
21  import qSlicerSegmentationsEditorEffectsPythonQt as effects
22  clonedEffect = effects.qSlicerSegmentEditorScriptedEffect(None)
23  clonedEffect.setPythonSource(__file__.replace('\\','/'))
24  return clonedEffect
25 
26  def icon(self):
27  iconPath = os.path.join(os.path.dirname(__file__), 'Resources/Icons/GrowFromSeeds.png')
28  if os.path.exists(iconPath):
29  return qt.QIcon(iconPath)
30  return qt.QIcon()
31 
32  def helpText(self):
33  return """<html>Growing segments to create complete segmentation<br>.
34 Location, size, and shape of initial segments and content of master volume are taken into account.
35 Final segment boundaries will be placed where master volume brightness changes abruptly. Instructions:<p>
36 <ul style="margin: 0">
37 <li>Use Paint or other offects to draw seeds in each region that should belong to a separate segment.
38 Paint each seed with a different segment. Minimum two segments are required.</li>
39 <li>Click <dfn>Initialize</dfn> to compute preview of full segmentation.</li>
40 <li>Browse through image slices. If previewed segmentation result is not correct then switch to
41 Paint or other effects and add more seeds in the misclassified region. Full segmentation will be
42 updated automatically within a few seconds</li>
43 <li>Click <dfn>Apply</dfn> to update segmentation with the previewed result.</li>
44 </ul><p>
45 If segments overlap, segment higher in the segments table will have priority.
46 The effect uses <a href="https://www.spl.harvard.edu/publications/item/view/2761">fast grow-cut method</a>.
47 <p></html>"""
48 
49 
50  def reset(self):
51  self.growCutFilter = None
52  AbstractScriptedSegmentEditorAutoCompleteEffect.reset(self)
53  self.updateGUIFromMRML()
54 
55  def computePreviewLabelmap(self, mergedImage, outputLabelmap):
56  import vtkSlicerSegmentationsModuleLogicPython as vtkSlicerSegmentationsModuleLogic
57 
58  if not self.growCutFilter:
59  self.growCutFilter = vtkSlicerSegmentationsModuleLogic.vtkImageGrowCutSegment()
60  self.growCutFilter.SetIntensityVolume(self.clippedMasterImageData)
61  self.growCutFilter.SetMaskVolume(self.clippedMaskImageData)
62  maskExtent = self.clippedMaskImageData.GetExtent() if self.clippedMaskImageData else None
63  if maskExtent is not None and maskExtent[0] <= maskExtent[1] and maskExtent[2] <= maskExtent[3] and maskExtent[4] <= maskExtent[5]:
64  # Mask is used.
65  # Grow the extent more, as background segment does not surround region of interest.
66  self.extentGrowthRatio = 0.50
67  else:
68  # No masking is used.
69  # Background segment is expected to surround region of interest, so narrower margin is enough.
70  self.extentGrowthRatio = 0.20
71 
73 
74  self.growCutFilter.SetSeedLabelVolume(mergedImage)
75  self.growCutFilter.Update()
76 
77  outputLabelmap.DeepCopy( self.growCutFilter.GetOutput() )