引言
关于ZED相机的安装和配置可以先参考【ZED】从零开始使用ZED相机(一):windows下的安装配置与测试,接下来开始二次开发ZED相机。 阅读ZED API文档,选择自己使用的语言,学会相机的基本调用函数 笔者建议:安装包的samples与官方文档配合使用效果更加 必备导入库:(后面涉及的代码都要导入这两个库)
import pyzed.sl as sl
import cv2
ZED开发目录:
(1)打开相机,终端打印相机的ZED相机的基本信息
(2)捕获图像(左、右、深度图等)+ 获取计算的数据(视差、深度、3D数据等)
下面详细展开
1 hello_zed(初见ZED)
import pyzed.sl as sl
import cv2
def hello_zed():
zed = sl.Camera()
init_params = sl.InitParameters()
init_params.sdk_verbose = False
err = zed.open(init_params)
if err != sl.ERROR_CODE.SUCCESS:
exit(1)
zed_info = zed.get_camera_information()
print('相机序列号:%s' % zed_info.serial_number)
print('相机型号:%s' % zed_info.camera_model)
print('相机分辨率: width:%s, height:%s' % (zed_info.camera_resolution.width, zed_info.camera_resolution.height))
print('相机FPS:%s' % zed_info.camera_fps)
print('相机外部参数:')
print('相机旋转矩阵R:%s' % zed_info.calibration_parameters.R)
print('相机变换矩阵T:%s' % zed_info.calibration_parameters.T)
print('相机基距:%s' % zed_info.calibration_parameters.get_camera_baseline())
print('初始化参数:')
zed_init = zed.get_init_parameters()
print('相机分辨率:%s' % (zed_init.camera_resolution))
print('深度最小:%s' % (zed_init.depth_minimum_distance))
print('深度最大:%s' % (zed_init.depth_maximum_distance))
zed.close()
if __name__ == "__main__":
hello_zed()
2 image_capture(捕获图像)
import pyzed.sl as sl
import cv2
import os
def image_capture():
zed = sl.Camera()
init_params = sl.InitParameters()
init_params.camera_resolution = sl.RESOLUTION.HD1080
init_params.camera_fps = 30
err = zed.open(init_params)
if err != sl.ERROR_CODE.SUCCESS:
exit(1)
runtime_parameters = sl.RuntimeParameters()
runtime_parameters.sensing_mode = sl.SENSING_MODE.STANDARD
i = 0
image = sl.Mat()
disparity = sl.Mat()
dep = sl.Mat()
depth = sl.Mat()
point_cloud = sl.Mat()
resolution = zed.get_camera_information().camera_resolution
w, h = resolution.width , resolution.height
x,y = int(w/2),int(h/2)
while True:
if zed.grab(runtime_parameters) == sl.ERROR_CODE.SUCCESS:
timestamp = zed.get_timestamp(sl.TIME_REFERENCE.CURRENT)
zed.retrieve_image(image, sl.VIEW.LEFT)
img = image.get_data()
zed.retrieve_measure(disparity,sl.MEASURE.DISPARITY,sl.MEM.CPU)
dis_map = disparity.get_data()
zed.retrieve_measure(depth,sl.MEASURE.DEPTH,sl.MEM.CPU)
zed.retrieve_image(dep,sl.VIEW.DEPTH)
depth_map = depth.get_data()
dep_map = dep.get_data()
zed.retrieve_measure(point_cloud,sl.MEASURE.XYZBGRA,sl.MEM.CPU)
point_map = point_cloud.get_data()
print('时间点',timestamp.get_seconds(),'中心点视差值',dis_map[x,y],'中心点深度值',depth_map[x,y],'中心点云数据',point_map[x,y])
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:
break
if key & 0xFF == ord('s'):
savePath = os.path.join("./images", "V{:0>3d}.png".format(i))
cv2.imwrite(savePath, view)
i = i + 1
zed.close()
视图显示结果如下:笔者通过设置按键“s”进行想要图像的保存,需要注意的是保存路径需要先创建好
终端打印如下:
代码中涉及的可选参数(点击关键字可进入官方文档链接)
-
分辨率选择 -
可捕获的视图 -
计算获得的视图
|