问题描述
是否可以使用mayavi绘制具有3个颜色通道的图像?根据mayavi的文档,mayavi.mlab.imshow
仅能处理形状(n x m)的图像.
Is it possible to draw a image with 3 color channels using mayavi? According to the documentation for mayavi, mayavi.mlab.imshow
can only handle images of shape (n x m).
推荐答案
方法
我必须使用mayavi的自定义颜色图,请参见 http://docs .enthought.com/mayavi/mayavi/auto/example_custom_colormap.html .
我制作了一个色彩图,其中包含原始(n x m x 3)图像中所有像素的行.我还包括一个Alpha通道,以便可以正确显示透明png.接下来,我将灰度图像用作查找表,并使用像素值作为存储在颜色图中的原始像素的索引.我用查找表作为输入图像创建了一个imshow对象,并用我的自定义颜色图替换了imshow对象的颜色图.
I made a colormap that consists of all the pixles in the original (n x m x 3) image as rows. I also included an alpha channel so that transparent png's can be displayed correctly. Next I used a grayscale image as a lookup table, with pixle values as indices to the original pixles stored in the colormap. I created an imshow object with the lookup table as the input image and replaced the imshow object's colormap with my custom colormap.
这里有一些有用的代码,带有测试用例,供有兴趣的人使用.除在测试用例中的pl.imread(...)之外,Pylab均可被Numpy替换(Pylab包装Numpy).在Windows 7上使用Mayavi 4.3.0在Python2.7上进行了测试.(不幸的是,我必须首先在Windows https://github.com/enthought/mayavi/pull/96/files ).
Here is some working code with a test case for those interested. Pylab can be replaced by Numpy (Pylab wraps Numpy) everywhere except in the testcase at pl.imread(...). Tested on Python2.7 with Mayavi 4.3.0 on Windows 7. (Unfortunately I had to first fix the following mayavi bug on Windows https://github.com/enthought/mayavi/pull/96/files).
import pylab as pl
from mayavi import mlab
def mlab_imshowColor(im, alpha=255, **kwargs):
"""
Plot a color image with mayavi.mlab.imshow.
im is a ndarray with dim (n, m, 3) and scale (0->255]
alpha is a single number or a ndarray with dim (n*m) and scale (0->255]
**kwargs is passed onto mayavi.mlab.imshow(..., **kwargs)
"""
try:
alpha[0]
except:
alpha = pl.ones(im.shape[0] * im.shape[1]) * alpha
if len(alpha.shape) != 1:
alpha = alpha.flatten()
# The lut is a Nx4 array, with the columns representing RGBA
# (red, green, blue, alpha) coded with integers going from 0 to 255,
# we create it by stacking all the pixles (r,g,b,alpha) as rows.
myLut = pl.c_[im.reshape(-1, 3), alpha]
myLutLookupArray = pl.arange(im.shape[0] * im.shape[1]).reshape(im.shape[0], im.shape[1])
#We can display an color image by using mlab.imshow, a lut color list and a lut lookup table.
theImshow = mlab.imshow(myLutLookupArray, colormap='binary', **kwargs) #temporary colormap
theImshow.module_manager.scalar_lut_manager.lut.table = myLut
mlab.draw()
return theImshow
def test_mlab_imshowColor():
"""
Test if mlab_imshowColor displays correctly by plotting the wikipedia png example image
"""
#load a png with a scale 0->1 and four color channels (an extra alpha channel for transparency).
from urllib import urlopen
url = 'http://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png'
im = pl.imread(urlopen(url), format='png')
im *= 255
mlab_imshowColor(im[:, :, :3], im[:, :, -1])
mlab.points3d([-200, 300, -200, 300],
[-200, 300, 200, -300],
[300, 300, 300, 300])
mlab.show()
if __name__ == "__main__":
test_mlab_imshowColor()
结果
这篇关于如何在Mayavi(ImShow)中绘制彩色图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!