#============================================================================== # # Project: SharpImage # Module: NaryPixelMath.py # Language: IronPython # Author: Dan Mueller # Date: $Date$ # Revision: $Revision$ # # 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 NaryPixelMathScript(ImageToImageScriptObject): # ------------------------------------------------------------------------- Name = "NaryPixelMath" Help = """Performs pixel-wise operations on all open images.""" Parameters = """(string) Operation = "Add" or "Max".""" Operation = None # ------------------------------------------------------------------------- def ThreadedDoWork(self): """ Perform the main functions of the script on a background thread. """ try: # Setup self.StartedWork() # Check Operation is not None if (self.Operation == None): self.WriteLineToConsole( "WARNING: Defaulting to Add due to invalid operation: None." ) self.Operation = "Add" # Create correct filter instance ignoreCase = True filterPixelMath = None if (String.Compare( self.Operation, "Add", ignoreCase) == 0): filterPixelMath = itkNaryAddImageFilter.New( self.Input, self.Output ) elif (String.Compare( self.Operation, "Max", ignoreCase) == 0): filterPixelMath = itkNaryMaximumImageFilter.New( self.Input, self.Output ) else: self.WriteLineToConsole( "WARNING: Defaulting to Add due to unknown operation: '" + self.Operation + "'" ) filterPixelMath = itkNaryAddImageFilter.New( self.Input, self.Output ) # Add all open images as input i = 0 for renderer in self.ParentApplication.Renderers: if (renderer != None and renderer.Inputs.Count > 0): self.WriteImageName( "Input=", renderer.Inputs[0] ) filterPixelMath.SetInput( i, renderer.Inputs[0] ) i += 1 # Peform the work self.AddEventHandlersToProcessObject( filterPixelMath ) filterPixelMath.UpdateLargestPossibleRegion() filterPixelMath.GetOutput( self.Output ) self.Output.DisconnectPipeline() self.DisposeOfObject( filterPixelMath ) self.WriteOutputName( ) self.FinishedWork( True ) except Exception, ex: self.HandleException( ex ) self.FinishedWork( False ) self.Finalise( )