#============================================================================== # # Project: SharpImage # Module: ThresholdWithForm.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 reference and import Threshold library clr.AddReference("ManagedITK.ThresholdFilters") from itk import * class ThresholdWithFormScript(SlicePipelineScriptObject): # ------------------------------------------------------------------------- Name = "ThresholdWithForm" Help = """Sets image values to OutsideValue if they are outside specified values. This filter sets image values to OutsideValue (eg. black) if the image values are outside of the range [Lower..Upper]. The Lower and Upper parameters are specified using sliders.""" Parameters = """(pixel) OutsideValue = the output value if outside thresholds. (0) (double) SliderMinimum = the minimum slider value. (min) (double) SliderMaximum = the maximum slider value. (max) (double) Increment = slider value increment. (1.0)""" OutsideValue = None SliderMinimum = None SliderMaximum = None Increment = 1.0 Filter = None Form = None Variables = None # ------------------------------------------------------------------------- def AddVariablesToForm(self): """ Add the required variables to the form. """ # Setup InsideValue and OutsideValue image = self.Renderer.Inputs[0] pixelType = image.PixelType if (self.OutsideValue == None): self.OutsideValue = 0 # Setup SliderMinimum and SliderMaximum if (self.SliderMinimum != None and self.SliderMinimum != None): # Do nothing - keep the user specified values self.SliderMinimum self.SliderMaximum elif (image.Metadata.ContainsKey("MinimumValueAsD") and image.Metadata.ContainsKey("MaximumValueAsD")): # Use the image min/max values self.SliderMinimum = image.Metadata["MinimumValueAsD"] self.SliderMaximum = image.Metadata["MaximumValueAsD"] else: # Use the pixel min/max values self.SliderMinimum = pixelType.MinValue self.SliderMaximum = pixelType.MaxValue # Add variables self.Form.AddVariable( "Lower", self.SliderMinimum, self.SliderMinimum, self.SliderMaximum, self.Increment ) self.Form.AddVariable( "Upper", self.SliderMaximum, self.SliderMinimum, self.SliderMaximum, self.Increment ) 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. """ self.Pipeline = itkThresholdImageFilter.New( self.Input ) 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 inputs and parameters self.Pipeline.SetInput( self.Input ) self.Pipeline.OutsideValue = itkPixel( self.Output.PixelType, self.OutsideValue ) pixelLower = itkPixel( self.Output.PixelType, self.Variables["Lower"] ) pixelUpper = itkPixel( self.Output.PixelType, self.Variables["Upper"] ) self.Pipeline.ThresholdOutside( pixelLower, pixelUpper ) # Write the parameters if not isSlice if (not isSlice): self.WriteLineToConsole( "Lower=" + pixelLower.ToString() ) self.WriteLineToConsole( "Upper=" + pixelUpper.ToString() ) self.WriteLineToConsole( "OutsideValue=" + self.Pipeline.OutsideValue.ToString() ) # Update the pipeline and get output self.Pipeline.Update( ) self.Pipeline.GetOutput( self.Output ) # Disconnect inputs/outputs self.DisconnectInputAndOutput( ) def VariableChangedHandler(self, name, variables): """ Handle the Form.VariableChangedHandler event. """ if (name == "Lower" and variables["Lower"] > variables["Upper"]): variables["Upper"] = variables["Lower"] elif (name == "Upper" and variables["Lower"] > variables["Upper"]): variables["Lower"] = variables["Upper"] self.Variables = variables self.ApplyPipeline( True ) self.ShowOutputAsCurrentSlice( )