簡體   English   中英

Python VTK:點雲和顏色條

[英]Python VTK: point cloud and colorbar

我有一個文件,其中:第一列是x坐標,第二列是y,第三列是z,第四列是與每個點關聯的值。 我想繪制這些點,並且每個點都應根據第4列着色。 我會在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部分的任何建議,並且有涉及真實色彩表的內容? 我也嘗試使用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慢,而且我無法更改色點和軸的點的大小。

謝謝

我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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM