#============================================================================== # # Project: SharpImage # Module: GenerateMask.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.IntensityFilters") from itk import * class GenerateMaskScript(ImageToImageScriptObject): # ------------------------------------------------------------------------- Name = "GenerateMask" Help = """Generates a mask image using a number of objects specified using widgets.""" Form = None Renderers = None Selections = None # ------------------------------------------------------------------------- def Run(self): """ The entry-point for this script. """ self.Initialise() # NOTE: DoWork() will be invoked when Continue is clicked. # NOTE: Finalise() will be invoked when FinishedWork() is called. 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 ) # Search for inputs self.Renderers = List[siRenderer]() pattern = Path.GetFileNameWithoutExtension( self.Input.Name ) self.Renderers = self.GetAllRenderersMatchingPattern( pattern ) # Write the inputs used for renderer in self.Renderers: name = Path.GetFileName( renderer.Inputs[0].Name ) self.WriteLineToConsole( "Using Input: " + name ) # Show the form self.Form = siFormSelection( self.Renderers ) self.Form.Continue += self.Continue self.Form.Cancel += self.Cancel self.ParentApplication.AddTool( self.Form ) self.ParentApplication.SetApplicationAsReady( 0 ) 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() # Generate the mask self.GenerateMask( ) # Finish work self.WriteOutputName() self.UnregisterEventHandlers() self.FinishedWork( True ) except Exception, ex: self.HandleException( ex ) self.FinishedWork( False ) self.Finalise( ) def GenerateMask(self): """ Generate the mask using the GenerateMask filter. """ filterMask = itkGenerateMaskImageFilter.New( self.Output ) self.AddEventHandlersToProcessObject( filterMask ) for selection in self.Selections: if (String.Compare( selection.GenerateMaskImageFilterType, "Ellipse", True ) == 0): self.WriteLineToConsole( selection.ToString() ) filterMask.AddEllipse( selection.Center, selection.Radius ) if (String.Compare( selection.GenerateMaskImageFilterType, "Point", True ) == 0): for point in selection.Points: self.WriteLineToConsole( "Point: " + point.ToString() ) filterMask.AddPoint( point ) if (String.Compare( selection.GenerateMaskImageFilterType, "Region", True ) == 0): region = selection.ToImageRegion() self.WriteLineToConsole( "Region: " + region.ToString() ) filterMask.AddRegion( region ) if (String.Compare( selection.GenerateMaskImageFilterType, "Image", True ) == 0): if (hasattr(selection, "ToImage")): filterMask.AddImage( selection.ToImage() ) filterMask.Size = self.Input.Size filterMask.Spacing = self.Input.Spacing filterMask.Origin = self.Input.Origin filterMask.UpdateLargestPossibleRegion() filterMask.GetOutput( self.Output ) self.DisconnectInputAndOutput(); self.DisposeOfObject( filterMask ) def Continue(self, sender, args): """ Allow processing to continue. """ self.Selections = self.Form.Selections self.DoWork() def Cancel(self, sender, args): """ Cancel the script. """ self.UnregisterEventHandlers() ScriptObject.Finalise( self ) def UnregisterEventHandlers(self): """ Unregister all the event handlers. """ if (self.Form != None): self.Form.Continue -= self.Continue self.Form.Cancel -= self.Cancel self.Form = None