Python VTK:点云和颜色条



我有一个文件,其中:第一列是x坐标,第二列是y坐标,第三列是z坐标,第四列是与每个点相关的值。我想把这些点画出来,每个点都应该根据第四列的颜色。我会用python来做。我在Windows上使用anaconda与vtk和vtk_visualizer。我有数百万个点。我发现更快的方法是使用python-vtk。这是我现在的代码:

import vtk
import numpy as np
## DATA
# Generate random points w/ random RGB colors
n     = 10**5
xyz   = 100*np.random.rand(n, 3)
color = 10*np.random.rand(n, 1)
# Point size
point_size = 10
## COLORMAP
cmax = np.max(color)
cmin = np.min(color)
cmed = (cmax+cmin)/2
normalizzato = color / np.max( np.absolute(cmax), np.absolute(cmin) )
rgb = np.zeros((len(color), 3))
for i in range(0, len(color) ):
    if color[i] >= cmed:
        # Red
        rgb[i][0] = 255*normalizzato[i]
    if color[i] < cmed:
        # Blue
        rgb[i][2] = 255*normalizzato[i]
## VTK
# Create the geometry of a point (the coordinate)
points = vtk.vtkPoints()
# Create the topology of the point (a vertex)
vertices = vtk.vtkCellArray()
# Setup colors
Colors = vtk.vtkUnsignedCharArray()
Colors.SetNumberOfComponents(3)
Colors.SetName("Colors")
# Add points
for i in range(0, len(xyz)):
    p = xyz[i]
    id = points.InsertNextPoint(p)
    vertices.InsertNextCell(1)
    vertices.InsertCellPoint(id)
    Colors.InsertNextTuple3(rgb[i][0], rgb[i][1], rgb[i][2])
# Create a polydata object
point = vtk.vtkPolyData()
# Set the points and vertices we created as the geometry and topology of the polydata
point.SetPoints(points)
point.SetVerts(vertices)
point.GetPointData().SetScalars(Colors)
point.Modified()
# Visualize
mapper = vtk.vtkPolyDataMapper()
if vtk.VTK_MAJOR_VERSION <= 5:
    mapper.SetInput(point)
else:
    mapper.SetInputData(point)
## ACTOR
# Create an actor
actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetPointSize(point_size)
axes = vtk.vtkAxesActor()
## RENDER
renderer = vtk.vtkRenderer()
# Add actor to the scene
renderer.AddActor(actor)
# Background
renderer.SetBackground(0.1, 0.2, 0.3)
# Reset camera
renderer.ResetCamera()
# Render window
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)
# Interactor
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)
# Begin interaction
renderWindow.Render()
renderWindowInteractor.Start()

这相当快。正如你所看到的,有一个颜色栏,但我不能得到适当的范围和颜色。什么好主意吗?你有什么建议关于如何取代## COLORMAP部分,并有一些东西,指的是一个真正的颜色映射?我也试过用maya .mlab。Point3d,但是非常慢,还有vtk_visualizer这里是代码:

from vtk_visualizer import *
import numpy as np    
# Generate random points w/ random RGB colors
n = 10**6
xyz = np.random.rand(n, 3)
color = 10*np.random.rand(n, 1)    
## Colormap
cmax = np.max(color)
cmin = np.min(color)
cmed = (cmax+cmin)/2
normalizzato = color / np.max( np.absolute(cmax), np.absolute(cmin) )
rgb = np.zeros((len(color), 3))
for i in range(0, len(color) ):
    if color[i] >= cmed:
        # Red
        rgb[i][0] = 255*normalizzato[i]
    if color[i] < cmed:
        # Blue
        rgb[i][2] = 255*normalizzato[i]    
# Stack arrays in sequence horizontally (column wise).
pc = np.hstack([xyz,rgb])    
# Plot them
plotxyzrgb(pc)

但是它比vtk慢,我不能改变点的大小,有一个颜色条和轴。

谢谢

我想你可以很容易地改变点的大小,看看这里:http://docs.enthought.com/mayavi/mayavi/auto/mlab_helper_functions.html#mayavi.mlab.points3d

也有不同大小点的例子

颜色图和坐标轴也可以简单地创建:

from mayavi import mlab
mlab.axes()
mlab.colorbar()

命令可以在这里找到:http://docs.enthought.com/mayavi/mayavi/auto/mlab_other_functions.html

相关内容

  • 没有找到相关文章

最新更新