#============================================================================== # # Project: SharpImage # Module: ConnectedThreshold.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 ConnectedThresholdScript(ImageToImageScriptObject): # ------------------------------------------------------------------------- Name = "ConnectedThreshold" Help = """Label pixels that are connected to a seed and lie within a range of values.""" Parameters = """(pixel) Lower = The lower threshold for the range of values. (100) (pixel) Upper = The upper threshold for the range of values. (255) (pixel) ReplaceValue = The value to set pixels inside of the range. (255) (bool) OverlayResult = the resultant output is overlaid on the input. (False) (Color) LabelColor = The color of the label. (Color.FromArgb(128,255,0,0))""" Lower = 100 Upper = 255 ReplaceValue = 255 OverlayResult = False LabelColor = Color.FromArgb(128,255,0,0) 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( False ) ScriptObject.Finalise( self ) 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 = itkConnectedThresholdImageFilter.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.Lower = itkPixel( self.Input.PixelType, self.Lower ) filterConnected.Upper = itkPixel( self.Input.PixelType, self.Upper ) filterConnected.ReplaceValue = itkPixel( self.Input.PixelType, self.ReplaceValue ) self.WriteLineToConsole( "Lower=" + filterConnected.Lower.ToString() ) self.WriteLineToConsole( "Upper=" + filterConnected.Upper.ToString() ) self.WriteLineToConsole( "ReplaceValue=" + filterConnected.ReplaceValue.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 Finalise(self): """ Finalise the environment after running this script. """ """ NOTE: This function is invoked on the main UI thread. """ self.UnregisterEventHandlers() ThreadedScriptObject.Finalise(self) if (self.OverlayResult): # Set output LUT labelValue = itkPixel( self.Output.PixelType, self.ReplaceValue ) lut = siGdiLookupTable( ) lut.SetTableRange( self.Output, 256 ) lut.SetTableToSingleColor( labelValue.ValueAsD, self.LabelColor ) # Add to current Renderer self.Renderer.AddInputAsLabel( self.Output, lut ) self.Renderer.Repaint() self.Renderer.Focus() else: # Display in a new Renderer newRenderer = siGdiSliceRenderer( self.ParentApplication ) newRenderer.Inputs.Add( self.Output ) newRenderer.Initialise() self.ParentApplication.ShowRenderer( newRenderer ) 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