#============================================================================== # # Project: SharpImage # Module: ImageRange.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 ThreadedScript from ThreadedScript import * # Add reference and import Calculators library clr.AddReference("ManagedITK.Calculators") from itk import * class ImageRangeScript(ThreadedScriptObject): # ------------------------------------------------------------------------- Name = "ImageRange" Help = """Sets the shown range of the given image. All UC images have the range [0..255], whereas all other types have the range [min..max]. This script allows the user to change this range.""" Parameters = """(pixel) Minimum = the minimum image range value. (min) (pixel) Maximum = the maximum image range value. (max)""" Minimum = None Maximum = None # ------------------------------------------------------------------------- def Initialise(self): """ Initialise the environment for running this script. """ """ NOTE: This function is invoked on the main UI thread. """ ThreadedScriptObject.Initialise(self) self.Renderer = self.ParentApplication.CurrentRenderer self.Input = self.Renderer.Inputs[0] def ThreadedDoWork(self): """ Perform the main functions of the script. """ """ NOTE: This function is invoked on a background thread. """ try: self.StartedWork() # Compute the image min/max values if required calcMinMax = None if (self.Minimum == None or self.Maximum == None): calcMinMax = itkMinimumMaximumImageCalculator.New( self.Input ) calcMinMax.SetImage( self.Input ) calcMinMax.Compute() self.Minimum = calcMinMax.Minimum.ValueAsD self.Maximum = calcMinMax.Maximum.ValueAsD # Write to console self.WriteLineToConsole( "Minimum=" + self.Minimum.ToString() ) self.WriteLineToConsole( "Maximum=" + self.Maximum.ToString() ) # Set the image range min/max values metadataContainsMin = self.Input.Metadata.ContainsKey("MinimumValueAsD") metadataContainsMax = self.Input.Metadata.ContainsKey("MaximumValueAsD") if (metadataContainsMin and metadataContainsMax): self.Input.Metadata["MinimumValueAsD"] = self.Minimum self.Input.Metadata["MaximumValueAsD"] = self.Maximum self.Input.Metadata["MinimumValue"] = itkPixel( self.Input.PixelType, self.Minimum ) self.Input.Metadata["MaximumValue"] = itkPixel( self.Input.PixelType, self.Maximum ) else: self.Input.Metadata.Add( "MinimumValueAsD", self.Minimum ) self.Input.Metadata.Add( "MaximumValueAsD", self.Maximum ) self.Input.Metadata.Add( "MinimumValue", itkPixel(self.Input.PixelType, self.Minimum) ) self.Input.Metadata.Add( "MaximumValue", itkPixel(self.Input.PixelType, self.Maximum) ) # Initialise the renderer to rescale the input image self.Renderer.InitialiseInputs() self.Renderer.Repaint() # Finish up self.DisposeOfObject( calcMinMax ) self.FinishedWork( True ) except Exception, ex: self.HandleException( ex ) self.FinishedWork( False ) self.Finalise( )