VisPy中体素的透明度

时间:2018-07-12 13:05:25

标签: python 3d volume vispy voxels

我正在尝试使用python和VisPy显示来自numpy数组的3D数据。我查看了示例here,它非常有用。我已经有要显示的数据了-但是看起来不像我想要的那样。

现在我的数据显示方式我真的看不到每个体素。我可以看到形状的侧面,但说实话,它看起来像只是挖空的立方体。 It looks like this。我认为这可能是由于许多值都是负数,或者大多数值相对较小,而有一些较大的异常值。

无论哪种方式,我都希望能够分别设置值的透明度,例如,正值的透明度为70%,而小于-2但大于-5的值仅具有30%的透明度和绿色阴影。我希望这将使我能够看到实际的“体积”,而不是现在的体积。

这是我现在所拥有的内容,以防万一有人希望看到(注意,从我链接的示例vispy代码中大量复制和粘贴):

# Creating volume data (returns a 100x100x100 numpy array)
volume_data = func()

# Prepare canvas
canvas = scene.SceneCanvas(keys='interactive', size=(800, 600), show=True)

# Set up a viewbox to display the image with interactive pan/zoom
view = canvas.central_widget.add_view()

# Creating and assigning camera
camera = scene.cameras.ArcballCamera(parent=view.scene, fov=60)
view.camera = camera  

# Create the volume
volume = scene.visuals.Volume(volume_data, clim=(0, 1), parent=view.scene, 
threshold=0.225,emulate_texture=False)

volume.cmap = 'blues'

if __name__ == '__main__':
    print(__doc__)
    app.run()

1 个答案:

答案 0 :(得分:0)

也许您可以尝试将(0.,1.)之间的体积规格化,并定义具有多个透明度级别的颜色图。只是一个例子:

import numpy as np
from vispy import scene, app, io
from vispy.color import BaseColormap

class MultiLevels(BaseColormap):
    """Mix of green and red."""

    glsl_map = """
    vec4 translucent_fire(float t) {
        if (t > .5 && t < .6) {
            return vec4(1, 0., 0., .3);
        }
        else if (t >= .6) {
            return vec4(0, 1., 0., 1.);
        }
        else {
            return vec4(0., 0., 0., 0.);
        }
    }
    """

class Greens(BaseColormap):
    """Transparent green."""

    glsl_map = """
    vec4 translucent_fire(float t) {
        float alpha;
        if (t < .4) {
            alpha = 0.;
        }
        else if (t >= .4 && t < .5) {
            alpha = .1;
        }
        else {
            alpha = 1.;
        }
        return vec4(t, pow(t, 0.5), t*t, alpha);
    }
    """

# Creating volume data (returns a 100x100x100 numpy array)
volume_data = np.load(io.load_data_file('volume/stent.npz'))['arr_0'].astype(float)
volume_data /= volume_data.max()  # normalize the volume between (0., 1.)

# Prepare canvas
canvas = scene.SceneCanvas(keys='interactive', size=(800, 600), show=True)

# Set up a viewbox to display the image with interactive pan/zoom
view = canvas.central_widget.add_view()

# Creating and assigning camera
camera = scene.cameras.ArcballCamera(parent=view.scene, fov=60)
view.camera = camera

# Create the volume
method = 'translucent'  # 'mip'
volume = scene.visuals.Volume(volume_data, parent=view.scene, method=method)
volume.cmap = Greens()  # MultiLevels()

if __name__ == '__main__':
    app.run()