#============================================================================== # # Project: SharpImage # Module: ShiftScaleWithForm.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.IntensityFilters") from itk import * class ShiftScaleWithFormScript(SlicePipelineScriptObject): # ------------------------------------------------------------------------- Name = "ShiftScaleWithForm" Help = """Applies a linear transformation by firstly adding Shift to each pixel, then multiplying each pixel by Scale. The Shift and Scale parameters are specified using sliders.""" Parameters = """(double) ShiftMinimum = the minimum slider value. (-max) (double) ShiftMaximum = the maximum slider value. ( max) (double) ShiftIncrement = the shift slider value increment. (1.0) (double) ScaleIncrement = the shift slider value increment. (0.1)""" ShiftMinimum = None ShiftMaximum = None ShiftIncrement = 1.0 ScaleIncrement = 0.1 # ------------------------------------------------------------------------- def AddVariablesToForm(self): """ Add the required variables to the form. """ # Get pixelMax and imageMax image = self.Renderer.Inputs[0] pixelType = image.PixelType pixelMax = pixelType.MaxValue imageMax = pixelMax if (image.Metadata.ContainsKey("MaximumValueAsD")): imageMax = image.Metadata["MaximumValueAsD"] # Setup ShiftMinimum and ShiftMaximum if (self.ShiftMinimum == None and pixelType.IsChar): # Setup for Char self.ShiftMinimum = -1.0 * Math.Abs( pixelMax ) if (self.ShiftMaximum == None and pixelType.IsChar): self.ShiftMaximum = pixelMax if (self.ShiftMinimum == None and pixelType.IsShort): # Setup for Short self.ShiftMinimum = -1.0 * Math.Abs( imageMax ) if (self.ShiftMaximum == None and pixelType.IsShort): self.ShiftMaximum = imageMax if (self.ShiftMinimum == None and pixelType.IsLong): # Setup for Long self.ShiftMinimum = -1.0 * Math.Abs( imageMax ) if (self.ShiftMaximum == None and pixelType.IsLong): self.ShiftMaximum = imageMax if (self.ShiftMinimum == None and pixelType.IsReal): # Setup for Real self.ShiftMinimum = -1.0 * Math.Abs( imageMax ) if (self.ShiftMaximum == None and pixelType.IsReal): self.ShiftMaximum = imageMax # Setup Scale minimum and maximum scaleMaximum = 25.0 scaleMinimum = 0.0 if (pixelType.IsSigned): scaleMinimum = -1.0*scaleMaximum # Add variables self.Form.AddVariable( "Shift", 0.0, self.ShiftMinimum, self.ShiftMaximum, self.ShiftIncrement ) self.Form.AddVariable( "Scale", 1.0, scaleMinimum, scaleMaximum, self.ScaleIncrement ) 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 = itkShiftScaleImageFilter.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.Shift = self.Variables["Shift"] self.Pipeline.Scale = self.Variables["Scale"] # Write the parameters if not isSlice if (not isSlice): self.WriteLineToConsole( "Shift=" + self.Pipeline.Shift.ToString() ) self.WriteLineToConsole( "Scale=" + self.Pipeline.Scale.ToString() ) # Update the pipeline and get output self.Pipeline.Update( ) self.Pipeline.GetOutput( self.Output ) # Disconnect inputs/outputs self.DisconnectInputAndOutput( )