直接上代码
from matplotlib import pyplot as plot # 用来绘制图形
import numpy as np # 用来处理数据
from mpl_toolkits.mplot3d import Axes3D # 用来给出三维坐标系。
import cv2
def getZ(img, X, Y):
gray = img[X, Y]
return gray
img = cv2.imread("SoutheastUniversity.jpg", 0)
try:
img.shape
except:
print('Failed read image')
height, width = img.shape[:2]
figure = plot.figure()
# 画出三维坐标系:
axes = Axes3D(figure)
X = np.arange(0, height, 1)
Y = np.arange(0, width, 1)
# 生成二维的底部网格线:
X, Y = np.meshgrid(X, Y)
# Z = 3 * (X) ** 2 + 2 * (Y) ** 2 + 5
# 绘制曲面:
axes.plot_surface(X, Y, getZ(img, X, Y), cmap='rainbow')
# 显示图形:
plot.show()
原图
运行结果