简体   繁体   English

使用 python 将 PNG 序列加载到 vtkImageData 以进行 3D 体积渲染

[英]Load sequence of PNGs into vtkImageData for 3D volume render using python

I have a sequence of about 100 PNG files containing 512x512 pre-segmented CAT scan data.我有一个大约 100 个 PNG 文件的序列,其中包含 512x512 预分割的 CAT 扫描数据。 I want to use vtk on Python to create a 3D model using marching cubes algorithm.我想在 Python 上使用 vtk 使用行进立方体算法创建 3D 模型。 The part that I don't know how to do is to load the sequence of PNG files and convert them to a single vtk pixel data object suitable for sending to the vtkDiscreteMarchingCubes algorithm.我不知道该怎么做的部分是加载 PNG 文件序列并将它们转换为适合发送到vtkDiscreteMarchingCubes算法的单个 vtk 像素数据对象。

I also think that I need to convert the pixel values of the PNG data because right now the data is in the alpha channel, so this needs to be converted into scalar data with values of zero and 1.我还认为我需要转换 PNG 数据的像素值,因为现在数据在 alpha 通道中,因此需要将其转换为值为 0 和 1 的标量数据。

use vtkPNGreader and load in individual slices and then populate a vtkImageData which you can define the dimensions as and for each z-slice or image fill the image data form the output of the reader into your vtkImageData.使用 vtkPNGreader 并加载单个切片,然后填充一个 vtkImageData,您可以将其定义为每个 z 切片或图像的尺寸,并将读取器输出的图像数据填充到您的 vtkImageData 中。

Rough pseudocode - not checked for bugs :)粗略的伪代码 - 未检查错误 :)

import vtk
from vtk.util import numpy_support

pngfiles = glob.glob('*.png')

png_reader = vtk.vtkPNGReader()
png_reader.SetFileName(pngfiles[0])
x,y = png_reader.GetOutput().GetDimensions()

data_3d = np.zeros([x,y,len(pngfiles)])

for i,p in enumerate(png):
    png_reader.SetFileName(pngfiles[0])
    png_reader.Update()
    img_data = png_reader.GetOutput()
    data_3D[:,:,i] = numpy_support.vtk_to_numpy(img_data)

#save your 3D numpy array out.
data_3Dvtk = numpy_support.numpy_to_vtk(data_3D)

Just in case anyone stumbles on here looking for another way to do this only using vtk, you can use vtkImageAppend class.以防万一有人偶然在这里寻找另一种方法来仅使用 vtk 来执行此操作,您可以使用vtkImageAppend类。

def ReadImages(files):
    reader = vtk.vtkPNGReader()
    image3D = vtk.vtkImageAppend()
    image3D.SetAppendAxis(2)
    
    for f in files:
        reader.SetFileName(f)
        reader.Update()
        t_img = vtk.vtkImageData()
        t_img.DeepCopy(reader.GetOutput())
        image3D.AddInputData(t_img)

    image3D.Update()

    return image3D.GetOutput()

for converting the data you can take a look at what the output of t_img.GetPointData().GetArray('PNGImage') gives and see if it is the expected value.要转换数据,您可以查看 t_img.GetPointData().GetArray('PNGImage') 的输出结果,看看它是否是预期值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM