#============================================================================== # # Project: SharpImage # Module: LevelSetSpeed.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 SlicePipelineScript from SlicePipelineScript import * # Add references and import required libraries clr.AddReference("ManagedITK.LevelSetFilters") from itk import * class LevelSetSpeedScript(SlicePipelineScriptObject): # ------------------------------------------------------------------------- Name = "LevelSetSpeed" Help = """Generates a speed feature image for Level Set evolution.""" Parameters = """(pixel) OutputMinimum = the minimum value of the speed image. (-1.0) (pixel) OutputMaximum = the maximum value of the speed image. (+1.0) (bool) NormalizeAcrossScale = indicates if the scale is used. (False)""" OutputMinimum = -1.0 OutputMaximum = +1.0 NormalizeAcrossScale = False # ------------------------------------------------------------------------- def AddVariablesToForm(self): """ Add the required variables to the form. """ # Get pixelMin/Max and imageMin/Max image = self.Renderer.Inputs[0] pixelType = image.PixelType imageMin = pixelType.MinValue imageMax = pixelType.MaxValue if (image.Metadata.ContainsKey("MinimumValueAsD")): imageMin = image.Metadata["MinimumValueAsD"] if (image.Metadata.ContainsKey("MaximumValueAsD")): imageMax = image.Metadata["MaximumValueAsD"] # Add variables self.Form.AddVariable( "Gaussian Sigma", 0.5, 0.01, 5.0, 0.01 ) self.Form.AddVariable( "Sigmoid Alpha", -10.0, -50.0, 50.0, 0.1 ) self.Form.AddVariable( "Sigmoid Beta", 10.0, -50.0, 50.0, 0.1 ) self.Form.AddVariable( "Threshold Lower", imageMin, imageMin, imageMax, 1.0 ) self.Form.AddVariable( "Threshold Upper", imageMax, imageMin, imageMax, 1.0 ) self.Form.ValueStringFormat = "0.00" def CreatePipeline(self, isSlice): """ Set self.Pipeline to an instance of an itkPipeline. If isSlice is True the events are watched, otherwise events are NOT watched. """ # Create pipeline self.Pipeline = itkLevelSetSpeedImageFilter.New( self.Input, self.Output ) if (isSlice): self.Pipeline.RemoveAllObservers( ) else: self.AddEventHandlersToProcessObject( self.Pipeline ) def ApplyPipeline(self, isSlice): """ Apply self.Pipeline by setting the input, setting the filter parameters (stored in self.Variables), updating the pipeline, getting the output, and disconnecting the inputs and outputs. If isSlice is True the pipeline is being applied to the current slice only, otherwise the pipeline is being applied to whole image (eg. a 2D or 3D image). """ # Check the pipeline was not null if (self.Pipeline == None): return # Setup the pipeline parameters pixelType = self.Input.PixelType self.Pipeline.GaussianSigma = self.Variables["Gaussian Sigma"] self.Pipeline.GaussianNormalizeAcrossScale = self.NormalizeAcrossScale self.Pipeline.SigmoidAlpha = self.Variables["Sigmoid Alpha"] self.Pipeline.SigmoidBeta = self.Variables["Sigmoid Beta"] self.Pipeline.ThresholdLower = itkPixel( pixelType, self.Variables["Threshold Lower"] ) self.Pipeline.ThresholdUpper = itkPixel( pixelType, self.Variables["Threshold Upper"] ) self.Pipeline.OutputMinimum = itkPixel( pixelType, self.OutputMinimum ) self.Pipeline.OutputMaximum = itkPixel( pixelType, self.OutputMaximum ) # Write the parameters if not isSlice if (not isSlice): self.WriteLineToConsole( "Gaussian Normalize=" + self.NormalizeAcrossScale.ToString() ) self.WriteLineToConsole( "Gaussian Sigma=" + self.Variables["Gaussian Sigma"].ToString() ) self.WriteLineToConsole( "Sigmoid Alpha=" + self.Variables["Sigmoid Alpha"].ToString() ) self.WriteLineToConsole( "Sigmoid Beta=" + self.Variables["Sigmoid Beta"].ToString() ) self.WriteLineToConsole( "Threshold Lower=" + self.Variables["Threshold Lower"].ToString() ) self.WriteLineToConsole( "Threshold Upper=" + self.Variables["Threshold Upper"].ToString() ) self.WriteLineToConsole( "Rescale OutputMinimum=" + self.Pipeline.OutputMinimum.ToString() ) self.WriteLineToConsole( "Rescale OutputMaximum=" + self.Pipeline.OutputMaximum.ToString() ) # Connect the internal filters in the pipeline self.Pipeline.SetInput( self.Input ) self.Pipeline.GetOutput( self.Output ) # Update the pipeline and get output self.Pipeline.Update( ) # Disconnect inputs/outputs self.Output.DisconnectPipeline( ) self.Input.DisconnectPipeline( ) # Set min/max metadata for Renderer self.Output.Metadata["MinimumValue"] = itkPixel( pixelType, self.OutputMinimum ) self.Output.Metadata["MaximumValue"] = itkPixel( pixelType, self.OutputMaximum ) self.Output.Metadata["MinimumValueAsD"] = self.OutputMinimum self.Output.Metadata["MaximumValueAsD"] = self.OutputMaximum