#============================================================================== # # Project: SharpImage # Module: UnaryPixelMath.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 PixelMath library clr.AddReference("ManagedITK.PixelMathFilters") from itk import * class UnaryPixelMathScript(ImageToImageScriptObject): # ------------------------------------------------------------------------- Name = "UnaryPixelMath" Help = """Performs pixel-wise operations on a single input image.""" Parameters = """(string) Operation = "Abs", "Exp", "Log", "Not", "Negate", "Sqrt", "Square" or "Power". (double) Exponent = the exponent used by "Power".""" Operation = None Exponent = 1.0 # ------------------------------------------------------------------------- def ThreadedDoWork(self): """ Perform the main functions of the script on a background thread. """ try: # Setup self.StartedWork() self.WriteInputName() # Check Operation is not None if (self.Operation == None): self.WriteLineToConsole( "WARNING: Defaulting to Abs due to invalid operation: None." ) self.Operation = "Abs" # Create correct filter instance ignoreCase = True filterPixelMath = None if (String.Compare( self.Operation, "Abs", ignoreCase) == 0): filterPixelMath = itkAbsImageFilter.New( self.Input, self.Output ) elif (String.Compare( self.Operation, "Exp", ignoreCase) == 0): filterPixelMath = itkExpImageFilter.New( self.Input, self.Output ) elif (String.Compare( self.Operation, "Log", ignoreCase) == 0): filterPixelMath = itkLogImageFilter.New( self.Input, self.Output ) elif (String.Compare( self.Operation, "Not", ignoreCase) == 0): filterPixelMath = itkNotImageFilter.New( self.Input, self.Output ) elif (String.Compare( self.Operation, "Negate", ignoreCase) == 0): filterPixelMath = itkNegateImageFilter.New( self.Input, self.Output ) elif (String.Compare( self.Operation, "Sqrt", ignoreCase) == 0): filterPixelMath = itkSqrtImageFilter.New( self.Input, self.Output ) elif (String.Compare( self.Operation, "Square", ignoreCase) == 0): filterPixelMath = itkSquareImageFilter.New( self.Input, self.Output ) elif (String.Compare( self.Operation, "Power", ignoreCase) == 0): filterPixelMath = itkPowerImageFilter.New( self.Input, self.Output ) filterPixelMath.Exponent = self.Exponent else: self.WriteLineToConsole( "WARNING: Defaulting to Abs due to invalid operation: " + self.Operation ) filterPixelMath = itkAbsImageFilter.New( self.Input, self.Output ) # Peform the work self.AddEventHandlersToProcessObject( filterPixelMath ) filterPixelMath.SetInput( self.Input ) filterPixelMath.UpdateLargestPossibleRegion() filterPixelMath.GetOutput( self.Output ) self.DisconnectInputAndOutput() self.DisposeOfObject( filterPixelMath ) self.WriteOutputName() self.FinishedWork( True ) except Exception, ex: self.HandleException( ex ) self.FinishedWork( False ) self.Finalise( )