I have a small python VTK
function that calculates the volume and surface area of an object embedded in a stack of TIFF
images. To read the TIFF's
into VTK
, I have used vtkTIFFReader
and processed the result using vtkImageThreshold
. I then use vtkMassProperties
to extract the volume and surface area of the object identified after thresholding.
With VTK-5.04
, this function returns the correct value for a test stack (3902 pixels). However, using VTK-5.4.2
the same function returns a different value (422 pixels). Can someone explain this?
Code
def testvtk():
# read 36 TIFF images. Each TIFF is 27x27 pixels
v16=vtk.vtkTIFFReader()
v16.SetFilePrefix("d:/test/slice")
v16.SetDataExtent(0,27,0,27,1,36)
v16.SetFilePattern("%s%04d.tif")
v16.SetDataSpacing (1,1,1)
v16.Update()
# Threshold level for seperating background/foreground pixels
maxthres=81
# Threshold the image stack
thres=vtk.vtkImageThreshold()
thres.SetInputConnection(v16.GetOutputPort())
thres.ThresholdByLower(0)
thres.ThresholdByUpper(maxthres)
# create ISO surface from thresholded images
iso=vtk.vtkImageMarchingCubes()
iso.SetInputConnection(thres.GetOutputPort())
# Have VTK calculate the Mass (volume) and surface area
Mass = vtk.vtkMassProperties()
Mass.SetInputConnection(iso.GetOutputPort())
Mass.Update()
# just print the results
print "Volume = ", Mass.GetVolume()
print "Surface = ", Mass.GetSurfaceArea()
Note
By testing both VTK-5.4.2 and VTK-5.2.1, I narrowed things down a bit and believe this behaviour was introduced between versions 5.0.4 and 5.2.1.
Update
It seems that in VTK-5.4.2, vtkTIFFReader ignores the x and y values set in the SetDataSpacing method. Instead vtkTIFFReader is calculating the x and y dataspacing from the resolution reported by the TIFF files.