【问题标题】:How to draw a color image in mayavi (imshow)如何在 mayavi (imshow) 中绘制彩色图像
【发布时间】:2014-06-28 21:40:09
【问题描述】:

是否可以使用 mayavi 绘制具有 3 个颜色通道的图像?根据 mayavi 的文档,mayavi.mlab.imshow 只能处理形状为 (n x m) 的图像。

【问题讨论】:

    标签: python image mayavi


    【解决方案1】:

    方法

    我必须使用 mayavi 的自定义颜色图,请参阅 http://docs.enthought.com/mayavi/mayavi/auto/example_custom_colormap.html

    我制作了一个颜色图,其中包含原始 (n x m x 3) 图像中的所有像素作为行。我还包括一个 alpha 通道,以便可以正确显示透明的 png。接下来,我使用灰度图像作为查找表,像素值作为存储在颜色图中的原始像素的索引。我用查找表作为输入图像创建了一个 imshow 对象,并用我的自定义颜色图替换了 imshow 对象的颜色图。

    代码

    这里有一些带有测试用例的工作代码,供感兴趣的人参考。 Pylab 可以在任何地方都被 Numpy(Pylab 包装 Numpy)替换,除了 pl.imread(...) 的测试用例中。在 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()
    

    结果

    【讨论】:

    • 感谢您的代码,但是,它没有显示我机器中的骰子图像,只是一个灰色从黑色渐变为白色的飞机。我正在使用 mayavi 4.5
    • 这里也一样。在 Mayavi 4.5 上
    • 从那以后我就没用过 Mayavi,太糟糕了,它不再起作用了。我只记得每件小事都很难编码。
    【解决方案2】:

    Table was removed in 4.4.3

    load_lut_from_file 似乎与 imshow 并没有像预期的那样工作。 Setting the color directly in the actor 是唯一对我有用的方法。

    import pylab as pl
    import numpy as np
    
    from mayavi import mlab
    from urllib import urlopen
    from tvtk.api import tvtk
    
    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).
        url = 'http://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png'
        im = pl.imread(urlopen(url), format='png') * 255
    
        colors = tvtk.UnsignedCharArray()
        colors.from_array(im.transpose((1,0,2)).reshape(-1, 4))
        m_image = mlab.imshow(np.ones(im.shape[:2]))
        m_image.actor.input.point_data.scalars = colors
    
        mlab.points3d([-200, 300, -200, 300],
                      [-200, 300, 200, -300],
                      [300, 300, 300, 300])
    
        mlab.draw()
        mlab.show()
    
    if __name__ == "__main__":
        test_mlab_imshowColor()
    

    【讨论】:

      猜你喜欢
      • 2018-06-20
      • 1970-01-01
      • 2013-04-06
      • 2019-10-16
      • 1970-01-01
      • 2022-01-14
      • 1970-01-01
      • 1970-01-01
      • 2019-09-17
      相关资源
      最近更新 更多