教程3:使用ZED进行深度感测
本教程将介绍如何从ZED SDK获取深度。该程序将循环,直到50帧被抓取。我们假设您已经遵循以前的教程(打开ZED和图像捕获)。先决条件
Windows 7 64位或更高版本,Ubuntu 16.04ZED SDK及其依赖项(CUDA)
构建程序
构建Windows
- 在源文件夹中创建一个“build”文件夹
- 打开cmake-gui并选择源和构建文件夹
- 生成Visual Studio Win64解决方案
- 打开生成的解决方案并将配置更改为 Release
- 构建解决方案
构建Linux
在示例目录中打开一个终端,并执行以下命令:
mkdir build
cd build
cmake ..
make
代码概述
创建一个相机
与其他教程一样,我们创建,配置和打开ZED。我们将HD-HD模式的ZED设置为60fps,并在PERFORMANCE模式下实现深度。ZED SDK提供不同的深度模式:性能,介质,质量。有关更多信息,请参阅在线文档。// Create a ZED camera
Camera zed;
// Create configuration parameters
InitParameters init_params;
init_params.sdk_verbose = true; // Enable the verbose mode
init_params.depth_mode = DEPTH_MODE_PERFORMANCE; // Set the depth mode to performance (fastest)
// Open the camera
ERROR_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 stop
int 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 distance
int 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 camera
zed.close();
您现在正在使用ZED作为深度传感器。您可以转到下一个教程,了解如何使用ZED作为位置跟踪器。