Sim*_*her 2 python image mayavi
是否可以使用mayavi绘制带有3个颜色通道的图像?根据mayavi的文档,mayavi.mlab.imshow只能处理形状(nxm)的图像.
我不得不使用mayavi的自定义色图,请参阅http://docs.enthought.com/mayavi/mayavi/auto/example_custom_colormap.html.
我制作了一张色彩图,它由原始(nxmx 3)图像中的所有像素组成.我还包括一个alpha通道,以便正确显示透明png.接下来,我使用灰度图像作为查找表,其中pixle值作为存储在colormap中的原始像素的索引.我用查找表作为输入图像创建了一个imshow对象,并用我的自定义色彩映射替换了imshow对象的colormap.
这是一些工作代码,其中包含一个感兴趣的测试用例.除了在pl.imread(...)的测试用例之外,Pylab可以被Numpy(Pylab包裹Numpy)替换.在Windows 7上使用Mayavi 4.3.0在Python2.7上进行了测试.(不幸的是,我必须首先修复Windows上的以下mayavi错误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()
Run Code Online (Sandbox Code Playgroud)

| 归档时间: |
|
| 查看次数: |
1988 次 |
| 最近记录: |