#============================================================================== # # Project: SharpImage # Module: CropSeries.py # Language: IronPython # Author: Dan Mueller # Date: $Date: 2007-07-13 06:30:23 +1000 (Fri, 13 Jul 2007) $ # Revision: $Revision: 8 $ # # 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 Script from Script import * # Add reference and import Resize library clr.AddReference("ManagedITK.ResizeFilters") from itk import * class CropSeriesScript(ThreadedScriptObject): # ------------------------------------------------------------------------- Name = "CropSeries" Help = """Crops a given series of images so that a subsequent image series read will work.""" Parameters = """(string) FilePath = the path of the image series. (\"C:/\") (string) InputFilePattern = the pattern of the series to crop. ("Input_*.png") (string) OutputFilePattern = the pattern of the cropped output file names. ("Output_*.png") (itkIndex) CropIndex = the index of the cropping region. (itkIndex(0,0,.)) (itkSize) CropSize = the size of the cropping region. (itkSize(128,128,.)""" FilePath = "C:/" InputFilePattern = "Input_*.png" OutputFilePattern = "Output_*.png" CropIndex = itkIndex(0, 0) CropSize = itkSize(128, 128) # ------------------------------------------------------------------------- def ThreadedDoWork(self): """ Perform the main functions of the script. """ """ NOTE: This function is invoked on a background thread. """ try: self.StartedWork() # Write script parameters self.WriteLineToConsole( "FilePath=" + self.FilePath ) self.WriteLineToConsole( "InputFilePattern=" + self.InputFilePattern ) self.WriteLineToConsole( "OutputFilePattern=" + self.OutputFilePattern ) self.WriteLineToConsole( "CropIndex=" + self.CropIndex.ToString() ) self.WriteLineToConsole( "CropSize=" + self.CropSize.ToString() ) # Construct crop region cropRegion = itkImageRegion( self.CropSize, self.CropIndex ) index = 0 for inputPath in Directory.GetFiles( self.FilePath, self.InputFilePattern ): # Read image slice self.WriteLineToConsole("Processing slice " + index.ToString("000") + "...") imageIn = itkImage.New( itkPixelType.SS, 2 ) imageIn.Read( inputPath ) imageOut = itkImage.New( imageIn ) # Crop filterCrop = itkExtractImageFilter.New( imageIn, imageOut ) filterCrop.SetInput( imageIn ) filterCrop.ExtractionRegion = cropRegion filterCrop.UpdateLargestPossibleRegion() filterCrop.GetOutput( imageOut ) self.DisconnectInputAndOutput() self.DisposeOfObject( filterCrop ) # Write outputPath = Path.Combine( self.FilePath, self.OutputFilePattern.Replace("*", index.ToString("000")) ) imageOut.Write(outputPath) # Increment index index += 1 # Indicate we are done self.FinishedWork( True ) except Exception, ex: self.HandleException( ex ) self.FinishedWork( False ) self.Finalise( ) def Finalise(self): """ Finalise the environment after running this script. """ """ NOTE: This function is invoked on the main UI thread. """ ScriptObject.Finalise(self)