#============================================================================== # # Project: SharpImage # Module: Resize.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 Resize library clr.AddReference("ManagedITK.Image.Transform") clr.AddReference("ManagedITK.Image.Interpolators") from itk import * class ResizeScript(ImageToImageScriptObject): # ------------------------------------------------------------------------- Name = "Resize" Help = """Resizes the input image to the given size. The spacing is automatically computed. If Interpolation="WindowedSinc" then the WindowFunction property is used.""" Parameters = """(itkSize) OutputSize = the output size of the image. (string) Interpolator = \"Linear\", \"Nearest\", \"BSpline\", or \"WindowedSinc\" (\"Linear\") (string) WindowFunction = \"Cosine\", \"Hamming\", \"Lanczos\", or \"Welch\" (\"Cosine\")""" OutputSize = itkSize(128, 128) OutputSpacing = None Interpolator = "Linear" WindowFunction = "Cosine" # ------------------------------------------------------------------------- def ThreadedDoWork(self): """ Perform the main functions of the script. """ """ NOTE: This function is invoked on a background thread. """ try: self.StartedWork() # Create transform transform = itkIdentityTransform.New( self.Input.Dimension ) # Create interpolator CoordTypeDouble = itkPixelType.D; interpolator = None if String.Compare(self.Interpolator, "Linear", False) == 0: interpolator = itkLinearInterpolateImageFunction.New( self.Input, CoordTypeDouble ) elif String.Compare(self.Interpolator, "Nearest", False) == 0: interpolator = itkNearestNeighborInterpolateImageFunction.New( self.Input, CoordTypeDouble ) elif String.Compare(self.Interpolator, "BSpline", False) == 0: interpolator = itkBSplineInterpolateImageFunction.New( self.Input, CoordTypeDouble ) elif String.Compare(self.Interpolator, "WindowedSinc", False) == 0: radius = itkRadius( 4 ) if String.Compare(self.WindowFunction, "Cosine", False) == 0: interpolator = itkCosineWindowedSincInterpolateImageFunction.New( self.Input, radius ) elif String.Compare(self.WindowFunction, "Hamming", False) == 0: interpolator = itkHammingWindowedSincInterpolateImageFunction.New( self.Input, radius ) elif String.Compare(self.WindowFunction, "Lanczos", False) == 0: interpolator = itkLanczosWindowedSincInterpolateImageFunction.New( self.Input, radius ) elif String.Compare(self.WindowFunction, "Welch", False) == 0: interpolator = itkWelchWindowedSincInterpolateImageFunction.New( self.Input, radius ) # Compute new spacing self.OutputSpacing = itkSpacing( self.OutputSize.Dimension ) for i in range( self.OutputSize.Dimension ): inputSize = System.Double( self.Input.Size[i] ) outputSize = System.Double( self.OutputSize[i] ) self.OutputSpacing[i] = self.Input.Spacing[i] * (inputSize / outputSize) # Write parameters self.WriteInputName() self.WriteLineToConsole( "InputSize=" + self.Input.Size.ToString() ) self.WriteLineToConsole( "InputSpacing=" + self.Input.Spacing.ToString() ) self.WriteLineToConsole( "OutputSize=" + self.OutputSize.ToString() ) self.WriteLineToConsole( "OutputSpacing=" + self.OutputSpacing.ToString() ) self.WriteLineToConsole( "Interpolator=" + interpolator.ToString() ) # Do actual work filterResample = itkResampleImageFilter.New( self.Input, self.Output ) self.AddEventHandlersToProcessObject( filterResample ) filterResample.SetInput( self.Input ) filterResample.OutputSize = self.OutputSize filterResample.OutputSpacing = self.OutputSpacing filterResample.SetTransform( transform ) filterResample.SetInterpolator( interpolator ) filterResample.UpdateLargestPossibleRegion() filterResample.GetOutput( self.Output ) self.DisconnectInputAndOutput() self.DisposeOfObject( filterResample ) self.WriteOutputName() self.FinishedWork( True ) except Exception, ex: self.HandleException( ex ) self.FinishedWork( False ) self.Finalise( )