Zed 捕获图像+测距
- 1. 导入相关库
- 2. 相机初始化设置
- 3. 获取中心点深度数据
- 4. 计算中心点深度值
- 5. 完整代码
- 5. 实验效果
此代码基于官方代码基础上进行改写,主要是获取zed相机深度画面中心点的深度值,为yolo测距打基础。
1. 导入相关库
import pyzed.sl as sl
import math
import numpy as np
import sys
import math
2. 相机初始化设置
zed = sl.Camera()
# 相机初始化设置init_params = sl.InitParameters()init_params.depth_mode = sl.DEPTH_MODE.ULTRA # 使用高质量模式init_params.coordinate_units = sl.UNIT.MILLIMETER # 单位设置为毫米
3. 获取中心点深度数据
if zed.grab(runtime_parameters) == sl.ERROR_CODE.SUCCESS: # 相机成功获取图象zed.retrieve_image(image, sl.VIEW.LEFT) # image:容器,sl.VIEW.LEFT:内容zed.retrieve_measure(depth, sl.MEASURE.DEPTH) # 深度值zed.retrieve_measure(point_cloud, sl.MEASURE.XYZRGBA) # 保留RGBA color的三维点云数据img = image.get_data() # 转换成图像数组,便于后续的显示或者储存dep_map = dep.get_data()# 获取图像的像素坐标x = round(image.get_width() / 2)y = round(image.get_height() / 2)err, point_cloud_value = point_cloud.get_value(x, y) # 将像素坐标转为三维坐标并存储
4. 计算中心点深度值
if math.isfinite(point_cloud_value[2]): # 判断坐标不为空和无限大distance = math.sqrt(point_cloud_value[0] * point_cloud_value[0] +point_cloud_value[1] * point_cloud_value[1] +point_cloud_value[2] * point_cloud_value[2]) # 计算距离print(f"Distance to Camera at {{{x};{y}}}: {distance}")
5. 完整代码
import os
import cv2
import pyzed.sl as sl
import numpy as np
import mathdef main():# 创建相机对象zed = sl.Camera()# 相机初始化设置init_params = sl.InitParameters()init_params.camera_resolution = sl.RESOLUTION.HD1080 # Use HD1080 video modeinit_params.camera_fps = 30 # fps可选:15、30、60、100# 打开相机status = zed.open(init_params)if status != sl.ERROR_CODE.SUCCESS: #Ensure the camera has opened succesfullyprint("Camera Open : "+repr(status)+". Exit program.")exit()# Create and set RuntimeParameters after opening the cameraruntime_parameters = sl.RuntimeParameters()image = sl.Mat() # 图像depth = sl.Mat() # 深度值dep = sl.Mat() # 深度图point_cloud = sl.Mat() # 点云i = 0while True:if zed.grab(runtime_parameters) == sl.ERROR_CODE.SUCCESS: # 相机成功获取图象zed.retrieve_image(image, sl.VIEW.LEFT) # image:容器,sl.VIEW.LEFT:内容zed.retrieve_measure(depth, sl.MEASURE.DEPTH) # 深度值zed.retrieve_image(dep, sl.VIEW.DEPTH) # 深度图zed.retrieve_measure(point_cloud, sl.MEASURE.XYZRGBA) # 保留RGBA color的三维点云数据img = image.get_data() # 转换成图像数组,便于后续的显示或者储存dep_map = dep.get_data()# 获取图像的像素坐标x = round(image.get_width() / 2)y = round(image.get_height() / 2)err, point_cloud_value = point_cloud.get_value(x, y) # 将像素坐标转为三维坐标并存储distance = math.sqrt(point_cloud_value[0] * point_cloud_value[0] +point_cloud_value[1] * point_cloud_value[1] +point_cloud_value[2] * point_cloud_value[2]) # 计算距离print(f"Distance to Camera at {{{x};{y}}}: {distance}")view = np.concatenate((cv2.resize(img,(640,360)),cv2.resize(dep_map,(640,360))),axis=1) # 原图和深度图拼接cv2.imshow("View", view)key = cv2.waitKey(1)if key & 0xFF == 27: # esc退出breakif key & 0xFF == ord('s'): # 图像保存savePath = os.path.join("./images", "V{:0>3d}.png".format(i)) # 注意根目录是否存在"./images"文件夹cv2.imwrite(savePath, view)i = i + 1# 关闭相机zed.close()if __name__ == "__main__":main()
5. 实验效果
深度图和原图拼接
计算画面中心点深度值