【问题标题】:RBG image as texture for a 3D cube using matplotlib使用 matplotlib 将 RGB 图像作为 3D 立方体的纹理
【发布时间】:2020-05-27 13:24:31
【问题描述】:

我想使用 matplotlib 绘制一个 3D 立方体,并将任意 RGB 图像(jpg 或 png)作为纹理映射到立方体的每一侧。有一个很好的例子here using matlab

我只是想知道如何使用 Python/matplotlib 来实现,可能从 this example 开始。

【问题讨论】:

    标签: python matplotlib visualization texture-mapping


    【解决方案1】:

    我自己弄明白了,代码如下:

    import numpy as np
    import matplotlib.image as image
    import matplotlib.pyplot as plt
    
    C = image.imread('d:/timg.png')
    
    xp, yp, __ = C.shape
    
    x = np.arange(0, xp, 1)
    y = np.arange(0, yp, 1)
    Y, X = np.meshgrid(y, x)
    
    fig = plt.figure(figsize=(12,9))
    ax = fig.gca(projection='3d')
    ax.dist=6.2
    ax.view_init(elev=38, azim=-45)
    
    ax.plot_surface(X, Y, X-X+yp, facecolors=C,
                    rstride=2, cstride=2,
                    antialiased=True, shade=False)
    
    ax.plot_surface(X, X-X, Y, facecolors=np.fliplr(C.transpose((1,0,2))),
                    rstride=2, cstride=2,
                    antialiased=True, shade=False)
    
    ax.plot_surface(X-X+xp, X, Y, facecolors=np.fliplr(C.transpose((1,0,2))),
                    rstride=2, cstride=2,
                    antialiased=True, shade=False)
    

    生成的图像是:

    3D texture

    将stride改为1会得到更多png图片的细节,但似乎不是一个很有效的方法,尤其是当图片尺寸很大的时候。

    【讨论】:

      猜你喜欢
      • 2021-07-25
      • 2013-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-12
      相关资源
      最近更新 更多