python - 在 MayaVi 中指定 3D 点的绝对颜色

标签 python point-clouds mayavi color-mapping

我正在使用 MayaVi使用 points3d 类绘制 3d 点的 Python 库。 The documentation指定每个点的颜色通过第四个参数指定,s:

In addition, you can pass a fourth array s of the same shape as x, y, and z giving an associated scalar value for each point, or a function f(x, y, z) returning the scalar value. This scalar value can be used to modulate the color and the size of the points.

这为每个点指定一个标量值,它将点映射到颜色图,例如 copperjethsv。例如。来自他们的文档:

import numpy
from mayavi.mlab import *

def test_points3d():
    t = numpy.linspace(0, 4*numpy.pi, 20)
    cos = numpy.cos
    sin = numpy.sin

    x = sin(2*t)
    y = cos(t)
    z = cos(2*t)
    s = 2+sin(t)

    return points3d(x, y, z, s, colormap="copper", scale_factor=.25)

给予:

enter image description here

相反,我想将每个点的实际值指定为 (r, g, b) 元组。这在 MayaVi 中可能吗?我尝试用元组数组替换 s,但抛出了错误。

最佳答案

在今天的大部分时间里为此苦苦挣扎之后,我找到了一种相对简单的方法来准确地完成问题的要求——为每个点指定一个 RGB 元组。诀窍就是定义一个颜色图,其条目数与要绘制的点数完全相同,然后将参数设置为索引列表:

# Imports
import numpy as np
from mayavi.mlab import quiver3d, draw

# Primitives
N = 200 # Number of points
ones = np.ones(N)
scalars = np.arange(N) # Key point: set an integer for each point

# Define color table (including alpha), which must be uint8 and [0,255]
colors = (np.random.random((N, 4))*255).astype(np.uint8)
colors[:,-1] = 255 # No transparency

# Define coordinates and points
x, y, z = colors[:,0], colors[:,1], colors[:,2] # Assign x, y, z values to match color
pts = quiver3d(x, y, z, ones, ones, ones, scalars=scalars, mode='sphere') # Create points
pts.glyph.color_mode = 'color_by_scalar' # Color by scalar

# Set look-up table and redraw
pts.module_manager.scalar_lut_manager.lut.table = colors
draw()

关于python - 在 MayaVi 中指定 3D 点的绝对颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18537172/

相关文章:

python - Pandas - 如何避免将 map 转换为 float

r - 使用 R 尽快计算大型点云的特征值

python - 将纹理映射到 mayavi 网格

python - 更改 mlab quiver3d 和 surf 数据源而不清除特征脚本中的数字

python - sklearn train_test_split - ValueError : Found input variables with inconsistent numbers of samples

python - 谷歌应用引擎: unitest works nosetest fails

point-cloud-library - 如何可视化 "XYZL"点云?

python - 如何有效地将点云数据的大型 numpy 数组转换为下采样的二维数组?

Mayavi 不会画线

python - 使用 WTForms-Alchemy 进行 CSRF 保护