9

我正在尝试制作一个 3D 图,该图由一系列通过 RGB 堆栈的 2D 平面组成,如下所示:

在此处输入图像描述

我知道可以mpl_toolkits.mplot3d通过将每个像素的 x、y、z 坐标和 RGB(A) 颜色传递给plot_surface

import numpy as np
from matplotlib import pyplot as pp
from mpl_toolkits.mplot3d.axes3d import Axes3D

def plot_stack_slices(rgbstack, scale=(1., 1., 1.), z_interval=10.):

    fig, ax = pp.subplots(1,1,subplot_kw={'projection':'3d'})
    ax.invert_zaxis()
    ax.hold(True)

    sx, sy, sz = scale
    nz, ny, nx, nc = rgbstack.shape

    stack_xyz = np.mgrid[:nx*sx:nx*1j, :ny*sy:ny*1j, :nz*sz:nz*1j]

    slices = rgbstack[::-z_interval]
    slice_xyz = np.rollaxis(stack_xyz, 3, 0)[::-z_interval]

    surflist = []

    for (img,xyz) in zip(slices, slice_xyz):
        x, y, z = xyz
        s = ax.plot_surface(x, y, z, facecolors=img**0.75, 
            rstride=50, cstride=50)
        surflist.append(s)

    return fig, ax, surflist

不幸的是,如果我设置为以全分辨率显示纹理,这会变得非常慢。rstride=1, cstride=1

我也知道Mayavi可以轻松处理以全分辨率显示多个 2D 纹理:

from mayavi import mlab

def plot_stack_slices2(stack, scale=(1., 1., 20.), z_interval=10.):

    mfig = mlab.figure(bgcolor=(1,)*3)

    sx, sy, sz = scale
    nz, ny, nx = stack.shape

    slices = stack[::-z_interval]
    slice_z = np.linspace(0,nz*sz,nz)[::z_interval]

    surflist = []

    for (img,z) in zip(slices, slice_z):
        im = mlab.imshow(img.T, colormap='gray', figure=mfig)
        im.actor.scale = [sx,sy,sz]
        im.actor.position = [0, 0, z]
        surflist.append(z)


    return fig, surflist

但是,现在的问题是似乎没有任何方法可以使用 Mayavi 显示真彩色 RGB 纹理 -根据文档,我只能指定单个(R, G, B)元组或预定义的颜色图。

有谁知道在 3D 图中显示真彩色 2D RGB 纹理的更好方法?

如果有足够的时间,我可能会弄清楚如何在 Vtk 甚至纯 OpenGL 中做到这一点,但我真的希望有现有的库可以完成这项工作。

4

2 回答 2

6

非常感谢 aestrivex 提供使用 Mayavi/VTK 的工作解决方案 - 这是我将来可能需要做更复杂的事情的有用信息。

最后,我实际上选择了 cgohlke 的使用 visvis 的建议结果证明实现起来要简单得多:

import visvis as vv
vv.use('wx')

import numpy as np
from matplotlib.image import imread
from matplotlib.cbook import get_sample_data

imgdata = imread(get_sample_data('lena.png'))

nr, nc = imgdata.shape[:2]
x,y = np.mgrid[:nr, :nc]
z = np.ones((nr, nc))

for ii in xrange(5):
    vv.functions.surf(x, y, z*ii*100, imgdata, aa=3)

在此处输入图像描述

于 2013-10-20T11:37:27.510 回答
4

我不知道其他库——volshow 看起来很整洁,但我还没有测试过——但你可以在 vtk 中做到这一点。

我一直致力于在 mayavi 中执行此操作(请参阅如何在 mayavi 中直接设置 RGB/RGBA 颜色),但对于某些图像源,mayavi 以一种根本不适合处理此问题的方式构建 vtk 管道。我从 mlab.imshow 开始将 2D vtk.ImageData 转换为真彩色的努力在每一步都遇到了阻力,但我做到了。

首先,这是我如何在 mayavi 中使用 mlab 做到这一点的。即使按照我的标准,这也太 hacky 和“魔术”了:

from mayavi import mlab
import numpy as np
from tvtk.api import tvtk

k=mlab.imshow(np.random.random((10,10)),colormap='bone')

colors=tvtk.UnsignedCharArray()
colors.from_array(np.random.randint(256,size=(100,3)))
k.mlab_source.dataset.point_data.scalars=colors
k.actor.input.point_data.scalars=colors
#the latter set of scalars is what is actually used in the VTK pipeline in this 
#case, but if they don't play nice with the mayavi source then tvtk will
#complain because we are circumventing the structure it expects
k.actor.input.scalar_type='unsigned_char'
k.actor.input.number_of_scalar_components=3
k.image_map_to_color.lookup_table=None

k.actor.input.modified()
mlab.draw()
#this draw fails.  As it fails, there is an interaction here, somewhere deep in 
#tvtk, causing the ImageData to partially reset.. I have not been able to track 
#it down yet.  ignore the error output

k.actor.input.scalar_type='unsigned_char'
k.actor.input.number_of_scalar_components=3
#now after we reset these back to what they should be, it works

mlab.draw()
mlab.show()

但在纯粹的 tvtk 中,它并没有那么糟糕:

import numpy as np
from tvtk.api import tvtk

colors=np.random.randint(256,size=(100,3))

an_image=tvtk.ImageData()
an_image.number_of_scalar_components=3
an_image.scalar_type='unsigned_char'
an_image.point_data.scalars=tvtk.UnsignedCharArray()
an_image.point_data.scalars.from_array(colors)
an_image.dimensions=np.array((10,10,1))

an_actor=tvtk.ImageActor()
an_actor.input=an_image
an_actor.interpolate=False

ren=tvtk.Renderer()
renWin=tvtk.RenderWindow()
renWin.add_renderer(ren)
ren.add_actor2d(an_actor)
iren=tvtk.RenderWindowInteractor()
iren.render_window=renWin
iren.interactor_style=tvtk.InteractorStyleTrackballCamera()
renWin.render()
iren.start()

当然,在 vtk 中做更多的工作。你甚至可以很好地包装它,这样它就很合理了。

我想修复 mayavi 以正确处理此问题,但正如您从我的片段中看到的那样,这并不简单,可能需要一段时间。

于 2013-10-18T16:44:24.617 回答