简体   繁体   English

在 Mayavi 体积可视化中使用感知统一的颜色图

[英]Using perceptually uniform colormaps in Mayavi volumetric visualization

AFAIK Mayavi does not come with any perceptually uniform colormaps. AFAIK Mayavi 没有任何感知上统一的颜色图。 I tried naively to just pass it one of Matplotlib's colormaps but it failed:我天真地尝试将Matplotlib 的颜色图之一传递给它但它失败了:

from mayavi import mlab
import multiprocessing
import matplotlib.pyplot as plt

plasma = plt.get_cmap('plasma')

...
mlab.pipeline.volume(..., colormap=plasma)

TraitError: Cannot set the undefined 'colormap' attribute of a 'VolumeFactory' object. TraitError: 无法设置“VolumeFactory”对象的未定义“colormap”属性。


Edit: I found a guide to convert Matplotlib colormaps to Mayavi colormaps.编辑:我找到将 Matplotlib 颜色图转换为 Mayavi 颜色图的指南。 However, it unfortunately doesn't work since I am trying to use a volume using a perceptually uniform colormap.但是,不幸的是,它不起作用,因为我试图使用感知上统一的颜色图来使用体积。

from matplotlib.cm import get_cmap
import numpy as np
from mayavi import mlab

values = np.linspace(0., 1., 256)
lut_dict = {}
lut_dict['plasma'] = get_cmap('plasma')(values.copy())

x, y, z = np.ogrid[-10:10:20j, -10:10:20j, -10:10:20j]
s = np.sin(x*y*z)/(x*y*z)

mlab.pipeline.volume(mlab.pipeline.scalar_field(s), vmin=0, vmax=0.8, colormap=lut_dict['plasma'])  # still getting the same error
mlab.axes()
mlab.show()

... ...

Instead of setting it as the colormap argument, if you set it as the ColorTransferFunction of the volume, it works as expected.如果将其设置为卷的ColorTransferFunction ,则不是将其设置为colormap参数,而是按预期工作。

import numpy as np
from mayavi import mlab
from tvtk.util import ctf
from matplotlib.pyplot import cm

values = np.linspace(0., 1., 256)
x, y, z = np.ogrid[-10:10:20j, -10:10:20j, -10:10:20j]
s = np.sin(x*y*z)/(x*y*z)

volume = mlab.pipeline.volume(mlab.pipeline.scalar_field(s), vmin=0, vmax=0.8)
# save the existing colormap
c = ctf.save_ctfs(volume._volume_property)
# change it with the colors of the new colormap
# in this case 'plasma'
c['rgb']=cm.get_cmap('plasma')(values.copy())
# load the color transfer function to the volume
ctf.load_ctfs(c, volume._volume_property)
# signal for update
volume.update_ctf = True

mlab.show()

While the previous answer by like444 helped me partially with a similar problem, it leads to incorrect translation between colormaps.虽然 like444 的先前答案在一定程度上帮助了我解决了类似的问题,但它导致颜色图之间的转换不正确。 This is because the format in which matplotlib and tvtk store color information is slightly different: Matplotlib uses RGBA, while ColorTransferFunction uses VRGB, where V is the value in the shown data that this part of the colormap is assigned to.这是因为 matplotlib 和 tvtk 存储颜色信息的格式略有不同:Matplotlib 使用 RGBA,而 ColorTransferFunction 使用 VRGB,其中 V 是分配给这部分颜色图的显示数据中的值。 So by doing a 1-to-1 copy, green becomes red, blue becomes green and alpha becomes blue.因此,通过进行 1 对 1 复制,绿色变为红色,蓝色变为绿色,alpha 变为蓝色。 The following code snippet fixes that:以下代码片段修复了该问题:

def cmap_to_ctf(cmap_name):
    values = list(np.linspace(0, 1, 256))
    cmap = cm.get_cmap(cmap_name)(values)
    transfer_function = ctf.ColorTransferFunction()
    for i, v in enumerate(values):
        transfer_function.add_rgb_point(v, cmap[i, 0], cmap[i, 1], cmap[i, 2])
    return transfer_function

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

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