来自一组2D图像的Python 3D等值面

时间:2015-07-09 06:57:29

标签: python numpy 3d

我正在使用此代码:

z = np.asarray(image_list)
mydata = z[::1,::1]
fig = pl.figure(facecolor='w')

ax2 = fig.add_subplot(1,1,1,projection='3d')
x,y = np.mgrid[:mydata.shape[0],:mydata.shape[1]]
ax2.plot_surface(x,y,mydata,cmap=pl.cm.jet,rstride=1,cstride=1,linewidth=0.,antia liased=False)
ax2.set_title('3D')
ax2.set_zlim3d(0,200)
pl.show()

使用包含一组图像的列表绘制3D图像,但是我收到此错误:

Traceback (most recent call last):
ax2.plot_surface(x,y,mydata,cmap=pl.cm.jet,rstride=1,cstride=1,linewidth=0.,antialiased=False)

File "/usr/lib/pymodules/python2.7/mpl_toolkits/mplot3d/axes3d.py", line 1553, in plot_surface 
X, Y, Z = np.broadcast_arrays(X, Y, Z)

File "/usr/lib/python2.7/dist-packages/numpy/lib/stride_tricks.py", line 100, in broadcast_arrays 
"incompatible dimensions on axis %r." % (axis,))

ValueError: shape mismatch: two or more arrays have incompatible dimensions on axis 2

任何人都可以帮我解决此错误或建议其他技术从包含2D图像的图像列表中创建3D图像吗?

1 个答案:

答案 0 :(得分:0)

我无法记住我发现的确切位置(这是另一个StackOverflow线程),但这是正常工作的代码 - 你的代码看起来像我的样本 - 只需更改文件名即可加载:

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import pylab as pl
from PIL import Image
import numpy as np
import pylab

img = Image.open('natureWallpaper.jpg').convert('L')
z   = np.asarray(img)
#slice notation: "start:stop:step" - here it's referring to the z matrix's x and y dimensions, get the whole of each
mydata = z[::1,::1]
fig = pl.figure(facecolor='w')
# subplot(nrows, ncols, plotnumber)
ax1 = fig.add_subplot(1,2,1)
# im = ax1.imshow(mydata,interpolation='nearest',cmap=pl.cm.jet)
im = ax1.imshow(mydata,interpolation='none',cmap=pl.cm.jet)
ax1.set_title('2D')

ax2 = fig.add_subplot(1,2,2,projection='3d')
x,y = np.mgrid[:mydata.shape[0],:mydata.shape[1]] ax2.plot_surface(x,y,mydata,cmap=pl.cm.jet,rstride=10,cstride=10,linewidth=0.antialiased=False)
ax2.set_title('3D')
ax2.set_zlim3d(0,255)
pl.show()
相关问题