Bil*_*ham 12 python color-mapping point-clouds mayavi
我正在使用MayaVi Python库来绘制3d点,使用points3d
该类.文档指定通过第四个参数指定每个点的颜色s
:
此外,您可以传递与x,y和z相同形状的第四个数组s,为每个点提供关联的标量值,或者返回标量值的函数f(x,y,z).此标量值可用于调整点的颜色和大小.
这指定了每个点的标量值,将点映射到颜色映射,例如copper
,jet
或hsv
.例如,从他们的文件:
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)
Run Code Online (Sandbox Code Playgroud)
得到:
相反,我想将每个点的实际值指定为(r,g,b)元组.这可能在MayaVi中吗?我已经尝试用s
一个元组数组替换,但是会抛出一个错误.
Cli*_*err 12
在今天的大部分时间里努力解决这个问题后,我发现了一个相对简单的方法来完成问题所要求的 - 为每个点指定一个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()
Run Code Online (Sandbox Code Playgroud)
您可以使用rgb查找表,并使用所需的任何逻辑将rgb值映射到该表。这是一个简单的例子:
import numpy, random
from mayavi.mlab import *
def cMap(x,y,z):
#whatever logic you want for colors
return [random.random() for i in x]
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 = cMap(x,y,z)
return points3d(x, y, z, s, colormap="spectral", scale_factor=0.25)
test_points3d()
Run Code Online (Sandbox Code Playgroud)
我不知道您想要什么配色方案,但是您可以评估x,y,z的位置并返回与您要查找的rgb值相对应的任何标量。
我找到了一种更好的直接设置颜色的方法。
您可以轻松创建自己的直接LUT。假设我们想要256 ** 3粒度:
#create direct grid as 256**3 x 4 array
def create_8bit_rgb_lut():
xl = numpy.mgrid[0:256, 0:256, 0:256]
lut = numpy.vstack((xl[0].reshape(1, 256**3),
xl[1].reshape(1, 256**3),
xl[2].reshape(1, 256**3),
255 * numpy.ones((1, 256**3)))).T
return lut.astype('int32')
# indexing function to above grid
def rgb_2_scalar_idx(r, g, b):
return 256**2 *r + 256 * g + b
#N x 3 colors
colors = numpy.array([_.color for _ in points])
#N scalars
scalars = numpy.zeros((colors.shape[0],))
for (kp_idx, kp_c) in enumerate(colors):
scalars[kp_idx] = rgb_2_scalar_idx(kp_c[0], kp_c[1], kp_c[2])
rgb_lut = create_8bit_rgb_lut()
points_mlab = mayavi.mlab.points3d(x, y, z
keypoint_scalars,
mode = 'point')
#magic to modify lookup table
points_mlab.module_manager.scalar_lut_manager.lut._vtk_obj.SetTableRange(0, rgb_lut.shape[0])
points_mlab.module_manager.scalar_lut_manager.lut.number_of_colors = rgb_lut.shape[0]
points_mlab.module_manager.scalar_lut_manager.lut.table = rgb_lut
Run Code Online (Sandbox Code Playgroud)
color
现在可以简单地通过参数来完成
from mayavi import mlab
import numpy as np
c = np.random.rand(200, 3)
r = np.random.rand(200) / 10.
mlab.points3d(c[:, 0], c[:, 1], c[:, 2], r, color=(0.2, 0.4, 0.5))
mlab.show()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4572 次 |
最近记录: |