Transparency with voxels in VisPy
我正在尝试使用python和VisPy显示来自numpy数组的3D数据。我在这里查看了示例,它非常有用。我已经显示了数据-但是看起来不像我想要的那样。
我的数据现在显示的方式确实看不到每个体素。我可以看到形状的侧面,但说实话,它看起来像只是一个挖空的立方体。看起来像这样。我认为这可能是由于许多值都是负数,或者大多数值相对较小,而有一些较大的异常值。
无论哪种方式,我都希望能够分别设置值的透明度-例如,使正值具有70%的透明度,而小于-2但大于-5的值仅具有30%的透明度和绿色阴影。我希望这将使我能够看到实际的"体积",而不是现在的体积。
这是我现在所看到的内容,以防万一有人想看到(请注意,从我链接的示例vispy代码中大量复制和粘贴):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | # 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() |
也许您可以尝试将(0.,1.)之间的体积标准化,并定义具有多个透明度级别的颜色图。举个例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | 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() |