#============================================================================== # # Project: SharpImage # Module: ConstantPad.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 Resize library clr.AddReference("ManagedITK.ResizeFilters") from itk import * class ConstantPadScript(ImageToImageScriptObject): # ------------------------------------------------------------------------- Name = "ConstantPad" Help = """Pad the image with a constant, typically to reverse a cropping. Either Lower/Upper must be supplied, or Size/Origin. If the Size/Origin method is used, the lower bounds are calculated from the Input.Origin and the upper bounds computed from the OutputSize. WARNING: The Size/Origin method assumes a zero-origin before crop. Example: ConstantPad Lower=[10,10] Upper=[10,10]""" Parameters = """(pixel) Constant = the value to pad the image with. (0) (itkSize) OutputSize = the desired size of the output image. (List) Lower = the padding for the lower bounds. ([0,0,.]) (List) Upper = the padding for the upper bounds. ([0,0,.])""" Constant = 0 Lower = None Upper = None OutputSize = None # ------------------------------------------------------------------------- def ThreadedDoWork(self): """ Perform the main functions of the script on a background thread. """ try: # Start self.StartedWork() # Convert the constant to a pixel self.Constant = itkPixel( self.Output.PixelType, self.Constant ) # Compute the lower and upper bounds to pad if (self.Lower == None and self.Upper == None): # Instantiate the lower and upper arrays if (self.Input.Dimension == 2): self.Lower = [0,0] self.Upper = [0,0] elif (self.Input.Dimension == 3): self.Lower = [0,0,0] self.Upper = [0,0,0] # Convert the origin to pixel space index = itkIndex( self.Input.Dimension ) for i in range( self.Input.Dimension ): index[i] = Math.Round(self.Input.Origin[i] / self.Input.Spacing[i]) # Populate the lower and upper bounds for i in range( self.Input.Dimension ): self.Lower[i] = index[i] self.Upper[i] = self.OutputSize[i] - index[i] - self.Input.Size[i] # Setup and run filter self.WriteInputName() filterPad = itkConstantPadImageFilter.New( self.Input, self.Output ) self.AddEventHandlersToProcessObject( filterPad ) filterPad.SetInput( self.Input ) filterPad.Constant = self.Constant filterPad.PadUpperBound = System.Array[UInt32]( self.Upper ) filterPad.PadLowerBound = System.Array[UInt32]( self.Lower ) self.WriteLineToConsole( "Constant=" + self.Constant.ToString() ) self.WriteLineToConsole( "PadLowerBound=" + self.ArrayToString(self.Lower) ) self.WriteLineToConsole( "PadUpperBound=" + self.ArrayToString(self.Upper) ) filterPad.GetOutput( self.Output ) filterPad.UpdateLargestPossibleRegion() self.DisconnectInputAndOutput() self.DisposeOfObject( filterPad ) region = itkImageRegion( self.Output.Size, itkIndex(self.Output.Dimension) ) self.Output.SetRegions( region ) self.Output.Origin = itkPoint( self.Output.Dimension ) # Finish off self.WriteOutputName() self.FinishedWork( True ) except Exception, ex: self.HandleException( ex ) self.FinishedWork( False ) self.Finalise( )