#============================================================================== # # Project: SharpImage # Module: Threshold.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 ThresholdScript(ImageToImageScriptObject): # ------------------------------------------------------------------------- Name = "Threshold" Help = """Sets image values to OutsideValue if they are outside specified values. This filter sets image values to OutsideValue (eg. black) if the image values are outside of the range [Lower..Upper].""" Parameters = """(pixel) Lower = all pixels with a lower value are set OutsideValue. (0) (pixel) Upper = all pixels with a higher value are set OutsideValue. (200) (pixel) OutsideValue = the value for pixels 'outside' the range. (0)""" Lower = 0 Upper = 200 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.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 = itkThresholdImageFilter.New( self.Input ) self.AddEventHandlersToProcessObject( filterThreshold ) filterThreshold.SetInput( self.Input ) filterThreshold.OutsideValue = itkPixel( self.Input.PixelType, self.OutsideValue ) pixelLower = itkPixel( self.Output.PixelType, self.Lower ) pixelUpper = itkPixel( self.Output.PixelType, self.Upper ) self.WriteLineToConsole( "Lower=" + pixelLower.ToString() ) self.WriteLineToConsole( "Upper=" + pixelUpper.ToString() ) self.WriteLineToConsole( "OutsideValue=" + filterThreshold.OutsideValue.ToString() ) filterThreshold.ThresholdOutside( pixelLower, pixelUpper ) 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( )