通过Depth image计算被测物的深度信息

最近在玩Kinect2,我们可以通过Kinect2采集到深度图像,如何通过深度计算出被测物的深度信息呢?废话少说,直接放代码:

import numpy as np
import cv2

img = cv2.imread("depth.png")
cv2.namedWindow("Image")

rows, cols, channels = img.shape
print(img.shape)
print("row:", rows, "cols:", cols, "channels:", channels)

# roi=img[0:rows,0:cols]   y  x       通过画图工具粗略获取roi像素坐标 rows:对应图像高H y  cols:对应图像W x
roi_img = img[237:264, 237:280, 0]/255*4.5    # 单位 m

avg = np.average(roi_img)
min = np.min(roi_img)
max = np.max(roi_img)

avg = round(avg, 4)   # round函数将W保留四位小数
min = round(min, 4)
max = round(max, 4)

print("avg", avg)
print("min", min)
print("max", max)

text = "avg:" + str(avg) + " " + "min:" + str(min) + " " + "max:" + str(max)
cv2.putText(img, text, (5, 50), cv2.FONT_HERSHEY_PLAIN, 2.0, (0, 0, 255), 2)
cv2.imshow("Image", img)
cv2.imwrite("result_img.png", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

你可能感兴趣的:(Kinect2,Python)