使用VTK和Python进行可视化导致半规则模式

时间:2012-02-27 03:06:16

标签: python visualization vtk

您好,并提前感谢您的时间。

我目前正在创建一个软件包,允许用户可视化导入的数据。我目前使用的数据是.txt文件,其大小为120列乘24320行。我正在导入并转换为尺寸为120x128x190的3D网格。然后,我将此数据与阈值进行比较,并将值1分配给值大于阈值的任何单元格。这一切都很好,我甚至手动检查数据,保存并与txt文件交叉检查,这是正确的,但我不能让可视化工作正常,而不是输出心形,它给出了一个非常规则的网格像模式(见http://img855.imageshack.us/img855/4944/vtk.png)。我真的在试图让我的头发工作,所以任何帮助都会非常感激。我目前使用的代码是:

import vtk
from vtk.util import vtkImageImportFromArray
from numpy import *

#intensity threshold 900000

# import the data
# read files
intensityI=[]

print("Reading files...")

in_file = open("intensity.txt", "rt")
for line in in_file.readlines():
    intensityI.append([])
    for i in line.split():
        intensityI[-1].append(float(i))
in_file.close()

print("...done")

#set parameters specific to data
nx = 119
ny = 127
nz = 189
sizex=nx+1
sizey=ny+1
sizez=nz+1
threshold = 900000

#convert to array
I = zeros((sizez, sizex, sizey))
It = zeros((sizez, sizex, sizey), dtype=uint8)

print("converting to 3D array...")
z=0
while z<sizez:
    x=0
    while x<sizex:
        y=0
        while y<sizey:
            I[z][x][y] = int(intensityI[(z*128)+y][x])
            #print(I[z][x][y])
            #print(int(intensityI[(z*128)+y][x]))
            if I[z][x][y] > threshold:
                It[z][x][y] = 1
            y += 1
        x += 1
    z += 1

print("...done")

print("converting to string...")

# I now contains raw intensity information
# It contains thesholded intensity

dataImporter = vtk.vtkImageImport()
data_string = It.tostring()
dataImporter.CopyImportVoidPointer(data_string, len(data_string))
dataImporter.SetDataScalarTypeToUnsignedChar()
dataImporter.SetNumberOfScalarComponents(1)
dataImporter.SetDataExtent(0, nz, 0, ny, 0, nx)
dataImporter.SetWholeExtent(0, nz, 0, ny, 0, nx)

print("...done")
print("drawing...")

alphaChannelFunc = vtk.vtkPiecewiseFunction()
alphaChannelFunc.AddPoint(1, 0.05)
alphaChannelFunc.AddPoint(0, 0.0)

colorFunc = vtk.vtkColorTransferFunction()
colorFunc.AddRGBPoint(1, 1.0, 0.0, 0.0)
#colorFunc.AddRGBPoint(0, 0.0, 1.0, 0.0)

volumeProperty = vtk.vtkVolumeProperty()
volumeProperty.SetColor(colorFunc)
volumeProperty.SetScalarOpacity(alphaChannelFunc)

compositeFunction = vtk.vtkVolumeRayCastCompositeFunction()
volumeMapper = vtk.vtkVolumeRayCastMapper()
volumeMapper.SetVolumeRayCastFunction(compositeFunction)
volumeMapper.SetInputConnection(dataImporter.GetOutputPort())

volume = vtk.vtkVolume()
volume.SetMapper(volumeMapper)
volume.SetProperty(volumeProperty)

renderer = vtk.vtkRenderer()
renderWin = vtk.vtkRenderWindow()
renderWin.AddRenderer(renderer)
renderInteractor = vtk.vtkRenderWindowInteractor()
renderInteractor.SetRenderWindow(renderWin)

renderer.AddVolume(volume)
renderer.SetBackground(0,0,0)
renderWin.SetSize(400, 400)

def exitCheck(obj, event):
    if obj.GetEventPending() != 0:
        obj.SetAbortRender(1)

renderWin.AddObserver("AbortCheckEvent", exitCheck)

renderInteractor.Initialize()
renderWin.Render()
renderInteractor.Start()     
print("...done")

我认为问题是由于我使用numpy数组引起的,所以我试图使用vtkStructuredPoints但是如果我尝试将1写入vtkStrucutredPoints数组,我会收到以下错误:TypeError:'vtkobject'对象不可订阅在It[z][x][y] = 1行上,所以我不确定该错误消息的含义。

我用来创建vtkStructuredPoints数组的代码是

It = vtkStructuredPoints() 
It.SetDimensions(sizez, sizex, sizey) 
It.SetOrigin(0.0, 0.0, 0.0) 
It.SetSpacing(1.0, 1.0, 1.0) 

非常感谢您提供的任何帮助或指示。

约翰

0 个答案:

没有答案