#============================================================================== # # Project: SharpImage # Module: JointHistogram.py # Language: IronPython # Author: Dan Mueller # Date: $Date: 2007-07-13 06:30:23 +1000 (Fri, 13 Jul 2007) $ # Revision: $Revision: 8 $ # # 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 Statistics library clr.AddReference("ManagedITK.Statistics") from itk import * class JointHistogramScript(ImageToImageScriptObject): # ------------------------------------------------------------------------- Name = "JointHistogram" Help = """Computes the joint histogram of two n-D images.""" Parameters = """(string) Input1 = the name search pattern for the first input. ("Input1") (string) Input2 = the name search pattern for the second input. ("Input2") (List) NumberOfBins = The number of bins for each input. ([256,256]) (double) MarginalScale = The marginal scale. (5.0) (HistogramToImageMappingEnum) Mapping = the histogram to image mapping type. (LogProbability)""" Input1 = String( "Input1" ) Input2 = String( "Input2" ) NumberOfBins = [256, 256] MarginalScale = 5.0 Mapping = HistogramToImageMappingEnum.LogProbability # ------------------------------------------------------------------------- def Initialise(self): """ Initialise the environment for running this script. """ """ NOTE: This function is invoked on the main UI thread. """ try: # Initialise the base class ThreadedScriptObject.Initialise( self ) # Get Renderer self.Renderer = self.ParentApplication.CurrentRenderer # Search for the input images self.Input1 = self.GetOpenImageMatchingPattern( self.Input1 ) self.Input2 = self.GetOpenImageMatchingPattern( self.Input2 ) if (self.Input1 == None): raise FileNotFoundException( "Could not find Input1." ) if (self.Input2 == None): raise FileNotFoundException( "Could not find Input2." ) # Ensure the input requested regions are largest possible self.Input1.RequestedRegion = self.Input1.LargestPossibleRegion self.Input2.RequestedRegion = self.Input2.LargestPossibleRegion # Create the output image self.Output = itkImage.New( self.Input1.PixelType, 2 ) except Exception, ex: self.HandleException( ex ) self.FinishedWork( False ) self.Finalise( ) def ThreadedDoWork(self): """ Perform the main functions of the script on a background thread. """ try: # Setup self.StartedWork() self.WriteImageName( "Input1=", self.Input1 ) self.WriteImageName( "Input2=", self.Input2 ) # Create filter filterJointHistogram = itkJointHistogramImageFilter.New( self.Input1, self.Output ) self.AddEventHandlersToProcessObject( filterJointHistogram ) filterJointHistogram.SetInput( 0, self.Input1 ) filterJointHistogram.SetInput( 1, self.Input2 ) filterJointHistogram.NumberOfBins = System.Array[UInt32]( self.NumberOfBins ) filterJointHistogram.MarginalScale = self.MarginalScale filterJointHistogram.Mapping = self.Mapping self.WriteLineToConsole( "NumberOfBins=" + self.ArrayToString(self.NumberOfBins) ); self.WriteLineToConsole( "MarginalScale=" + self.MarginalScale.ToString() ); self.WriteLineToConsole( "Mapping=" + self.Mapping.ToString() ); filterJointHistogram.UpdateLargestPossibleRegion( ) filterJointHistogram.GetOutput( self.Output ) region = itkImageRegion( self.Output.Size, itkIndex(self.Output.Dimension) ) self.Output.SetRegions( region ) # Finish up self.Input1.DisconnectPipeline() self.Input2.DisconnectPipeline() self.Output.DisconnectPipeline() self.DisposeOfObject( filterJointHistogram ) pathInput1 = Path.GetDirectoryName( self.Input1.Name ) nameInput1 = Path.GetFileNameWithoutExtension( self.Input1.Name ) nameInput2 = Path.GetFileNameWithoutExtension( self.Input2.Name ) extInput1 = Path.GetExtension( self.Input1.Name ) fileOutput = Path.Combine( pathInput1, "JointHistogram_" + nameInput1 + "_with_" + nameInput2 + extInput1 ) self.WriteGivenOutputName( fileOutput ) self.FinishedWork( True ) except Exception, ex: self.HandleException( ex ) self.FinishedWork( False ) self.Finalise( )