Python的:3D轮廓从2D图像 - pylab和contourf轮廓、图像、Python、pylab

由网友(「勃大精深」)分享简介:我有关于Python的(pylab),并绘制一个问题 - 我能够加载和显示图像(code以下加载图像的下方),但我不能,我明白了一个数组需要 pylab绘制本作中3D轮廓.contourf(X,Y,Z)虽然我不能确定如何从装载的图象数据实现这一目标。 I have a question regarding Pyth...

我有关于Python的(pylab),并绘制一个问题 - 我能够加载和显示图像(code以下加载图像的下方),但我不能,我明白了一个数组需要 pylab绘制本作中3D轮廓.contourf(X,Y,Z)虽然我不能确定如何从装载的图象数据实现这一目标。

I have a question regarding Python(pylab) and plotting - I'm able to load and display an image (code below loads the image below), but I'm unable to plot this as a contour in 3D, I understand an array is required for pylab.contourf(x,y,z) though I'm unsure how to achieve this from the loaded image data.

任何建议和帮助,请。我的code:

Any suggestions and assistance please. My code:

from PIL import Image
import pylab

fileName = "image1.png"
im = Image.open(fileName)
#pylab.contourf(im) # don't work - needs an array but how
pylab.axis('off')
pylab.imshow(im)
pylab.show()

推荐答案

确定,一些研究和简化code,关键是转换(L),即RGB为灰度,然后Ali_m的code ++工程

OK, some research and simplifying code, the key is convert('L'), i.e. rgb to greyscale, then Ali_m's code works:

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('40.jpg').convert('L')
z   = np.asarray(img)
mydata = z[::1,::1]
fig = pl.figure(facecolor='w')
ax1 = fig.add_subplot(1,2,1)
im = ax1.imshow(mydata,interpolation='nearest',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=1,cstride=1,linewidth=0.,antialiased=False)
ax2.set_title('3D')
ax2.set_zlim3d(0,100)
pl.show()

阅读全文

相关推荐

最新文章