#============================================================================== # # Project: SharpImage # Module: BinaryThresholdWithForm.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 Intensity library clr.AddReference("ManagedITK.ThresholdFilters") from itk import * class BinaryThresholdWithFormScript(SlicePipelineScriptObject): # ------------------------------------------------------------------------- Name = "BinaryThresholdWithForm" Help = """This filter produces an output image whose pixels are either one of two values (OutsideValue or InsideValue), depending on whether or not the corresponding input image pixel lies between the two thresholds (Lower and Upper). Values equal to either threshold is considered to be between the thresholds. The Lower and Upper parameters are specified using sliders.""" Parameters = """(pixel) InsideValue = the output value if between thresholds. (255) (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)""" InsideValue = None OutsideValue = None SliderMinimum = None SliderMaximum = None Increment = 1.0 Filter = None Form = None Variables = None # ------------------------------------------------------------------------- def SetupInsideOutsideValues(self): """ Setup the script inside and outside values """ image = self.GetInput() pixelType = image.PixelType if (self.InsideValue == None): self.InsideValue = itkPixel( pixelType, 255 ) if (self.OutsideValue == None): self.OutsideValue = itkPixel( pixelType, 0 ) def SetupSliderValues(self): """ Setup the script slider values """ image = self.GetInput() pixelType = image.PixelType if (self.SliderMinimum != None): # Do nothing - keep the user specified values self.SliderMinimum elif (image.Metadata.ContainsKey("MinimumValueAsD")): # Use the image min values self.SliderMinimum = image.Metadata["MinimumValueAsD"] else: # Use the pixel min values self.SliderMinimum = pixelType.MinValue if (self.SliderMaximum != None): # Do nothing - keep the user specified values self.SliderMaximum elif (image.Metadata.ContainsKey("MaximumValueAsD")): # Use the image max values self.SliderMaximum = image.Metadata["MaximumValueAsD"] else: # Use the pixel max values self.SliderMaximum = pixelType.MaxValue def AddVariablesToForm(self): """ Add the required variables to the form. """ self.SetupInsideOutsideValues( ) self.SetupSliderValues( ) 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 = itkBinaryThresholdImageFilter.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 inputs and parameters self.Pipeline.SetInput( self.Input ) self.Pipeline.LowerThreshold = itkPixel( self.Input.PixelType, self.Variables["Lower"] ) self.Pipeline.UpperThreshold = itkPixel( self.Input.PixelType, self.Variables["Upper"] ) self.Pipeline.InsideValue = self.InsideValue self.Pipeline.OutsideValue = self.OutsideValue # Write the parameters if not isSlice if (not isSlice): self.WriteLineToConsole( "Lower=" + self.Pipeline.LowerThreshold.ToString() ) self.WriteLineToConsole( "Upper=" + self.Pipeline.UpperThreshold.ToString() ) self.WriteLineToConsole( "InsideValue=" + self.Pipeline.InsideValue.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( )