我有一个解决方法。这里的要点是,当您将数据切换为体积渲染时,ctf 和 otf保持在原位,而不是进行奇怪的重新缩放和颜色更改。解决方案是双重的。
1) 确保所有数据量都精确地缩放到0-255。您必须确保最小值和最大值分别正好是 0 和 255,而不仅仅是在 [0,255] 范围内。这很重要,因为当您切换出 array.scalar_data 时,mayavi 会尝试根据您的数据重新调整 ctf/otf 范围。然而,它以奇怪的方式失败了。
2) 不要使用 SceneEditor(MayaviScene) 的内置编辑器,而是直接公开与您的数据关联的 Volume.view 特征。更改基础数据时,对此 ctf/otf 编辑器的更改不会将音量从蓝绿红模式切换到蓝红渐变。这是如何做到的:(关键行是 Item('vol_module',style='custom',show_label=False)
B=io.imread(bad)
G=io.imread(good)
B=B-B.min();B/=B.max();B*=255
G=G-G.min();G/=G.max();G*=255
class Vis(HasTraits):
query=Str()
scene = Instance(MlabSceneModel,())
switch_data=Button()
#You have to put vol_module here (or call add_trait later in init) for View() to see it
vol_module=Instance(Volume(),())
editor=SceneEditor(scene_class=MayaviScene)
def __init__(self,G,B):
self.G=G
self.B=B
HasTraits.__init__(self)
self.array_src=ArraySource()
self.array_src.scalar_data=G
self.G_active=True
self.scene.mayavi_scene.add_child(self.array_src)
self.volume=self.array_src.add_module(self.vol_module)
def _switch_data_fired(self):
if self.G_active:
self.array_src.scalar_data=self.B
self.G_active=False
else:
self.array_src.scalar_data=self.G
self.G_active=True
traits_view = View(
HGroup(
Item('scene',editor=editor,height=250, width=300, show_label=False),
Item('vol_module',style='custom',show_label=False),#critical
)
)
vis=Vis(G,B)
#vis=Vis(B,G)
vis.configure_traits()