#============================================================================== # # Project: SharpImage # Module: MorphologicalClose.py # Language: IronPython # Author: Dan Mueller # Date: $Date: 2007-07-13 06:30:23 +1000 (Fri, 13 Jul 2007) $ # Revision: $Revision: 8 $ # # 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.MorphologicalFilters") from itk import * class MorphologicalCloseScript(ImageToImageScriptObject): # ------------------------------------------------------------------------- Name = "MorphologicalClose" Help = """Computes the greyscale morphological closing of an image. This filter removes small (i.e. smaller than the binary ball structuring element) holes and tube like structures in the interior or at the boundaries of the image. The morphological closing of an image "I" is defined as: \tClosing(I) = Erode( Dilate(I) )""" Parameters = """(itkSize) KernelRadius = the radius of the ball. (itkSize(1,1,.)) (itkFlatStructuringElement) Kernel = the structuring element. (Ball) (string) Operation = either "Binary" or "Grayscale". ("Grayscale") (pixel) ForegroundValue = for Operation="Binary", sets the value considered as foreground. (pixel) BackgroundValue = for Operation="Binary", sets the value considered as background.""" KernelRadius = None Kernel = None Operation = "Grayscale" ForegroundValue = None BackgroundValue = None # ------------------------------------------------------------------------- def ThreadedDoWork(self): """ Perform the main functions of the script on a background thread. """ try: self.StartedWork() self.WriteInputName() filterClose = None if (String.Compare( self.Operation, "Binary", True ) == 0): filterClose = itkBinaryClosingImageFilter.New( self.Input, self.Output ) if (self.ForegroundValue != None): filterClose.ForegroundValue = self.ForegroundValue self.WriteLineToConsole( "ForegroundValue=" + filterClose.ForegroundValue.ToString() ) if (self.BackgroundValue != None): filterClose.BackgroundValue = self.BackgroundValue self.WriteLineToConsole( "BackgroundValue=" + filterClose.BackgroundValue.ToString() ) else: 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.Ball( self.KernelRadius ); self.WriteLineToConsole( "Kernel=" + self.Kernel.ToString() ) filterClose.Kernel = self.Kernel 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( )