#============================================================================== # # Project: SharpImage # Module: BinaryPixelMath.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 PixelMath library clr.AddReference("ManagedITK.Image.PixelMath") from itk import * class BinaryPixelMathScript(ImageToImageScriptObject): # ------------------------------------------------------------------------- Name = "BinaryPixelMath" Help = """Performs pixel-wise operations on two input images.""" Parameters = """(string) Input1 = the name search pattern for the first input (\"Input1\") (string) Input2 = the name search pattern for the second input (\"Input2\") (string) Operation = \"Add\", \"Sub\", \"Mult\", \"Div\", \"And\", \"Or\", \"Xor\", \"Max\", \"Min\", \"Mask\", or \"SquaredDiff\". (pixel) OutsideValue = the outside value for the \"Mask\" operation (0)""" Input1 = String( "Input1" ) Input2 = String( "Input2" ) Operation = None OutsideValue = None # ------------------------------------------------------------------------- def Initialise(self): """ Initialise the environment for running this script. """ """ NOTE: This function is invoked on the main UI thread. """ try: # Initialise the base class ThreadedScriptObject.Initialise( self ) # Get Renderer self.Renderer = self.ParentApplication.CurrentRenderer # Search for the input images self.Input1 = self.GetOpenImageMatchingPattern( self.Input1 ) self.Input2 = self.GetOpenImageMatchingPattern( self.Input2 ) if (self.Input1 == None): raise FileNotFoundException( "Could not find Input1." ) if (self.Input2 == None): raise FileNotFoundException( "Could not find Input2." ) # Create the output image self.Output = itkImage.New( self.Input1 ) except Exception, ex: self.HandleException( ex ) self.FinishedWork( False ) self.Finalise( ) def ThreadedDoWork(self): """ Perform the main functions of the script on a background thread. """ try: # Setup self.StartedWork() self.WriteImageName( "Input1=", self.Input1 ) self.WriteImageName( "Input2=", self.Input2 ) # 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 = itkAddImageFilter.New( self.Input1, self.Input2, self.Output ) elif (String.Compare( self.Operation, "Sub", ignoreCase) == 0): filterPixelMath = itkSubtractImageFilter.New( self.Input1, self.Input2, self.Output ) elif (String.Compare( self.Operation, "Mult", ignoreCase) == 0): filterPixelMath = itkMultiplyImageFilter.New( self.Input1, self.Input2, self.Output ) elif (String.Compare( self.Operation, "Div", ignoreCase) == 0): filterPixelMath = itkDivideImageFilter.New( self.Input1, self.Input2, self.Output ) elif (String.Compare( self.Operation, "And", ignoreCase) == 0): filterPixelMath = itkAndImageFilter.New( self.Input1, self.Input2, self.Output ) elif (String.Compare( self.Operation, "Or", ignoreCase) == 0): filterPixelMath = itkOrImageFilter.New( self.Input1, self.Input2, self.Output ) elif (String.Compare( self.Operation, "Xor", ignoreCase) == 0): filterPixelMath = itkXorImageFilter.New( self.Input1, self.Input2, self.Output ) elif (String.Compare( self.Operation, "Max", ignoreCase) == 0): filterPixelMath = itkMaximumImageFilter.New( self.Input1, self.Input2, self.Output ) elif (String.Compare( self.Operation, "Min", ignoreCase) == 0): filterPixelMath = itkMinimumImageFilter.New( self.Input1, self.Input2, self.Output ) elif (String.Compare( self.Operation, "Mask", ignoreCase) == 0): filterPixelMath = itkMaskImageFilter.New( self.Input1, self.Input2, self.Output ) if (self.OutsideValue != None): filterPixelMath.OutsideValue = itkPixel( self.Output.PixelType, self.OutsideValue ) elif (String.Compare( self.Operation, "SquaredDiff", ignoreCase) == 0): filterPixelMath = itkSquaredDifferenceImageFilter.New( self.Input1, self.Input2, self.Output ) else: self.WriteLineToConsole( "WARNING: Defaulting to Add due to unknown operation: '" + self.Operation +"'" ) filterPixelMath = itkAddImageFilter.New( self.Input1, self.Input2, self.Output ) # Peform the work self.AddEventHandlersToProcessObject( filterPixelMath ) filterPixelMath.SetInput1( self.Input1 ) filterPixelMath.SetInput2( self.Input2 ) filterPixelMath.UpdateLargestPossibleRegion() filterPixelMath.GetOutput( self.Output ) self.Input1.DisconnectPipeline() self.Input2.DisconnectPipeline() self.Output.DisconnectPipeline() self.DisposeOfObject( filterPixelMath ) pathInput1 = Path.GetDirectoryName( self.Input1.Name ) nameInput1 = Path.GetFileNameWithoutExtension( self.Input1.Name ) nameInput2 = Path.GetFileNameWithoutExtension( self.Input2.Name ) extInput1 = Path.GetExtension( self.Input1.Name ) fileOutput = Path.Combine( pathInput1, nameInput1 + "_" + self.Operation + "_" + nameInput2 + extInput1 ) self.WriteGivenOutputName( fileOutput ) self.FinishedWork( True ) except Exception, ex: self.HandleException( ex ) self.FinishedWork( False ) self.Finalise( )