#============================================================================== # # Project: SharpImage # Module: BinaryThreshold.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 BinaryThresholdScript(ImageToImageScriptObject): # ------------------------------------------------------------------------- Name = "BinaryThreshold" Help = """This filter produces an output image whose pixels are either one of two values (OutsideValue or InsideValue), depending on whether or not the corresponding input image pixel lies between the two thresholds (Lower and Upper). Values equal to either threshold is considered to be between the thresholds.""" Parameters = """(pixel) Lower = the lower threshold. (100) (pixel) Upper = the upper threshold. (255) (pixel) InsideValue = the value for pixels inside the range. (255) (pixel) OutsideValue = the value for pixels outside the range. (0)""" Lower = 100 Upper = 255 InsideValue = None OutsideValue = None # ------------------------------------------------------------------------- def Initialise(self): """ Initialise the environment for running this script. """ """ NOTE: This function is invoked on the main UI thread. """ ImageToImageScriptObject.Initialise( self ) if (self.InsideValue == None): self.InsideValue = 255 if (self.OutsideValue == None): self.OutsideValue = 0 def ThreadedDoWork(self): """ Perform the main functions of the script. """ """ NOTE: This function is invoked on a background thread. """ try: self.StartedWork() self.WriteInputName() filterThreshold = itkBinaryThresholdImageFilter.New( self.Input, self.Output ) self.AddEventHandlersToProcessObject( filterThreshold ) filterThreshold.SetInput( self.Input ) filterThreshold.LowerThreshold = itkPixel( self.Input.PixelType, self.Lower ) filterThreshold.UpperThreshold = itkPixel( self.Input.PixelType, self.Upper ) filterThreshold.InsideValue = itkPixel( self.Input.PixelType, self.InsideValue ) filterThreshold.OutsideValue = itkPixel( self.Input.PixelType, self.OutsideValue ) self.WriteLineToConsole( "Lower=" + filterThreshold.LowerThreshold.ToString() ) self.WriteLineToConsole( "Upper=" + filterThreshold.UpperThreshold.ToString() ) self.WriteLineToConsole( "InsideValue=" + filterThreshold.InsideValue.ToString() ) self.WriteLineToConsole( "OutsideValue=" + filterThreshold.OutsideValue.ToString() ) filterThreshold.UpdateLargestPossibleRegion() filterThreshold.GetOutput( self.Output ) self.DisconnectInputAndOutput() self.DisposeOfObject( filterThreshold ) self.WriteOutputName() self.FinishedWork( True ) except Exception, ex: self.HandleException( ex ) self.FinishedWork( False ) self.Finalise( )