import numpy as np
import cv2
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# 读取灰度图像
img_path = "C:/Users/z/Desktop/image/1.png" # 替换为你的图像文件路径
gray_img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
# 获取图像的高度和宽度
height, width = gray_img.shape
# 创建X和Y坐标网格
x = np.arange(0, width, 1)
y = np.arange(0, height, 1)
x, y = np.meshgrid(x, y)
# 将灰度值作为Z轴的高度
z = gray_img
# 创建三维图像
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# 绘制表面图
surf = ax.plot_surface(x, y, z, cmap='gray') # 使用灰度颜色映射
# 设置坐标轴的标签
ax.set_xlabel('X Coordinate')
ax.set_ylabel('Y Coordinate')
ax.set_zlabel('Intensity')
# 添加右侧的色卡条
fig.colorbar(surf, shrink=0.5, aspect=5)
# 显示图像
plt.show()
opencv-python:将2D灰度图像转换成3D图像
最新推荐文章于 2025-05-23 16:25:55 发布