使用 matplotlib 将 RBG 图像作为 3D 立方体的纹理
RBG image as texture for a 3D cube using matplotlib
我想使用 matplotlib 绘制 3D 立方体,并将任意 RGB 图像(jpg 或 png)作为纹理映射到立方体的每一侧。有个很好的例子.
我只是想知道如何使用 Python/matplotlib,可能从 开始。
我自己想出来的,下面是代码:
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图像的细节,但似乎不是一个非常有效的方法,尤其是当图像尺寸非常大时。
我想使用 matplotlib 绘制 3D 立方体,并将任意 RGB 图像(jpg 或 png)作为纹理映射到立方体的每一侧。有个很好的例子
我只是想知道如何使用 Python/matplotlib,可能从
我自己想出来的,下面是代码:
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图像的细节,但似乎不是一个非常有效的方法,尤其是当图像尺寸非常大时。