#============================================================================== # # Project: SharpImage # Module: WindowLevelWithForm.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 WindowLevelWithFormScript(SlicePipelineScriptObject): # ------------------------------------------------------------------------- Name = "WindowLevelWithForm" Help = """Applies a linear transformation to the intensity levels of the input image that are inside a user-defined interval. Values below this interval are mapped to a constant (itkPixelType.MinValue). Values above the interval are mapped to another constant (itkPixelType.MaxValue). This operation is very common in visualization, and can also be applied as a convenient preprocessing operation for image segmentation. The Window and Level parameters are specified using sliders.""" Parameters = """(pixel) OutputMinimum = the output image minimum. (min) (pixel) OutputMaximum = the output image maximum. (max) (double) Increment = slider value increment. (1.0)""" OutputMinimum = None OutputMaximum = None Increment = 1.0 # ------------------------------------------------------------------------- def AddVariablesToForm(self): """ Add the required variables to the form. """ # Setup min, max, and default values image = self.Renderer.Inputs[0] pixelType = image.PixelType pixelMin = pixelType.MinValue pixelMax = pixelType.MaxValue imageMin = pixelMin imageMax = pixelMax if (image.Metadata.ContainsKey("MinimumValueAsD")): imageMin = image.Metadata["MinimumValueAsD"] if (image.Metadata.ContainsKey("MaximumValueAsD")): imageMax = image.Metadata["MaximumValueAsD"] windowMin = 0.0 windowMax = 0.0 windowDefault = 0.0 levelMin = 0.0 levelMax = 0.0 levelDefault = 0.0 if (pixelType.IsChar): # Setup for Char windowMin = 0.0 windowMax = pixelMax windowDefault = windowMax levelMin = pixelMin levelMax = pixelMax levelDefault = (pixelMax + pixelMin + 1.0) / 2.0 self.OutputMinimum = itkPixel( pixelType, pixelMin ) self.OutputMaximum = itkPixel( pixelType, pixelMax ) elif (pixelType.IsShort): # Setup for Short windowMin = 0.0 windowMax = pixelMax windowDefault = windowMax levelMin = imageMin levelMax = imageMax levelDefault = (pixelMax + pixelMin + 1.0) / 2.0 self.OutputMinimum = itkPixel( pixelType, pixelMin ) self.OutputMaximum = itkPixel( pixelType, pixelMax ) elif (pixelType.IsLong): # Setup for Long windowMin = 0.0 windowMax = pixelMax windowDefault = windowMax levelMin = imageMin levelMax = imageMax levelDefault = (pixelMax + pixelMin + 1.0) / 2.0 self.OutputMinimum = itkPixel( pixelType, pixelMin ) self.OutputMaximum = itkPixel( pixelType, pixelMax ) elif (pixelType.IsReal): # Setup for Float or Double windowMin = 0.0 windowMax = abs( imageMax ) + abs( imageMin ) windowDefault = windowMax levelMin = imageMin levelMax = imageMax levelDefault = (imageMax + imageMin + 1.0) / 2.0 self.OutputMinimum = itkPixel( pixelType, imageMin ) self.OutputMaximum = itkPixel( pixelType, imageMax ) # Add variables self.Form.AddVariable( "Window", windowDefault, windowMin, windowMax, self.Increment ) self.Form.AddVariable( "Level", levelDefault, levelMin, levelMax, 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 = itkIntensityWindowingImageFilter.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 ) window = itkPixel( self.Input.PixelType, self.Variables["Window"] ) level = itkPixel( self.Input.PixelType, self.Variables["Level"] ) self.Pipeline.SetWindowLevel( window, level ) self.Pipeline.OutputMinimum = self.OutputMinimum self.Pipeline.OutputMaximum = self.OutputMaximum # Write the parameters if not isSlice if (not isSlice): self.WriteLineToConsole( "Window=" + window.ToString() ) self.WriteLineToConsole( "Level=" + level.ToString() ) self.WriteLineToConsole( "OutputMinimum=" + self.Pipeline.OutputMinimum.ToString() ) self.WriteLineToConsole( "OutputMaximum=" + self.Pipeline.OutputMaximum.ToString() ) # Copy the min/max from the input to the output (for not pixelType.IsChar images) if (self.Input.Metadata.ContainsKey("MinimumValue") and self.Input.Metadata.ContainsKey("MaximumValue")): self.Output.Metadata["MinimumValue"] = self.Input.Metadata["MinimumValue"] self.Output.Metadata["MaximumValue"] = self.Input.Metadata["MaximumValue"] self.Output.Metadata["MinimumValueAsD"] = self.Input.Metadata["MinimumValueAsD"] self.Output.Metadata["MaximumValueAsD"] = self.Input.Metadata["MaximumValueAsD"] # Update the pipeline and get output self.Pipeline.Update( ) self.Pipeline.GetOutput( self.Output ) # Disconnect inputs/outputs self.DisconnectInputAndOutput( )