#============================================================================== # # Project: SharpImage # Module: Extract.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.Resize") from itk import * class ExtractScript(ImageToImageScriptObject): # ------------------------------------------------------------------------- Name = "Extract" Help = """Extract a given region from the input image.""" Parameters = """(itkSize) ExtractSize = the size of the region to extract. (itkSize(256,256,.)) (itkIndex) ExtractIndex = the index of the region to extract. (itkIndex(0,0,.))""" ExtractSize = None ExtractIndex = None # ------------------------------------------------------------------------- def Initialise(self): """ Initialise the environment for running this script. """ """ NOTE: This function is invoked on the main UI thread. """ try: ThreadedScriptObject.Initialise( self ) self.Renderer = self.ParentApplication.CurrentRenderer self.Input = self.Renderer.Inputs[0] except Exception, ex: self.HandleException( ex ) self.FinishedWork( False ) ScriptObject.Finalise( self ) def ThreadedDoWork(self): """ Perform the main functions of the script. """ """ NOTE: This function is invoked on a background thread. """ try: self.StartedWork() # Create default ExtractSize if (self.ExtractSize == None): self.ExtractSize = itkSize( self.Input.Dimension ) self.ExtractSize.Fill( 256 ) # Create default ExtractIndex if (self.ExtractIndex == None): self.ExtractIndex = itkIndex( self.Input.Dimension ) self.ExtractIndex.Fill( 0 ) # Compute output dimensions outputDimension = 0 for i in range(self.Input.Dimension): if (self.ExtractSize[i] > 0): outputDimension += 1 # Create output self.Output = itkImage.New( self.Input.PixelType, outputDimension ) # Write parameters self.WriteInputName() self.WriteLineToConsole( "ExtractSize=" + self.ExtractSize.ToString() ) self.WriteLineToConsole( "ExtractIndex=" + self.ExtractIndex.ToString() ) # Do actual work filterExtract = itkExtractImageFilter.New( self.Input, self.Output ) self.AddEventHandlersToProcessObject( filterExtract ) filterExtract.SetInput( self.Input ) filterExtract.ExtractionRegion = itkImageRegion( self.ExtractSize, self.ExtractIndex ) filterExtract.GetOutput( self.Output ) filterExtract.UpdateLargestPossibleRegion() self.DisconnectInputAndOutput() region = itkImageRegion( self.Output.Size, itkIndex(self.Output.Dimension) ) self.Output.SetRegions( region ) self.DisposeOfObject( filterExtract ) self.WriteOutputName() self.FinishedWork( True ) except Exception, ex: self.HandleException( ex ) self.FinishedWork( False ) self.Finalise( )