在 3D 图中显示真彩色 2D RGB 纹理? [英] Displaying true-colour 2D RGB textures in a 3D plot?

查看:29
本文介绍了在 3D 图中显示真彩色 2D RGB 纹理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to make a 3D plot that consists of a series of 2D planes through an RGB stack, like this:

我知道可以使用 mpl_toolkits.mplot3d 来做到这一点,方法是将每个像素的 x、y、z 坐标和 RGB(A) 颜色传递给 plot_surface:

I know that it's possible to do this using mpl_toolkits.mplot3d by passing the x, y, z coordinates and the RGB(A) colours of each pixel to 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 以全分辨率显示纹理,这会变得非常慢.

Unfortunately this becomes extremely slow if I set rstride=1, cstride=1 in order to display the textures at full resolution.

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

I'm also aware that Mayavi can easily handle displaying multiple 2D textures at full resolution:

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) 元组或预定义的颜色图.

However, the problem now is that there does not seem to be any way of displaying true-colour RGB textures using Mayavi - according to the docs I can only specify either a single (R, G, B) tuple, or a pre-defined colourmap.

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

如果有足够的时间,我可能会弄清楚如何在 Vtk 甚至纯 OpenGL 中执行此操作,但我真的希望有现有的库可以完成这项工作.

Given enough time I could probably figure out how do do this in Vtk or even pure OpenGL if necessary, but I'm really hoping that there are existing libraries that will do the job.

推荐答案

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

Big thanks to aestrivex for providing working solutions using Mayavi/VTK - it's useful info that I may need for doing more complicated things in the future.

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

In the end I actually chose to go with cgohlke's suggestion of using visvis, which turned out to be a lot simpler to implement:

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)

这篇关于在 3D 图中显示真彩色 2D RGB 纹理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆