VTK render window image to numpy array

做~自己de王妃 提交于 2019-12-13 12:54:17

问题


In VTK I am able to use the following snippet to save the render window as an image. However, actually I want to get it directly as a numpy array (without writing then reading).

im = vtkWindowToImageFilter()
writer = vtkPNGWriter()
im.SetInput(renderWindow)
im.Update()
writer.SetInputConnection(im.GetOutputPort())
writer.SetFileName("file.png")
writer.Write()

What is the best way to do this?


回答1:


I believe there is no need to involve vtkXWriter (where X is some format), except if you need the data in the X format. After you define the window from which you want to export its contents as an image, you can proceed to get a VTK image and work with that.

from vtk.util.numpy_support import vtk_to_numpy

...

vtk_rw = vtk.vtkRenderWindow()

...

vtk_win_im = vtk.vtkWindowToImageFilter()
vtk_win_im.SetInput(vtk_rw)
vtk_win_im.Update()

vtk_image = vtk_win_im.GetOutput()

width, height, _ = vtk_image.GetDimensions()
vtk_array = vtk_image.GetPointData().GetScalars()
components = vtk_array.GetNumberOfComponents()

arr = vtk_to_numpy(vtk_array).reshape(height, width, components)

...


来源:https://stackoverflow.com/questions/14553523/vtk-render-window-image-to-numpy-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!