#============================================================================== # # Project: SharpImage # Module: ConnectedComponent.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 Threshold library clr.AddReference("ManagedITK.ThresholdFilters") from itk import * class ConnectedComponentScript(ImageToImageScriptObject): # ------------------------------------------------------------------------- Name = "ConnectedComponent" Help = """This filter labels the objects in a binary image. Each distinct object is assigned a unique label in consecutive order (with no gaps between the numbers used). The labels are sorted based on the size of the object: the largest object will have label #1, the second largest will have label #2, etc. The number of found labels is output to the script console. This script assumes the input image is binary in the range [0..1].""" Parameters = """None""" # ------------------------------------------------------------------------- def ThreadedDoWork(self): """ Perform the main functions of the script. """ """ NOTE: This function is invoked on a background thread. """ try: self.StartedWork() self.WriteInputName() # Perform connected component analysis # NOTE: The itkConnectedComponentImageFilter expects a binary image # in the range [0,1]. filterConnected = itkConnectedComponentImageFilter.New( self.Input, self.Output ) self.AddEventHandlersToProcessObject( filterConnected ) filterConnected.SetInput( self.Input ) filterConnected.FullyConnected = True # Relabel filterRelabel = itkRelabelComponentImageFilter.New( self.Input, self.Output ) self.AddEventHandlersToProcessObject( filterRelabel ) filterRelabel.SetInput( filterConnected.GetOutput() ) filterRelabel.UpdateLargestPossibleRegion() filterRelabel.GetOutput( self.Output ) self.DisconnectInputAndOutput() # Clean up self.DisposeOfObject( filterConnected ) self.DisposeOfObject( filterRelabel ) self.WriteOutputName() self.FinishedWork( True ) except Exception, ex: self.HandleException( ex ) self.FinishedWork( False ) self.Finalise( )