#============================================================================== # # Project: SharpImage # Module: ConfidenceConnected.py # Language: IronPython # Author: Dan Mueller # Date: $Date: 2007-07-06 10:57:00 +1000 (Fri, 06 Jul 2007) $ # Revision: $Revision: 2 $ # # 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 Threshold library clr.AddReference("ManagedITK.ThresholdFilters") from itk import * class ConfidenceConnectedScript(ImageToImageScriptObject): # ------------------------------------------------------------------------- Name = "ConfidenceConnected" Help = """This filter extracts a connected set of pixels whose pixel intensities are consistent with the pixel statistics of a seed point. The mean and variance across a neighborhood (8-connected, 26-connected, etc.) are calculated for a seed point. Then pixels connected to this seed point whose values are within the confidence interval for the seed point are grouped. The segmentation is then recalculated using these refined estimates for the mean and variance of the pixel values. This process is repeated for the specified number of iterations.""" Parameters = """(double) Multiplier = The value used to define the confidence interval. (2.5) (pixel) ReplaceValue = The value for pixels included by the segmentation. (255) (int) NeighborhoodRadius = The neighborhood radius used to compute the statistics. (1) (int) NumberOfIterations = The number of iterations to perform. (4)""" Multiplier = 2.5 ReplaceValue = 255 NeighborhoodRadius = 1 NumberOfIterations = 4 Form = None Seeds = None # ------------------------------------------------------------------------- def Run(self): """ The entry-point for this script. """ self.Initialise() def Initialise(self): """ Initialise the environment for running this script. """ """ NOTE: This function is invoked on the main UI thread. """ ImageToImageScriptObject.Initialise( self ) # Init ReplaceValue if (self.ReplaceValue == None): self.ReplaceValue = 255 # Create Form self.Form = siFormSelectSeeds( self.Renderer ) self.Form.Continue += self.Continue self.Form.Cancel += self.Cancel self.ParentApplication.AddTool( self.Form ) # Start processing self.StartedWork() self.WriteLineToConsole( "User selecting seeds..." ) self.ParentApplication.SetApplicationAsReady( 0 ) def Continue(self, sender, args): """ Allow processing to continue. """ self.Seeds = self.Form.PointsSelection.GetIndicies( self.Input ) self.DoWork() def Cancel(self, sender, args): """ Cancel the script. """ self.WriteLineToConsole( "Script cancelled by user..." ) self.UnregisterEventHandlers() self.FinishedWork( True ) def ThreadedDoWork(self): """ Perform the main functions of the script. """ """ NOTE: This function is invoked on a background thread. """ try: self.ParentApplication.SetApplicationAsWorking() self.WriteInputName() filterConnected = itkConfidenceConnectedImageFilter.New( self.Input, self.Output ) self.AddEventHandlersToProcessObject( filterConnected ) filterConnected.SetInput( self.Input ) for seed in self.Seeds: self.WriteLineToConsole( "Seed=" + seed.ToString() ) filterConnected.AddSeed( seed ) filterConnected.Multiplier = self.Multiplier filterConnected.ReplaceValue = itkPixel( self.Input.PixelType, self.ReplaceValue ) filterConnected.InitialNeighborhoodRadius = self.NeighborhoodRadius filterConnected.NumberOfIterations = self.NumberOfIterations self.WriteLineToConsole( "Multiplier=" + filterConnected.Multiplier.ToString() ) self.WriteLineToConsole( "ReplaceValue=" + filterConnected.ReplaceValue.ToString() ) self.WriteLineToConsole( "NeighborhoodRadius=" + filterConnected.InitialNeighborhoodRadius.ToString() ) self.WriteLineToConsole( "NumberOfIterations=" + filterConnected.NumberOfIterations.ToString() ) filterConnected.UpdateLargestPossibleRegion() filterConnected.GetOutput( self.Output ) self.DisconnectInputAndOutput() self.DisposeOfObject( filterConnected ) self.WriteOutputName() self.FinishedWork( True ) except Exception, ex: self.HandleException( ex ) self.FinishedWork( False ) self.Finalise( ) def UnregisterEventHandlers(self): """ Unregister all the event handlers. """ if (self.Form != None): self.Form.Continue -= self.Continue self.Form.Cancel -= self.Cancel self.Form = None