Python VTK:点云和颜色条

时间:2015-05-24 16:41:41

标签: python python-2.7 point vtk mayavi

我有一个文件,其中:第一列是x坐标,第二列是y,第三列是z,第四列是与每个点相关的值。 我想绘制这些点,每个点应根据第4列进行着色。 我想在python中这样做。我在Windows上使用带有vtk和vtk_visualizer的anaconda。 我有数百万的积分。我找到的更快的方法是使用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部分有任何建议,并且有一些引用真彩色图的内容吗? 我也尝试使用mayavi.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慢,我无法改变点的大小,有一个颜色条和轴。

由于

1 个答案:

答案 0 :(得分:0)

我愿意你可以轻松改变积分的大小,看看这里: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