【问题标题】:Python VTK: point cloud and colorbarPython VTK:点云和颜色条
【发布时间】:2015-05-24 16:41:41
【问题描述】:

我有一个文件,其中:第 1 列是 x 坐标,第 2 列是 y,第 3 列是 z,第 4 列是与每个点关联的值。 我想绘制这些点,每个点都应该根据第 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 慢,我无法更改点的大小,有一个颜色条和轴。

谢谢

【问题讨论】:

    标签: python python-2.7 point vtk mayavi


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回复。但我想使用 vtk,因为 mayavi 很慢。
    • 对于带有 vtk 的颜色条,我确实找到了以下代码:vtkSmartPointer&lt;vtkScalarBarActor&gt; scalarBar = vtkSmartPointer&lt;vtkScalarBarActor&gt;::New(); scalarBar-&gt;SetLookupTable(mapper-&gt;GetLookupTable()); scalarBar-&gt;SetTitle("Title"); scalarBar-&gt;SetNumberOfLabels(4);
    猜你喜欢
    • 2019-03-13
    • 2019-03-08
    • 1970-01-01
    • 2023-03-12
    • 2016-01-17
    • 1970-01-01
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多