In [1]:
## Boiler plate code common to many notebooks.  See the TestFilesCommonCode.ipynb for details
from __future__ import print_function
%run TestFilesCommonCode.ipynb


SimpleITK Version: 0.9.0.dev2032-gf9618
Compiled: Aug 12 2014 21:10:05


In [2]:
img = sitk.ReadImage(fdata('ITK/Testing/Data/Input/Spots.png'))
myshow(img)


Verified download for /Users/blowekamp/src/SimpleITK-Notebook-Answers/Data/ITK/Testing/Data/Input/Spots.png

In [3]:
img = sitk.ConnectedComponent(img==0)
myshow(sitk.LabelToRGB(img))
myshow(sitk.LabelToRGB((img==5)*5))



In [4]:
help(sitk.BinaryErodeImageFilter)


Help on class BinaryErodeImageFilter in module SimpleITK.SimpleITK:

class BinaryErodeImageFilter(ImageFilter_1)
 |  Fast binary erosion.
 |  
 |  
 |  BinaryErodeImageFilteris a binary erosion morphologic operation. This
 |  implementation is based on the papers:
 |  
 |  L.Vincent "Morphological transformations of binary images with
 |  arbitrary structuring elements", and
 |  
 |  N.Nikopoulos et al. "An efficient algorithm for 3d binary
 |  morphological transformations with 3d structuring elements for
 |  arbitrary size and shape". IEEE Transactions on ImageProcessing. Vol.
 |  9. No. 3. 2000. pp. 283-286.
 |  
 |  Gray scale images can be processed as binary images by selecting a
 |  "ErodeValue". Pixel values matching the erode value are considered
 |  the "foreground" and all other pixels are "background". This is
 |  useful in processing segmented images where all pixels in segment #1
 |  have value 1 and pixels in segment #2 have value 2, etc. A particular
 |  "segment number" can be processed. ErodeValue defaults to the
 |  maximum possible value of the PixelType. The eroded pixels will
 |  receive the BackgroundValue (defaults to 0).
 |  
 |  The structuring element is assumed to be composed of binary values
 |  (zero or one). Only elements of the structuring element having values
 |  > 0 are candidates for affecting the center pixel. A reasonable choice
 |  of structuring element is itk::BinaryBallStructuringElement.
 |  
 |  
 |  See:
 |  ImageToImageFilter BinaryDilateImageFilter BinaryMorphologyImageFilter
 |  Wiki Examples:
 |  
 |  All Examples
 |  
 |  Erode a binary image
 |  See:
 |   itk::simple::BinaryErode for the procedural interface
 |  
 |  
 |  
 |  C++ includes: sitkBinaryErodeImageFilter.h
 |  
 |  Method resolution order:
 |      BinaryErodeImageFilter
 |      ImageFilter_1
 |      ProcessObject
 |      __builtin__.object
 |  
 |  Methods defined here:
 |  
 |  BoundaryToForegroundOff(self)
 |      BoundaryToForegroundOff(BinaryErodeImageFilter self) -> BinaryErodeImageFilter
 |  
 |  BoundaryToForegroundOn(self)
 |      BoundaryToForegroundOn(BinaryErodeImageFilter self) -> BinaryErodeImageFilter
 |      
 |      Set the value of BoundaryToForeground to true or false respectfully.
 |  
 |  Execute(self, *args)
 |      Execute(BinaryErodeImageFilter self, Image image1) -> Image
 |      Execute(BinaryErodeImageFilter self, Image image1, double backgroundValue, double foregroundValue, bool boundaryToForeground) -> Image
 |      
 |      Execute the filter on the input image with the given parameters
 |  
 |  GetBackgroundValue(self)
 |      GetBackgroundValue(BinaryErodeImageFilter self) -> double
 |  
 |  GetBoundaryToForeground(self)
 |      GetBoundaryToForeground(BinaryErodeImageFilter self) -> bool
 |  
 |  GetForegroundValue(self)
 |      GetForegroundValue(BinaryErodeImageFilter self) -> double
 |  
 |  GetKernelRadius(self)
 |      GetKernelRadius(BinaryErodeImageFilter self) -> VectorUInt32
 |  
 |  GetKernelType(self)
 |      GetKernelType(BinaryErodeImageFilter self) -> itk::simple::KernelEnum
 |  
 |  GetName(self)
 |      GetName(BinaryErodeImageFilter self) -> std::string
 |      
 |      Name of this class
 |  
 |  SetBackgroundValue(self, *args, **kwargs)
 |      SetBackgroundValue(BinaryErodeImageFilter self, double BackgroundValue) -> BinaryErodeImageFilter
 |  
 |  SetBoundaryToForeground(self, *args, **kwargs)
 |      SetBoundaryToForeground(BinaryErodeImageFilter self, bool BoundaryToForeground) -> BinaryErodeImageFilter
 |  
 |  SetForegroundValue(self, *args, **kwargs)
 |      SetForegroundValue(BinaryErodeImageFilter self, double ForegroundValue) -> BinaryErodeImageFilter
 |  
 |  SetKernelRadius(self, *args)
 |      SetKernelRadius(BinaryErodeImageFilter self, uint32_t r) -> BinaryErodeImageFilter
 |      SetKernelRadius(BinaryErodeImageFilter self, VectorUInt32 r) -> BinaryErodeImageFilter
 |      
 |      Set/Get the radius of the kernel structuring element as a vector.
 |      
 |      If the dimension of the image is greater then the length of r, then
 |      the radius will be padded. If it is less the r will be truncated.
 |  
 |  SetKernelType(self, *args, **kwargs)
 |      SetKernelType(BinaryErodeImageFilter self, itk::simple::KernelEnum t) -> BinaryErodeImageFilter
 |  
 |  __del__ lambda self
 |  
 |  __getattr__ lambda self, name
 |  
 |  __init__(self)
 |      __init__(itk::simple::BinaryErodeImageFilter self) -> BinaryErodeImageFilter
 |      
 |      Default Constructor that takes no arguments and initializes default
 |      parameters
 |  
 |  __repr__ = _swig_repr(self)
 |  
 |  __setattr__ lambda self, name, value
 |  
 |  __str__(self)
 |      __str__(BinaryErodeImageFilter self) -> std::string
 |      
 |      Print ourselves out
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  Annulus = 0
 |  
 |  Ball = 1
 |  
 |  Box = 2
 |  
 |  Cross = 3
 |  
 |  __swig_destroy__ = <built-in function delete_BinaryErodeImageFilter>
 |      delete_BinaryErodeImageFilter(BinaryErodeImageFilter self)
 |  
 |  __swig_getmethods__ = {'GetGlobalDefaultDebug': <function <lambda>>, '...
 |  
 |  __swig_setmethods__ = {}
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from ProcessObject:
 |  
 |  Abort(self)
 |      Abort(ProcessObject self)
 |      
 |      Sets an abort flag on the active process.
 |      
 |      Requests the current active process to abort. Additional, progress or
 |      iteration event may occur. If aborted then, an AbortEvent should
 |      occur. The Progress should be set to 1.0 after aborting.
 |      
 |      The expected behavior is that not exception should be throw out of
 |      this processes Execute method. Additionally, the results returned are
 |      valid but undefined content. The content may be only partially
 |      updated, uninitialized or the a of size zero.
 |      
 |      If there is no active process the method has no effect.
 |  
 |  AddCommand(self, *args)
 |      AddCommand(ProcessObject self, itk::simple::EventEnum event, Command cmd) -> int
 |      AddCommand(ProcessObject self, itk::simple::EventEnum e, PyObject * obj) -> int
 |      
 |      Add a Command Object to observer the event.
 |      
 |      
 |      The Command object's Execute method will be invoked when the internal ITK Object
 |      has the event. These events only occur during this ProcessObject's Execute method when the ITK filter is running. The command occurs
 |      in the same thread as this objects Execute methods was called in.
 |      
 |      An internal reference is made between the Command and this ProcessObject which enable automatic removal of the command when deleted. This
 |      enables both object to exist as stack based object and be
 |      automatically cleaned up.
 |      
 |      Unless specified otherwise, it's safe to get any value during
 |      execution. "Measurements" will have valid values only after the
 |      Execute method has returned. "Active Measurements" will have valid
 |      values during events, and access the underlying ITK object.
 |      
 |      Deleting a registered command during execution causes program
 |      termination.
 |      
 |      For more information see the page Commands and Events for SimpleITK.
 |      
 |      
 |      The return value is reserved for latter usage.
 |  
 |  DebugOff(self)
 |      DebugOff(ProcessObject self)
 |  
 |  DebugOn(self)
 |      DebugOn(ProcessObject self)
 |  
 |  GetDebug(self)
 |      GetDebug(ProcessObject self) -> bool
 |  
 |  GetNumberOfThreads(self)
 |      GetNumberOfThreads(ProcessObject self) -> unsigned int
 |  
 |  GetProgress(self)
 |      GetProgress(ProcessObject self) -> float
 |      
 |      An Active Measurement of the progress of execution.
 |      
 |      
 |      Get the execution progress of the current process object. The progress
 |      is a floating number in [0,1] with 0 meaning no progress and 1 meaning
 |      the filter has completed execution (or aborted).
 |      
 |      This is an Active Measurement so it can be accessed during Events
 |      during the execution.
 |  
 |  HasCommand(self, *args, **kwargs)
 |      HasCommand(ProcessObject self, itk::simple::EventEnum event) -> bool
 |      
 |      Query of this object has any registered commands for event.
 |  
 |  RemoveAllCommands(self)
 |      RemoveAllCommands(ProcessObject self)
 |      
 |      Remove all registered commands.
 |  
 |  SetDebug(self, *args, **kwargs)
 |      SetDebug(ProcessObject self, bool debugFlag)
 |  
 |  SetNumberOfThreads(self, *args, **kwargs)
 |      SetNumberOfThreads(ProcessObject self, unsigned int n)
 |  
 |  ----------------------------------------------------------------------
 |  Static methods inherited from ProcessObject:
 |  
 |  GetGlobalDefaultDebug()
 |      GetGlobalDefaultDebug() -> bool
 |  
 |  GetGlobalDefaultNumberOfThreads()
 |      GetGlobalDefaultNumberOfThreads() -> unsigned int
 |  
 |  GetGlobalWarningDisplay()
 |      GetGlobalWarningDisplay() -> bool
 |  
 |  GlobalDefaultDebugOff()
 |      GlobalDefaultDebugOff()
 |  
 |  GlobalDefaultDebugOn()
 |      GlobalDefaultDebugOn()
 |  
 |  GlobalWarningDisplayOff()
 |      GlobalWarningDisplayOff()
 |  
 |  GlobalWarningDisplayOn()
 |      GlobalWarningDisplayOn()
 |  
 |  SetGlobalDefaultDebug(*args, **kwargs)
 |      SetGlobalDefaultDebug(bool debugFlag)
 |  
 |  SetGlobalDefaultNumberOfThreads(*args, **kwargs)
 |      SetGlobalDefaultNumberOfThreads(unsigned int n)
 |  
 |  SetGlobalWarningDisplay(*args, **kwargs)
 |      SetGlobalWarningDisplay(bool flag)
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from ProcessObject:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)


In [5]:
erodeFilter = sitk.BinaryErodeImageFilter()
erodeFilter.SetKernelRadius(8)
erodeFilter.SetForegroundValue(5)
out = erodeFilter.Execute(img)

In [6]:
myshow(sitk.LabelToRGB(out))