#============================================================================== # # Project: SharpImage # Module: MorphologicalOpen.py # Language: IronPython # Author: Dan Mueller # Date: $Date$ # Revision: $Revision$ # # Copyright (c) Queensland University of Technology (QUT) 2007. # All rights reserved. # # This software is distributed WITHOUT ANY WARRANTY; without even # the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR # PURPOSE. See the above copyright notices for more information. # #============================================================================== # Import the base script class import ImageToImageScript from ImageToImageScript import * # Add reference and import Morphology library clr.AddReference("ManagedITK.FastMorphologicalFilters") from itk import * class FastMorphologicalOpenScript(ImageToImageScriptObject): # ------------------------------------------------------------------------- Name = "FastMorphologicalOpen" Help = """Computes the fast greyscale morphological opening of an image. This filter preserves regions, in the foreground, that can completely contain the binary ball structuring element. At the same time, this filter eliminates all other regions of foreground pixels. The morphological opening of an image "I" is defined as: \tOpening(I) = Dilate( Erode(I) )""" Parameters = """(itkSize) KernelRadius = the radius of the ball. (itkSize(1,1,.)) (itkFlatStructuringElement) Kernel = the structuring element. (Box) (FastMorphologyEnum) Algorithm = Basic, Histogram, Anchor, or VHGW. (VHGW)""" KernelRadius = None Kernel = None Algorithm = FastMorphologyEnum.VHGW # ------------------------------------------------------------------------- def ThreadedDoWork(self): """ Perform the main functions of the script on a background thread. """ try: self.StartedWork() self.WriteInputName() filterOpen = itkGrayscaleMorphologicalOpeningImageFilter.New( self.Input, self.Output ) self.AddEventHandlersToProcessObject( filterOpen ) filterOpen.SetInput( self.Input ) if (self.KernelRadius == None): self.KernelRadius = itkSize( self.Input.Dimension ) self.KernelRadius.Fill( 1 ) if (self.Kernel == None): self.Kernel = itkFlatStructuringElement.Box( self.KernelRadius ); filterOpen.Kernel = self.Kernel filterOpen.Algorithm = self.Algorithm self.WriteLineToConsole( "Kernel=" + self.Kernel.ToString() ) self.WriteLineToConsole( "Algorithm=" + self.Algorithm.ToString() ) filterOpen.UpdateLargestPossibleRegion() filterOpen.GetOutput( self.Output ) self.DisconnectInputAndOutput() self.DisposeOfObject( filterOpen ) self.WriteOutputName() self.FinishedWork( True ) except Exception, ex: self.HandleException( ex ) self.FinishedWork( False ) self.Finalise( )