[vtkusers] VTK Reading 8-bit tiff files (solved) VTK4.2

Karl Garsha garsha at itg.uiuc.edu
Thu Nov 6 17:06:53 EST 2003


Greetings,
It looks like I was able to answer my own question with a little digging 
around.  For those that may encounter the challenge of dealing with 
8-bit tiffs in the future, here is how I was able to put together a 
solution that works in WinXP as well as OSX (and probably Linux, Solaris 
etc...):
    The key is to get away from vtkTIFFReader for actually opening the 
files--it is still handy to find out the dimensions of the dataset using 
vtkTIFFReader.  My example is in Python(2.3).
#************************************
import os #just some preliminary stuff
import vtk
os.chdir('/Workspace/Shared')
#************************************
RdrTiff=vtk.vtkTIFFReader() #vtkTIFFReader can be useful for extracting 
some info from the*.tif file
RdrTiff.SetFileName('test5.tif')
RdrTiff.Update()
DataExt=RdrTiff.GetDataExtent() #Creates a list with data extents 
(x0,x1,y0,y1,z0,z1)
#************************************
RdrLowLvl=vtk.vtkImageReader() #Use vtkImageReader rather than vtkTIFFReader
RdrLowLvl.SetDataExtent(DataExt) #We can use the data extent derived above
RdrLowLvl.SetDataScalarTypeToUnsignedChar() #this is the key to 8-bit 
scalars
RdrLowLvl.SetFileName('test5.tif')
RdrLowLvl.Update()
#************************************
BW_LUT=vtk.vtkLookupTable() #We must use a lookup table to map the 
scalar values to HSVA colors for display
BW_LUT.SetTableRange(0,255) #Sets range of values for our 8-bit unsigned 
char scalars
BW_LUT.SetSaturationRange(0,0) #Sets saturation range between 0 and 1 (0 
for greyscale)
BW_LUT.SetHueRange(0,0) #Sets hue between 0 and 1 (0 for black and white)
BW_LUT.SetValueRange(0,1) #sets value range between 0 and 1--we use 
whole range for greyscale here
BW_LUT.Build() #Don't forget to build the LUT
#************************************
LUT_Mapper=vtk.vtkImageMapToColors() #Now we have to map the LUT to the data
LUT_Mapper.SetInput(RdrLowLvl.GetOutput()) #Gets output from image 
reader for mapping of scalars
LUT_Mapper.SetLookupTable(BW_LUT) #Sets the lookup table we wish to use
LUT_Mapper.Update() #Update for good measure--probably not nessacary
#************************************
BW_Viewer=vtk.vtkImageViewer() #We can use vtkImageViewer to see if our 
data looks right
BW_Viewer.SetColorLevel(125) #In medical imaging, the color level 
specifies the median value for the LUT ramp--changing this value effects 
contrast in different parts of the histogram
BW_Viewer.SetColorWindow(255) #The color window specifies the width of 
the range of values to be used; the default values for color level and 
color window will not work very well with our data
#************************************
BW_Viewer.SetInput(LUT_Mapper.GetOutput()) #get the LUT mapped data to 
be viewed
BW_Viewer.Render() #Hopefully this renders the image-it worked for me...
   

Karl Garsha wrote:

> I've narrowed down the problem to vtkTIFFReader.  On Windows the 
> scalar range for the image data right out of the Reader is (0.0,0.0), 
> on the Mac the scalar range comes out of the Reader as (0.0,252.0).  
> Any workaround ideas are appreciated.  Thanks.
> -Karl
>
> Karl Garsha wrote:
>
>> Greetings,
>> I'm experiencing anomolous behavior when trying to read 8-bit 
>> greyscale images on Windows with the Python wrappers.  The code I'm 
>> using follows:
>>
>> Reader=vtk.vtkTIFFReader()
>> Reader.SetFileName('test.tif')
>> Reader.Update()
>> LUM=vtk.vtkImageLuminance()
>> LUM.SetInput(Reader.GetOutput())
>> LUM.Update()
>> ImageData1=LUM.GetOutput()
>> ImageData1.GetNumberOfScalarComponents()
>>
>> #***Result on WinXP
>> 1
>> #***Result on OSX
>> 1
>>
>> ImageData1.GetScalarRange()
>>
>> #***Result on WinXP
>> (0.0,0.0)
>> #***Result on OSX
>> (2.0, 251.0)  # This is the answer I expect
>>
>> Does anyone have some ideas about how I can go about reading 8-bit 
>> B&W or color indexed images in a cross platform manner?  I'm running 
>> Python2.3, wxWindows and VTK 4.2 on both machines (although I was 
>> lazy and just downloaded the Enthought Edition of SciPy 2.3 for 
>> Windows--my mac has everything up to date from CVS).  Is 
>> vtkImageLuminance or vtkTIFFReader known to behave differently on 
>> Windows?  vtkTIFFReader works fine if the image is converted to RGB 
>> greyscale in other software before reading, but that is a cumbersome 
>> approach.  Thanks in advance for any insights.
>> Best,
>> Karl
>>
>

-- 
Karl Garsha
Light Microscopy Specialist
Imaging Technology Group
Beckman Institute for Advanced Science and Technology
University of Illinois at Urbana-Champaign
405 North Mathews Avenue
Urbana, IL 61801
Office: B650J
Phone: 217.244.6292
Fax: 217.244.6219
Mobile: 217.390.1874
www.itg.uiuc.edu





More information about the vtkusers mailing list