[vtkusers] vtkImageResliceMapper and vtkImageSliceMapper

José M. Rodriguez Bacallao jmrbcu at gmail.com
Mon Jul 2 16:30:36 EDT 2012


hi folks, I am testing the following code, this example just change
from one slice to another in a mapper (slices are in a list of 2D
images, not a single 3D one) and update the render window:

import os, sys
import vtk, vtkgdcm, gdcm

def files_to_read(path):
    files = os.listdir(path)
    return [os.path.join(path, x) for x in files]

def sort_files(files):
    ipp = gdcm.IPPSorter()
    ipp.SetComputeZSpacing(True)
    ipp.SetZSpacingTolerance(1e-3);
    result = ipp.Sort(files);
    if result:
        return (ipp.GetFilenames(), ipp.GetZSpacing())

    return files, 0

def to_vtk_file_array(files):
    vtk_files = vtk.vtkStringArray()
    map(vtk_files.InsertNextValue, files)
    return vtk_files

images = []
current = 0
def next(o, e):
    global im, renWind, current, iren

    if iren.GetKeySym() == 'Right':
        current = (current + 1) % 200
    elif iren.GetKeySym() == 'Left':
        current = (current - 1) % 200

    im.SetInput(images[current])
    renWin.Render()


if __name__ == '__main__':
    # 200 images
    path = '/home/jmrbcu/pictures/workpictures/dicom/BRAIN/'
    print 'Reading files from %s' % path

    sorted_files, zspacing = sort_files(files_to_read(path))
    #sorted_files = to_vtk_file_array(sorted_files)
    print 'Files sorted, Z spacing is: %s' % zspacing

    for name in sorted_files:
        reader = vtkgdcm.vtkGDCMImageReader()
        reader.SetFileName(name)
        reader.Update()
        images.append(reader.GetOutput())

    ren1 = vtk.vtkRenderer()
    renWin = vtk.vtkRenderWindow()
    renWin.AddRenderer(ren1)

    im = vtk.vtkImageResliceMapper()
    im.SetInput(images[current])
    im.SliceFacesCameraOn()
    im.SliceAtFocalPointOn()
    im.BorderOff()

    ip = vtk.vtkImageProperty()
    ip.SetColorWindow(255)
    ip.SetColorLevel(128)
    ip.SetAmbient(0.0)
    ip.SetDiffuse(1.0)
    ip.SetOpacity(1.0)
    ip.SetInterpolationTypeToLinear()

    ia = vtk.vtkImageSlice()
    ia.SetMapper(im)
    ia.SetProperty(ip)

    ren1.AddViewProp(ia)
    ren1.SetBackground(0, 0, 0)
    renWin.SetSize(300, 300)

    iren = vtk.vtkRenderWindowInteractor()
    style = vtk.vtkInteractorStyleImage()
    style.SetInteractionModeToImageSlicing()
    style.SetImageOrientation([1, 0, 0], [0, -1, 0])
    style.AddObserver('KeyPressEvent', next)
    iren.SetInteractorStyle(style)
    renWin.SetInteractor(iren)

    renWin.Render();
    cam1 = ren1.GetActiveCamera()
    cam1.ParallelProjectionOn()
    ren1.ResetCameraClippingRange()
    renWin.Render()

    iren.Start()


the problem is that with my main machine (graphic card Intel G33,
Ubuntu Linux), it work flawlessly but with my laptop (Intel i845g
without hardware acceleration, Archlinux), it just show the first
slice (the mapper is not updated very well I think). The problem is
solved in the laptop changing from vtkImageResliceMapper to
vtkImageSliceMapper and all slice are showed when the slice change
action is requested, so, what to do, I really like to use
vtkImageResliceMapper over vtkImageSliceMapper as documentation says.



More information about the vtkusers mailing list