【问题标题】:How to directly set RGB/RGBA colors in mayavi如何在 mayavi 中直接设置 RGB/RGBA 颜色
【发布时间】:2013-10-17 15:35:16
【问题描述】:

我有一个带有多个顶点的 mayavi 对象,我想直接为这些顶点设置 RGB 或 RGBA 值,而不是将自己限制为带有标量的单个颜色图。这如何实现?

【问题讨论】:

    标签: python mayavi


    【解决方案1】:

    据我所知,没有这样做的文档,但我找到了一种方法,只需最少的黑客攻击。这是一个最小的示例,可能需要对不同类型的来源进行一些修改:

    from tvtk.api import tvtk; from mayavi import mlab; import numpy as np
    
    x,y,z=np.random.random((3,nr_points)) #some data
    colors=np.random.randint(256,size=(100,3)) #some RGB or RGBA colors
    
    pts=mlab.points3d(x,y,z)
    sc=tvtk.UnsignedCharArray()
    sc.from_array(colors)
    
    pts.mlab_source.dataset.point_data.scalars=sc
    pts.mlab_source.dataset.modified()
    

    看起来有时您必须确保映射器指向正确的东西。对于上面的示例,这不是必需的,但可能是其他来源

    pts.actor.mapper.input=pts.mlab_source.dataset
    

    在某些时候应该更好地修复 mayavi API,以便有一个 API 可以为所有管道函数执行此操作,但事实证明这是一组相当复杂和全面的更改,我目前没有有时间完成。

    编辑: 用户 eqzx 发布了另一个问题 (Specify absolute colour for 3D points in MayaVi) 的答案,该问题可能更简单,尤其是对于某些难以使用 tvtk.UnsignedCharArray 的源类型。

    他的想法是创建一个跨越 256x256x256 RGB 值的整个范围的 LUT。请注意,此 LUT 因此有 16,777,216 个条目。如果您想在许多 vtk 对象中使用它,如果您不小心,可能会浪费大量内存。

    #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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-28
      • 2011-01-04
      • 2015-06-21
      • 1970-01-01
      • 2021-09-20
      • 2012-12-13
      • 2015-06-16
      • 1970-01-01
      相关资源
      最近更新 更多