VTK循环遍历图像中的像素

时间:2019-09-17 12:26:02

标签: python data-visualization vtk

您好,我正在研究要插入2D图像的项目。我知道如何在x,y点上循环并应用我的双线性插值方法,但是我不知道如何访问图像文件中x,y点处的像素值。我遇到过创建图像数据对象的资源,但是我还没有看到提取像素数据值的任何特定代码示例。

import vtk
from vtk.util.misc import vtkGetDataRoot
VTK_DATA_ROOT = vtkGetDataRoot()

imagefile = "clouds.jpg" # to load from current directory

# create a rendering window and renderer
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)

# create a renderwindowinteractor
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

useFactory = True

if useFactory==True:
    createReader = vtk.vtkImageReader2Factory()         #using factory to create reader
    reader = createReader.CreateImageReader2(imagefile) #create the reader
else:
    reader = vtk.vtkJPEGReader();

reader.SetFileName(imagefile)

# create an ImageActor. It has its own mapper included in the object
actor = vtk.vtkImageActor()
actor.GetMapper().SetInputConnection(reader.GetOutputPort())

#RIGHT HERE IS WHERE I WANT TO LOOP X,Y AND APPLY
#BILINEAR INTERPOLATION

# assign actor to the renderer
ren.AddActor(actor)

# enable user interface interactor
iren.Initialize()
renWin.Render()
iren.Start()

图像为256x256,所以我只打算在双四循环中应用我的方法并重建插值图像,但是我不知道如何在设定的x,y坐标下获得图像的值。任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

尝试使用numpy_support:

import vtk
from vtk.util.misc import vtkGetDataRoot
from vtk.util.numpy_support import vtk_to_numpy
VTK_DATA_ROOT = vtkGetDataRoot()

imagefile = "clouds.jpg" # to load from current directory

# create a rendering window and renderer
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)

# create a renderwindowinteractor
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

useFactory = True

if useFactory==True:
    createReader = vtk.vtkImageReader2Factory()         #using factory to create reader
    reader = createReader.CreateImageReader2(imagefile) #create the reader
else:
    reader = vtk.vtkJPEGReader();

reader.SetFileName(imagefile)
reader.Update()
imgdata = reader.GetOutput()


# create an ImageActor. It has its own mapper included in the object
actor = vtk.vtkImageActor()
actor.GetMapper().SetInputConnection(reader.GetOutputPort())

#RIGHT HERE IS WHERE I WANT TO LOOP X,Y AND APPLY
#BILINEAR INTERPOLATION
print(vtk_to_numpy(imgdata.GetPointData().GetScalars()))

# assign actor to the renderer
ren.AddActor(actor)

# enable user interface interactor
iren.Initialize()
renWin.Render()
iren.Start()