#============================================================================== # # Project: SharpImage # Module: MorphologicalClose.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 FastMorphologicalCloseScript(ImageToImageScriptObject): # ------------------------------------------------------------------------- Name = "FastMorphologicalClose" Help = """Computes the fast greyscale morphological closing 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 closing of an image "I" is defined as: \tCloseing(I) = Erode( Dilate(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() filterClose = itkGrayscaleMorphologicalClosingImageFilter.New( self.Input, self.Output ) self.AddEventHandlersToProcessObject( filterClose ) filterClose.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 ); filterClose.Kernel = self.Kernel filterClose.Algorithm = self.Algorithm self.WriteLineToConsole( "Kernel=" + self.Kernel.ToString() ) self.WriteLineToConsole( "Algorithm=" + self.Algorithm.ToString() ) filterClose.UpdateLargestPossibleRegion() filterClose.GetOutput( self.Output ) self.DisconnectInputAndOutput() self.DisposeOfObject( filterClose ) self.WriteOutputName() self.FinishedWork( True ) except Exception, ex: self.HandleException( ex ) self.FinishedWork( False ) self.Finalise( )