我正在尝试使用某些函数投影网格的一些点。我可以访问聚数据中的点和单元格数据。从那我得到投影点。现在我想用投影点替换这些点。有没有办法更新马亚维或 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)
现在为了保存,我尝试了上面的循环中的以下行:
polydata_return.GetCell(i).GetPoints() = np.array([projected_point1,projected_point2,projected_point3])
但出现如下错误: 语法错误:无法分配给函数调用
有没有办法用投影点替换/编辑当前点。提前谢谢。
更新:生成网格:
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))
生成上述网格后,我想使用以下图像将纹理映射到网格,然后我必须使用一些转换来展平网格,因此网格将与纹理一起展平。我这样做是为了拆包图像以消除图像中的失真。 纹理图像
为了投影图像,我使用以下代码:
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()
这应该让你开始,你只需要在正确的位置使用正确的数据。有几个属性要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()