ZED stereo camera开发入门教程(3)

来源:互联网 发布:淘宝招工 编辑:程序博客网 时间:2024/06/05 04:39

教程3:使用ZED进行深度感测

原教程及代码链接:https://github.com/wangjhit/zedStereoCamera_tutorials/tree/master/tutorial%203%20-%20depth%20sensing
本教程将介绍如何从ZED SDK获取深度。该程序将循环,直到50帧被抓取。我们假设您已经遵循以前的教程(打开ZED和图像捕获)。

先决条件

Windows 7 64位或更高版本,Ubuntu 16.04
ZED SDK及其依赖项(CUDA)

构建程序

构建Windows

  • 在源文件夹中创建一个“build”文件夹
  • 打开cmake-gui并选择源和构建文件夹
  • 生成Visual Studio Win64解决方案
  • 打开生成的解决方案并将配置更改为 Release
  • 构建解决方案

构建Linux

在示例目录中打开一个终端,并执行以下命令:

mkdir buildcd buildcmake ..make

代码概述

创建一个相机

与其他教程一样,我们创建,配置和打开ZED。我们将HD-HD模式的ZED设置为60fps,并在PERFORMANCE模式下实现深度。ZED SDK提供不同的深度模式:性能,介质,质量。有关更多信息,请参阅在线文档。
// Create a ZED cameraCamera zed;// Create configuration parametersInitParameters init_params;init_params.sdk_verbose = true; // Enable the verbose modeinit_params.depth_mode = DEPTH_MODE_PERFORMANCE; // Set the depth mode to performance (fastest)// Open the cameraERROR_CODE err = zed.open(init_params);if (err!=SUCCESS)  exit(-1);

注意:深度模式的默认参数为DEPTH_MODE_PERFORMANCE。实际上,没有必要在InitParameters中设置深度模式。

捕获数据

现在ZED打开了,我们可以捕获图像和深度。这里我们循环,直到我们成功捕获了50张图像。检索深度图与检索图像一样简单:

  • 我们创建一个Mat来存储深度图。
  • 我们调用retrieveMeasure()获取深度图。
// Capture 50 images and depth, then stopint i = 0;sl::Mat image, depth;while (i < 50) {    // Grab an image    if (zed.grab(runtime_parameters) == SUCCESS) {        // A new image is available if grab() returns SUCCESS        zed.retrieveImage(image, VIEW_LEFT); // Get the left image        zed.retrieveMeasure(depth, MEASURE_DEPTH); // Retrieve depth Mat. Depth is aligned on the left image        i++;    }}
现在我们已经检索了深度图,我们可能想要在特定像素处获得深度。在该示例中,我们提取图像中心点的距离(width / 2,height / 2)
// Get and print distance value in mm at the center of the image// We measure the distance camera - object using Euclidean distanceint x = image.getWidth() / 2;int y = image.getHeight() / 2;sl::float4 point_cloud_value;point_cloud.getValue(x, y, &point_cloud_value);float distance = sqrt(point_cloud_value.x*point_cloud_value.x + point_cloud_value.y*point_cloud_value.y + point_cloud_value.z*point_cloud_value.z);printf("Distance to Camera at (%d, %d): %f mm\n", x, y, distance);
一旦50帧被抓住,我们关闭相机。

// Close the camerazed.close();
您现在正在使用ZED作为深度传感器。您可以转到下一个教程,了解如何使用ZED作为位置跟踪器。