#============================================================================== # # Project: SharpImage # Module: ProjectionScript.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 required libraries clr.AddReference("ManagedITK.Image.Projection") clr.AddReference("ManagedITK.Image.Resize") from itk import * class ProjectionScriptObject(ImageToImageScriptObject): # ------------------------------------------------------------------------- Name = " ProjectionScript" Parameters = """(int) ProjectionDimension = the dimension to accumulate. (Dimension-1) (int) StartIndex = the index of the starting slice. (0) (int) EndIndex = the index of the ending slice. (Size)""" ProjectionDimension = None StartIndex = None EndIndex = None # ------------------------------------------------------------------------- def Initialise(self): """ Initialise the environment for running this script. """ """ NOTE: This function is invoked on the main UI thread. """ ImageToImageScriptObject.Initialise( self ) self.Output = itkImage.New( self.Input.PixelType, self.Input.Dimension - 1 ) def ThreadedDoWork(self): """ Perform the main functions of the script on a background thread. """ try: self.StartedWork() self.WriteInputName() if (self.ProjectionDimension == None): self.ProjectionDimension = self.Input.Dimension - 1 # Extract slices for projection filterExtract = None if ( self.StartIndex != None and self.EndIndex != None ): # Check that StartIndex and EndIndex are valid if ( self.StartIndex < 0 ): self.StartIndex = 0 if ( self.EndIndex >= self.Input.Size[self.ProjectionDimension] ): self.EndIndex = self.Input.Size[self.ProjectionDimension] # Compute extraction region index = itkIndex( self.Input.Dimension ) size = itkSize( self.Input.Size.Data ) index[self.ProjectionDimension] = self.StartIndex size[self.ProjectionDimension] = self.EndIndex - self.StartIndex # Extract region filterExtract = itkExtractImageFilter.New( self.Input, self.Input ) self.AddEventHandlersToProcessObject( filterExtract ) filterExtract.SetInput( self.Input ) filterExtract.ExtractionRegion = itkImageRegion( size, index ) self.WriteLineToConsole( "ExtractionRegion=" + filterExtract.ExtractionRegion.ToString() ) # Setup projection filter filterProjection = self.GetProjectionFilter() self.AddEventHandlersToProcessObject( filterProjection ) if ( filterExtract == None ): filterProjection.SetInput( self.Input ) else: filterProjection.SetInput( filterExtract.GetOutput() ) filterProjection.ProjectionDimension = self.ProjectionDimension self.WriteLineToConsole( "ProjectionDimension=" + filterProjection.ProjectionDimension.ToString() ) # Setup final extraction filter to collapse dimensions filterCollapse = itkExtractImageFilter.New( self.Input, self.Output ) self.AddEventHandlersToProcessObject( filterCollapse ) filterCollapse.SetInput( filterProjection.GetOutput() ) indexCollapse = itkIndex( self.Input.Dimension ) sizeCollapse = itkSize( self.Input.Size.Data ) sizeCollapse[self.ProjectionDimension] = 0 filterCollapse.ExtractionRegion = itkImageRegion( sizeCollapse, indexCollapse ) filterCollapse.UpdateLargestPossibleRegion() filterCollapse.GetOutput( self.Output ) self.DisconnectInputAndOutput() # Clean up self.DisposeOfObject( filterExtract ) self.DisposeOfObject( filterProjection ) self.DisposeOfObject( filterCollapse ) self.WriteOutputName() self.FinishedWork( True ) except Exception, ex: self.HandleException( ex ) self.FinishedWork( False ) self.Finalise( )