[Paraview] Python Programmable Filter: Reading unsigned short TIFFs into vtkImageData

Allan Lyckegaard allan at lyckegaard.dk
Mon Nov 17 06:19:40 EST 2014


Hi paraview list

Based on this blog post: http://www.kitware.com/blog/home/post/534, I am
trying to make a Python plugin (source) that reads a stack of unsigned
short TIFFs (uint16) and outputs 3D unsigned short imagedata in Paraview.

Assigning the geometry to the imagedata works fine (see def
RequestInformation). My problem is that the values of the voxels in the
imagedata is not assigned correct (see def RequestData). According to
Paraview information tab the data type is unsigned short, but when reading
in a known TIFF (8x8 with uint16 values 1..64) the values of the imagedata
are not correct.

I suspect the last line "array.SetValue(offset+v, vals.GetValue(v))" is the
problem. Does the vals.GetValue(v) get casted into a Python type int before
stored in the array?

When I print vals.GetValue(v) to a textfile, I can see that I read the
correct values from the TIFF.

Can anybody see what the problem is?

Cheers,
Allan


def RequestData():
    import os
    from paraview import vtk
    from vtk import vtkTIFFReader # this reader does not exist in paraview??

    filepath = Filepath # path to file
    filestr = Filename  # string containing filename and %d
    first = First       # first image, e.g. 0 or 1
    last = Last         # last image
    N = last-first+1    # number of images

    # Get a vtkImageData object for the output
    pdo = self.GetOutput()

    print 'read'
    # set up reader
    reader = vtkTIFFReader()
    filename = filestr % first
    reader.SetFileName(os.path.join(filepath, filename))
    reader.Update()

    # get tiff info
    ext = reader.GetDataExtent()

    # set up output volume
    pdo.SetExtent(0, ext[1], 0, ext[3], 0, N-1)
    #pdo.SetDimensions(ext[1]+1,ext[3]+1,N)
    pdo.SetOrigin(0,0,0)
    pdo.SetSpacing(1,1,1)
    pdo.AllocateScalars(vtk.VTK_UNSIGNED_SHORT,1)

    # temporary array for voxel values
    array = vtk.vtkUnsignedShortArray()
    array.SetName('Voxels')
    array.SetNumberOfComponents(1)
    #array.SetNumberOfTuples(pdo.GetNumberOfPoints())
    array.SetNumberOfValues(pdo.GetNumberOfPoints())
    pdo.GetPointData().AddArray(array)

    # fill array with tiffs
    for i in range(N):
        filename = filestr % (first+i)
        print filename
        reader.SetFileName(os.path.join(filepath, filename))
        reader.Update()
        image = reader.GetOutput()
        vals = image.GetPointData().GetArray('Tiff Scalars')
        offset = i*N
        for v in range((ext[0]+1)*(ext[1]+1)):
            print vals.GetValue(v)
            array.SetValue(offset+v, vals.GetValue(v))


def RequestInformation():
    import os
    from paraview import vtk, util
    from vtk import vtkTIFFReader

    filepath = Filepath # path to file
    filestr = Filename  # string containing filename and %d
    first = First       # first image, e.g. 0 or 1
    last = Last         # last image

    # set up reader
    reader = vtkTIFFReader()
    filename = filestr % first
    reader.SetFileName(os.path.join(filepath, filename))
    reader.Update()

    # get tiff info
    ext = reader.GetDataExtent()

    # update extent information
    util.SetOutputWholeExtent(self, [0, ext[1], 0, ext[3], 0, last-first])
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/paraview/attachments/20141117/6fdd1c5a/attachment-0001.html>


More information about the ParaView mailing list