如何在vtk文件中编辑点数据

时间:2019-07-16 07:02:01

标签: python-3.x vtk mayavi

我正在尝试使用某些功能投影网格的某些点。我可以从多数据访问点和单元格数据。由此,我得到了预测点。现在,我想用投影点替换那些点。有什么办法可以更新mayavi或vtk中的点?

要从.ply文件中获取多数据

from plyfile import PlyData,PlyElement
import numpy as np 
import time
from mayavi import mlab
from tvtk.api import tvtk

plydata = PlyData.read(ply_file)
points = plydata.elements[0].data

# Get X,Y,Z coordinates from .ply file
x,y,z = [],[],[]                                                                                                                                                                                    

for i in points: 
    x.append(i[0]) 
    y.append(i[1]) 
    z.append(i[2]) 

s = [0.1]*len(x)

mesh = mlab.pipeline.delaunay2d(pts)
surf = mlab.pipeline.surface(mesh,color=(1,1,1))

actor = surf.actor.actors[0]
polydata = tvtk.to_vtk(actor.mapper.input)

投影点:

for i in range(polydata.GetNumberOfCells()):
    pts = polydata.GetCell(i).GetPoints()    
    np_pts = np.array([pts.GetPoint(i) for i in range(pts.GetNumberOfPoints())])
    projected_point1,projected_point2,projected_point3 = project_points(point1,point2,point3)

现在,为了保存,我在上面的for循环中尝试了以下行:

polydata_return.GetCell(i).GetPoints() = np.array([projected_point1,projected_point2,projected_point3])

,但出现以下错误: SyntaxError:无法分配给函数调用

有什么方法可以用投影点替换/编辑当前点。预先感谢。

更新: 生成网格:

from mayavi import mlab
from tvtk.api import tvtk
import numpy as np
from plyfile import PlyData,PlyElement

x = [random.randint(0,250) for i in range(250)]
y = [random.randint(0,250) for i in range(250)]
z = [random.randint(0,5) for i in range(250)]
s = [0.1]*len(x)

pts = mlab.points3d(x, y, z,s)
mesh = mlab.pipeline.delaunay2d(pts)
surf = mlab.pipeline.surface(mesh,color=(1,1,1))

生成上面的网格后,我想使用下面的图像将纹理映射到网格,然后我必须使用一些变换来展平网格,因此网格将与纹理一起展平。我这样做是为了对图像进行解包以消除图像中的失真。 texture image

对于投影图像,我使用以下代码:

image_file = texture_image_path
if image_file.split('.')[-1] == 'png':
    img = tvtk.PNGReader()
elif image_file.split('.')[-1] == 'jpg':
    img = tvtk.JPEGReader()

img.file_name=image_file
texture = tvtk.Texture(input_connection=img.output_port, interpolate=0)

surf.actor.enable_texture = True  
surf.actor.tcoord_generator_mode = 'plane'  
surf.actor.actor.texture = texture
mlab.show()

1 个答案:

答案 0 :(得分:0)

这应该可以帮助您入门,您只需要在正确的位置使用正确的数据即可。 mesh.mlab_source有几个属性。 我在评论中添加了一些。

from mayavi import mlab
from tvtk.api import tvtk
import numpy as np
#from plyfile import PlyData,PlyElement
import random

x = [random.randint(0,250) for i in range(250)]
y = [random.randint(0,250) for i in range(250)]
z = [random.randint(0,5) for i in range(250)]
s = [0.1]*len(x)

pts = mlab.points3d(x, y, z,s)
mesh = mlab.pipeline.delaunay2d(pts)
surf = mlab.pipeline.surface(mesh,color=(1,1,1))

#After generating above mesh I want to map a texture to mesh using following image and then I have to flattened the mesh using some transformation, so mesh will get flattened along with a texture. I doing this for dewrapping image to remove distortion in image. texture image

#For projecting image I use following code:

image_file = 'texture.jpg'
if image_file.split('.')[-1] == 'png':
    img = tvtk.PNGReader()
elif image_file.split('.')[-1] == 'jpg':
    img = tvtk.JPEGReader()


img.file_name=image_file
texture = tvtk.Texture(input_connection=img.output_port, interpolate=1)
#texture = tvtk.Texture(interpolate=1)
#texture.input = img

surf.actor.enable_texture = True  
surf.actor.tcoord_generator_mode = 'plane'  
surf.actor.actor.texture = texture

#src = mlab.pipeline.scalar_scatter(x, y, z, s)
#src.mlab_source.dataset.lines = connections src.update(
#src.mlab_source.reset()

@mlab.animate
def anim():
    for i in range(10):
        print(i)

        #plt.mlab_source.set(x=x, y=y, z=z)
        print(surf.mlab_source.dataset)
        x = [random.randint(0,250) for i in range(250)]
        y = [random.randint(0,250) for i in range(250)]
        z = [random.randint(0,5) for i in range(250)]
        s = [0.1]*len(x)

        pts = mlab.points3d(x, y, z,s)
        mesh = mlab.pipeline.delaunay2d(pts)


        surf.mlab_source.x = mesh.mlab_source.x
        surf.mlab_source.y = mesh.mlab_source.y
        surf.mlab_source.z = mesh.mlab_source.z


        #s.mlab_source.scalars = np.asarray(x*0.1*(i+1), 'd')
        yield

anim()

mlab.show()