简体   繁体   English

Python VTK:点云和颜色条

[英]Python VTK: point cloud and colorbar

I have a file in which: the 1st column is the x coordinate, the 2nd is the y, 3rd is the z and the 4th is a value associated with each point. 我有一个文件,其中:第一列是x坐标,第二列是y,第三列是z,第四列是与每个点关联的值。 I would like to plot these points and each one should be colored according to the 4th columnin. 我想绘制这些点,并且每个点都应根据第4列着色。 I would to do this in python. 我会在python中做到这一点。 I am using anaconda with vtk and vtk_visualizer on Windows. 我在Windows上将Anaconda与vtk和vtk_visualizer一起使用。 I have milions of points. 我有数百万分。 The faster way I found is using python-vtk. 我发现更快的方法是使用python-vtk。 This is the code that I have now: 这是我现在拥有的代码:

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()

This is quite fast. 这是相当快的。 As you can see there is a colorbar but I am not able to get the proper range and color. 如您所见,有一个颜色栏,但我无法获得正确的范围和颜色。 Any ideas? 有任何想法吗? Do you have any suggestions about how to replace the ## COLORMAP section and have something that refers to a real colormap? 您是否有关于如何替换## COLORMAP部分的任何建议,并且有涉及真实色彩表的内容? I also tried to use mayavi.mlab.point3d but it is very slow and also vtk_visualizer here is the code: 我也尝试使用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)

But it is slower than vtk and I am not able to change the size of the points, having a colorbar and axis. 但这比vtk慢,而且我无法更改色点和轴的点的大小。

Thanks 谢谢

I mayavi you can easyly change the size of the points, look here: http://docs.enthought.com/mayavi/mayavi/auto/mlab_helper_functions.html#mayavi.mlab.points3d 我mayavi,您可以轻松更改点的大小,请看此处: http ://docs.enthought.com/mayavi/mayavi/auto/mlab_helper_functions.html#mayavi.mlab.points3d

There are examples with different sized points too. 也有一些具有不同大小点的示例。

Also the colormap and axes are simply created by using: 另外,颜色图和轴也可以使用以下方法简单地创建:

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

The commands can be found here: http://docs.enthought.com/mayavi/mayavi/auto/mlab_other_functions.html 可以在以下位置找到命令: http : //docs.enthought.com/mayavi/mayavi/auto/mlab_other_functions.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM