#============================================================================== # # Project: SharpImage # Module: MorphologicalOpenByReconstruction.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 MorphologicalOpenByReconstructionScript(ImageToImageScriptObject): # ------------------------------------------------------------------------- Name = "MorphologicalOpenByReconstruction" Help = """Computes the greyscale morphological opening by reconstruction. This fitler is similar to MorpholicalOpen, except that opening by reconstruction preserves the shape of the components. The morphological opening by reconstruction of an image "I" is defined as: \tOpeningByReconstruction(I) = DilateByReconstruction( ErodeByReconstruction(I) )""" Parameters = """(itkSize) KernelRadius = the radius of the ball. (itkSize(1,1,.)) (itkFlatStructuringElement) Kernel = the structuring element. (Ball)""" KernelRadius = None Kernel = None # ------------------------------------------------------------------------- def ThreadedDoWork(self): """ Perform the main functions of the script on a background thread. """ try: self.StartedWork() self.WriteInputName() filterOpen = itkOpeningByReconstructionImageFilter.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.Ball( self.KernelRadius ); filterOpen.Kernel = self.Kernel filterOpen.FullyConnected = False filterOpen.PreserveIntensities = True self.WriteLineToConsole( "Kernel=" + self.Kernel.ToString() ) self.WriteLineToConsole( "FullyConnected=" + filterOpen.FullyConnected.ToString() ) self.WriteLineToConsole( "PreserveIntensities=" + filterOpen.PreserveIntensities.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( )