首页 > 解决方案 > 如何使用python获取图像中每个点(像素)的高程

问题描述

我正在尝试使用 python 的图像处理来获取图像中每个像素的高度。我的第一次尝试是通过使用以下代码将图像转换为灰度并将 2d 图像转换为 3d 图像:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import imread
imageFile = 'D:\Books\Pav Man\PICS\pic (17) - Copy.png'
mat = imread(imageFile)

mat = mat[:,:,0] # get the first channel
#mat = mat - np.full_like(mat , mat.mean()) #Use this to get negative value
rows, cols = mat.shape
xv, yv = np.meshgrid(range(cols), range(rows)[::-1])

blurred = ndimage.gaussian_filter(mat, sigma=(5, 5), order=0)
fig = plt.figure(figsize=(6,6))
ax = fig.add_subplot(221)
ax.imshow(mat, cmap='gray')
ax = fig.add_subplot(222, projection='3d')
ax.elev= 75
ax.plot_surface(xv, yv, mat)
ax = fig.add_subplot(223)
ax.imshow(blurred, cmap='gray')
ax = fig.add_subplot(224, projection='3d')
ax.elev= 75
ax.plot_surface(xv, yv, blurred)
plt.show()

垫子包含每个像素的 x,y,z 值,x = 宽度坐标,y = 高度坐标,z = 灰度值,范围从 0 到 1,但不包括实际高程。

第二次尝试是使用以下链接中提到的 2 个图像的深度数据: https ://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_calib3d/py_depthmap/py_depthmap.html

但是没有明确的方法来估计或预测图像中点的高程。下图描述了我的意思:

点击这里显示图片

我的问题是如何获取图像中每个点的高程以创建地形剖面?

标签: pythonimageopencvmatplotlibimage-processing

解决方案


推荐阅读