#============================================================================== # # Project: SharpImage # Module: Compose.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 relevent class import ImageToImageScript from ImageToImageScript import * # Add reference and import Compose library clr.AddReference("ManagedITK.Image.Compose") from itk import * class ComposeScript(ImageToImageScriptObject): # ------------------------------------------------------------------------- Name = "Compose" Help = """Compose a vector image from a number of single channels. The output composed image is always of unsigned char type.""" Parameters = """(string) Input1 = a single channel input. (None) (string) Input2 = a single channel input. (None) (string) Input3 = a single channel input. (None) (string) Input4 = a single channel input. (None)""" NumberOfInputs = 0 Input1 = None Input2 = None Input3 = None Input4 = None # ------------------------------------------------------------------------- def Initialise(self): """ Initialise the environment for running this script. """ """ NOTE: This function is invoked on the main UI thread. """ try: ImageToImageScriptObject.Initialise( self ) if (self.Input1 != None and self.Input1.Length > 0): self.NumberOfInputs += 1 self.Input1 = self.GetOpenImageMatchingPattern( self.Input1 ) if (self.Input2 != None and self.Input2.Length > 0): self.NumberOfInputs += 1 self.Input2 = self.GetOpenImageMatchingPattern( self.Input2 ) if (self.Input3 != None and self.Input3.Length > 0): self.NumberOfInputs += 1 self.Input3 = self.GetOpenImageMatchingPattern( self.Input3 ) if (self.Input4 != None and self.Input4.Length > 0): self.NumberOfInputs += 1 self.Input4 = self.GetOpenImageMatchingPattern( self.Input4 ) if (self.NumberOfInputs == 3 and self.Input1.Dimension == 3): self.Output = itkImage_RGBUC3.New( ) elif (self.NumberOfInputs == 4 and self.Input1.Dimension == 3): self.Output = itkImage_RGBAUC3.New( ) elif (self.NumberOfInputs == 3 and self.Input1.Dimension == 2): self.Output = itkImage_RGBUC2.New( ) elif (self.NumberOfInputs == 4 and self.Input1.Dimension == 2): self.Output = itkImage_RGBAUC2.New( ) except Exception, ex: self.HandleException( ex ) self.FinishedWork( False ) ImageToImageScriptObject.Finalise( self ) def ThreadedDoWork(self): """ Perform the main functions of the script on a background thread. """ try: self.StartedWork() # Create the compose filter filterCompose = None if (self.NumberOfInputs == 3): filterCompose = itkComposeRGBImageFilter.New( self.Input, self.Output ) elif(self.NumberOfInputs == 4): filterCompose = itkComposeRGBAImageFilter.New( self.Input, self.Output ) else: raise NotSupportedException( "The number of inputs is currently not supported." ) # Add the inputs if (self.Input1 != None): filterCompose.SetInput( 0, self.Input1 ) self.WriteLineToConsole( "Input1=" + self.Input1.Name ) if (self.Input2 != None): filterCompose.SetInput( 1, self.Input2 ) self.WriteLineToConsole( "Input2=" + self.Input2.Name ) if (self.Input3 != None): filterCompose.SetInput( 2, self.Input3 ) self.WriteLineToConsole( "Input3=" + self.Input3.Name ) if (self.Input4 != None): filterCompose.SetInput( 3, self.Input4 ) self.WriteLineToConsole( "Input4=" + self.Input4.Name ) # Setup and run the filter self.AddEventHandlersToProcessObject( filterCompose ) filterCompose.UpdateLargestPossibleRegion() filterCompose.GetOutput( self.Output ) # Clean up and return self.DisconnectInputAndOutput() self.DisposeOfObject( filterCompose ) self.WriteOutputName() self.FinishedWork( True ) except Exception, ex: self.HandleException( ex ) self.FinishedWork( False ) self.Finalise( )