[vtkusers] Picking question

WolfgangZillig wollez at gmx.net
Mon Feb 26 09:57:24 EST 2007


Hello,

I'm importing some CT data into vtk and display it as an image. But I 
don't manage to create a picker that gives me the cell values.

So, how do I have to adjust the picker that I get the cell values 
(ideally the original values and not the color value that is displayed)?

Am I using the correct data representation or should I create it as 
regular mesh and import the data there?

How can I querry single values from my imported data (without a picker) 
just when I know the element location (cell 100, 124)?

Thanks for any help!

The imported file can be found here: 
http://perswww.kuleuven.be/~u0049353/vtk/14.RAD

My python script:

import vtk,array
from struct import unpack
from vtk.util.colors import *

# file to read
filein="./14.rad"
file=open(filein,'rb')
file.seek(13312) # removing header of file
valMatrix=file.read(1024*1024*2) # matrix
valInt=unpack('1048576h',valMatrix)
valArr=array.array('I',valInt)
# print min(valArr), max(valArr)

w=1024; h=1024
extent = (0, w - 1, 0, h - 1, 0, 0)

# importing the rad-file values to a vtk object
importer = vtk.vtkImageImport()
importer.SetDataScalarTypeToInt()
importer.SetWholeExtent(extent)
importer.SetDataExtent(extent)
importer.SetImportVoidPointer(valArr)
importer.SetDataSpacing(1,1,0)
importer.Update()

# flipping the image
flipX = vtk.vtkImageFlip()
flipX.SetInput(importer.GetOutput())
flipX.SetFilteredAxes(1)

# Start by creatin a black/white lookup table.
cLut = vtk.vtkLookupTable()
cLut.SetTableRange(min(valArr), max(valArr))
cLut.SetSaturationRange(0, 1)
cLut.SetHueRange(0, 1)
cLut.SetValueRange(0, 1)
# cLut.SetRampToLinear()
# cLut.SetRampToSCurve()
# cLut.SetRampToSQRT()
# cLut.SetScaleToLog10()
# dont forget to build the table
cLut.Build()

# transfering the image values to colors
iSS = vtk.vtkImageMapToColors()
iSS.SetInputConnection(flipX.GetOutputPort())
iSS.SetLookupTable(cLut)

# Create a cell picker.
picker = vtk.vtkCellPicker()

# the text rendering
textMapper = vtk.vtkTextMapper()
tprop = textMapper.GetTextProperty()
tprop.SetFontFamilyToArial()
tprop.SetFontSize(10)
tprop.BoldOn()
tprop.ShadowOn()
tprop.SetColor(1, 1, 0)
textActor = vtk.vtkActor2D()
textActor.VisibilityOff()
textActor.SetMapper(textMapper)

# Create a Python function to create the text for the text mapper used 
to display the results of picking.
def annLM(object,event):
     pos=iren.GetEventPosition() # x,y in windowcoords
     realpos=picker.Pick((pos[0],pos[1],0),ren)
     selPt = picker.GetSelectionPoint()
     picPos=picker.GetPickPosition()
     textMapper.SetInput("(%.2f, %.2f)"%(picPos[0],picPos[1]))
     textActor.SetPosition(selPt[:2])
     textActor.VisibilityOn()
     renWin.Render()


# image actor
iActor=vtk.vtkImageActor()
iActor.SetInput(iSS.GetOutput())

# renderer
ren = vtk.vtkRenderer()
ren.AddActor2D(textActor)
ren.AddActor(iActor)
ren.ResetCamera()
ren.GetActiveCamera().Zoom(1.45)

# render Window
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
renWin.SetSize(800,800)

# defining an interactorstyle
interactor = vtk.vtkInteractorStyleImage() # works this only with 
vtkImageActor()???
interactor.AddObserver('LeftButtonPressEvent',annLM)

# the window interactor
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
iren.SetInteractorStyle(interactor)
iren.SetPicker(picker)
iren.Initialize()
iren.Start()








More information about the vtkusers mailing list