简体   繁体   中英

Python 3D isosurface from a set of 2D images

I am using this code:

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()

To plot a 3D image using a list containing a set of images, but I get this error:

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

Could anyone help me with this error or suggest some other technique to create a 3D image from a image list containing 2D images?

I can't remember where I found this exactly (it was another StackOverflow thread), but this is working code - yours looks like a sample of the one I have - just change out the filename to load:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM