python三维方式显示灰度图像(已验证)

直接上代码

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

原图

运行结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值