#============================================================================== # # Project: SharpImage # Module: GenerateMaskFromFile.py # Language: IronPython # Author: Dan Mueller # Date: $Date: 2007-08-06 06:43:54 +1000 (Mon, 06 Aug 2007) $ # Revision: $Revision: 13 $ # # 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.IntensityFilters") clr.AddReference("ManagedITK.CastFilters") from itk import * class GenerateMaskFromFileScript(ImageToImageScriptObject): # ------------------------------------------------------------------------- Name = "GenerateMaskFromFile" Help = """Generates a mask image from a list of objects in a text file.""" Parameters = """(string) Default = the full path of the mask file.""" Default = None # ------------------------------------------------------------------------- def Initialise(self): """ Initialise the environment for running this script. """ """ NOTE: This function is invoked on the main UI thread. """ # Initialise ImageToImageScriptObject.Initialise( self ) # Make output unsigned char self.Output = itkImage.New( itkPixelType.UC, self.Input.Dimension ) def ThreadedDoWork(self): """ Perform the main functions of the script on a background thread. """ try: # Start the work self.StartedWork() self.WriteInputName() # Generate the mask self.GenerateMaskFromFile( ) # Finish work self.WriteOutputName() self.FinishedWork( True ) except Exception, ex: self.HandleException( ex ) self.FinishedWork( False ) self.Finalise( ) def GenerateMaskFromFile(self): """ Generate the mask from objects listed in self.File. """ # Check the file is valid if ( self.Default == None or self.Default.Length == 0 or not File.Exists(self.Default) ): raise ArgumentException( "The given file does not exist or is invalid" ) return # Setup the filter filterMask = itkGenerateMaskImageFilter.New( self.Output ) self.AddEventHandlersToProcessObject( filterMask ) filterMask.Size = self.Input.Size filterMask.Spacing = self.Input.Spacing filterMask.Origin = self.Input.Origin # Read the file and add objects lines = File.ReadAllLines( self.Default ) for line in lines: self.WriteLineToConsole( line ) if ( line.StartsWith("Sphere") ): # Sphere: Center=XX,XX,XX Radius=XX linesplit = line.Split( ' ' ) scenter = linesplit[1].Replace( "Center=", "" ) sradius = linesplit[2].Replace( "Radius=", "" ) scentersplit = scenter.Split( ',' ) center = itkPoint( Convert.ToUInt16(scentersplit.Length) ) for i in range( scentersplit.Length ): center[i] = Single.Parse( scentersplit[i] ) radius = Single.Parse( sradius ) filterMask.AddEllipse( center, radius ) elif ( line.StartsWith("Point") ): # Point: XX,XX,XX spointsplit = line.Replace("Point:","").Split( ',' ) point = itkPoint( Convert.ToUInt16(spointsplit.Length) ) for i in range( spointsplit.Length ): point[i] = Single.Parse( spointsplit[i] ) filterMask.AddPoint( point ) else: # The object is not supported / recognised self.WriteLineToConsole( "WARNING: The above object is not supported." ) # Update the filter and clean up filterMask.GetOutput( self.Output ) filterMask.UpdateLargestPossibleRegion() self.DisconnectInputAndOutput(); self.DisposeOfObject( filterMask )