#============================================================================== # # Project: SharpImage # Module: RasterisePath.py # Language: IronPython # Author: Dan Mueller # Date: $Date$ # Revision: $Revision$ # # 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.Paths") clr.AddReference("ManagedITK.PixelMathFilters") from itk import * class RasterisePathScript(ImageToImageScriptObject): # ------------------------------------------------------------------------- Name = "RasterisePath" Help = """Rasterises the paths in the current input to a discrete unsigned char image.""" Parameters = """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( itkPixelType.UC, self.Input.Dimension ) def ThreadedDoWork(self): """ Perform the main functions of the script. """ """ NOTE: This function is invoked on a background thread. """ try: # Start the work self.StartedWork() self.WriteInputName() # Get the paths to rasterise pathsToRasterise = List[itkDataObject]() for dataobj in self.Renderer.Inputs: if ( Type.GetType("itk.itkParametricPath, ManagedITK.Common").IsInstanceOfType(dataobj) ): pathsToRasterise.Add( dataobj ) # Rasterise the paths if ( pathsToRasterise.Count == 0 ): raise ArgumentException( "No valid paths found in input." ) if ( pathsToRasterise.Count == 1 ): self.WriteTask( "Rasterising path" ) pathType = itkPolyLineParametricPath.New( self.Input.Dimension ) filterPath = itkPathToImageFilter.New( pathType, self.Output ) filterPath.Size = self.Input.Size filterPath.Spacing = self.Input.Spacing filterPath.Origin = self.Input.Origin filterPath.BackgroundValue = itkPixel_UC.NewMin( ) filterPath.PathValue = itkPixel_UC.NewMax( ) filterPath.SetInput( pathsToRasterise[0] ) filterPath.Update( ) filterPath.GetOutput( self.Output ) elif ( pathsToRasterise.Count > 1 ): i = 0; filterMax = itkNaryMaximumImageFilter.New( self.Output, self.Output ) temp = itkImage.New( itkPixelType.UC, self.Input.Dimension ) for dataobj in pathsToRasterise: self.WriteTask( "Rasterising path " + i.ToString() ) pathType = itkPolyLineParametricPath.New( self.Input.Dimension ) filterPath = itkPathToImageFilter.New( pathType, self.Output ) filterPath.Size = self.Input.Size filterPath.Spacing = self.Input.Spacing filterPath.Origin = self.Input.Origin filterPath.BackgroundValue = itkPixel_UC.NewMin( ) filterPath.PathValue = itkPixel_UC.NewMax( ) filterPath.SetInput( dataobj ) filterPath.Update() filterMax.SetInput( i, filterPath.GetOutput() ) i += 1 filterMax.Update() filterMax.GetOutput( self.Output ) # Clean up and finish self.DisconnectInputAndOutput() self.DisposeOfObject( filterPath ) self.WriteOutputName() self.FinishedWork( True ) except Exception, ex: self.HandleException( ex ) self.FinishedWork( False ) self.Finalise( )