1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
#!/usr/bin/env python
## Program: VMTK
## Module: $RCSfile: vmtkimageshiftscale.py,v $
## Language: Python
## Date: $Date: 2009/01/09 13:56:00 $
## Version: $Revision: 1.0 $
## Copyright (c) Luca Antiga, David Steinman. All rights reserved.
## See LICENCE file for details.
## 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.
## Note: this class was contributed by
## Hugo Gratama van Andel
## Academic Medical Centre - University of Amsterdam
## Dept. Biomedical Engineering & Physics
import vtk
import sys
import pypes
vmtkimageshiftscale = 'vmtkImageShiftScale'
class vmtkImageShiftScale(pypes.pypeScript):
def __init__(self):
pypes.pypeScript.__init__(self)
self.Image = None
self.OutputType = 'unchanged'
self.Shift = 0.0
self.Scale = 1.0
self.MapRanges = 0
self.InputRange = [0.0,0.0]
self.OutputRange = [0.0,0.0]
self.ClampOverflowOn = 1
self.SetScriptName('vmtkimageshiftscale')
self.SetScriptDoc('shift and scale the intensity of an image and cast it to a specified type')
self.SetInputMembers([
['Image','i','vtkImageData',1,'','the input image','vmtkimagereader'],
['OutputType','type','str',1,'["unchanged","float","double","uchar","char","ushort","short","long","ulong","int","uint"]','the output image type - use "unchanged", the default, to keep same type as input'],
['ClampOverflowOn','clamp','bool',1,'','Whith ClampOverflow On, the data is thresholded so that the output value does not exceed the max or min of the data type'],
['Shift','shift','float',1,'','the shift value'],
['Scale','scale','float',1,'','the scale value'],
['MapRanges','mapranges','bool',1,'','toggle mapping of input range to output range instead of simple shift scale'],
['InputRange','inputrange','float',2,'','the input range that will be mapped in the output range - leave default or set to 0.0 0.0 for using input image scalar range as input range'],
['OutputRange','outputrange','float',2,'','the output range into which the input range will be mapped']
])
self.SetOutputMembers([
['Image','o','vtkImageData',1,'','the output image','vmtkimagewriter']
])
def Execute(self):
if self.Image == None:
self.PrintError('Error: No input image.')
if self.MapRanges:
if self.InputRange == [0.0,0.0]:
self.InputRange = self.Image.GetScalarRange()
if self.InputRange[1] == self.InputRange[0]:
self.PrintError('Error: Input range has zero width. Cannot map values')
self.Shift = -self.InputRange[0]
self.Scale = (self.OutputRange[1] - self.OutputRange[0]) / (self.InputRange[1] - self.InputRange[0])
shiftScale = vtk.vtkImageShiftScale()
shiftScale.SetInput(self.Image)
shiftScale.SetShift(self.Shift)
shiftScale.SetScale(self.Scale)
if self.OutputType == 'double':
shiftScale.SetOutputScalarTypeToDouble()
elif self.OutputType == 'uchar':
shiftScale.SetOutputScalarTypeToUnsignedChar()
elif self.OutputType == 'char':
shiftScale.SetOutputScalarTypeToChar()
elif self.OutputType == 'ushort':
shiftScale.SetOutputScalarTypeToUnsignedShort()
elif self.OutputType == 'short':
shiftScale.SetOutputScalarTypeToShort()
elif self.OutputType == 'ulong':
shiftScale.SetOutputScalarTypeToUnsignedLong()
elif self.OutputType == 'long':
shiftScale.SetOutputScalarTypeToLong()
elif self.OutputType == 'uint':
shiftScale.SetOutputScalarTypeToUnsignedInt()
elif self.OutputType == 'int':
shiftScale.SetOutputScalarTypeToInt()
elif self.OutputType == 'float':
shiftScale.SetOutputScalarTypeToFloat()
if self.OutputType != 'unchanged':
if self.ClampOverflowOn:
shiftScale.ClampOverflowOn()
shiftScale.ClampOverflowOff()
shiftScale.Update()
self.Image = shiftScale.GetOutput()
if self.MapRanges and self.OutputRange[0] != 0.0:
shiftScale2 = vtk.vtkImageShiftScale()
shiftScale2.SetInput(self.Image)
shiftScale2.SetShift(self.OutputRange[0])
shiftScale2.SetScale(1.0)
shiftScale2.Update()
self.Image = shiftScale2.GetOutput()
if __name__=='__main__':
main = pypes.pypeMain()
main.Arguments = sys.argv
main.Execute()
|