当前位置: 首页 > news >正文

realsense应用--rs-distance(距离测量)

概述

本代码示例是C语言简易示例集的一部分,演示如何通过RealSense SDK的C语言接口快速集成摄像头访问功能到应用程序中。

该示例通过流式传输深度数据,并计算输出摄像头到图像中心物体的距离

主要演示功能:

使用C API获取深度数据流实时计算并输出图像中心点对应的物体距离展示基础深度数据交互模式

技术特性:

纯C语言接口实现中心点坐标自动计算:基于图像分辨率 (width/2, height/2)默认输出单位:米(可通过深度单位参数转换)

应用场景:
✔ 避障系统开发
✔ 物体距离检测
✔ 深度传感器功能验证

以下案例中,深度图WIDTH和HEIGHT的最大值是1280*720

// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2017 Intel Corporation. All Rights Reserved./* Include the librealsense C header files */
#include <librealsense2/rs.h>
#include <librealsense2/h/rs_pipeline.h>
#include <librealsense2/h/rs_option.h>
#include <librealsense2/h/rs_frame.h>
#include "example.h"#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                     These parameters are reconfigurable                                        //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define STREAM          RS2_STREAM_DEPTH  // rs2_stream is a types of data provided by RealSense device           //
#define FORMAT          RS2_FORMAT_Z16    // rs2_format identifies how binary data is encoded within a frame      //
#define WIDTH           640               // Defines the number of columns for each frame or zero for auto resolve//
#define HEIGHT          0                 // Defines the number of lines for each frame or zero for auto resolve  //
#define FPS             30                // Defines the rate of frames per second                                //
#define STREAM_INDEX    0                 // Defines the stream index, used for multiple streams of the same type //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////int main()
{rs2_error* e = 0;// Create a context object. This object owns the handles to all connected realsense devices.// The returned object should be released with rs2_delete_context(...)rs2_context* ctx = rs2_create_context(RS2_API_VERSION, &e);check_error(e);/* Get a list of all the connected devices. */// The returned object should be released with rs2_delete_device_list(...)rs2_device_list* device_list = rs2_query_devices(ctx, &e);check_error(e);int dev_count = rs2_get_device_count(device_list, &e);check_error(e);printf("There are %d connected RealSense devices.\n", dev_count);if (0 == dev_count)return EXIT_FAILURE;// Get the first connected device// The returned object should be released with rs2_delete_device(...)rs2_device* dev = rs2_create_device(device_list, 0, &e);check_error(e);print_device_info(dev);// Create a pipeline to configure, start and stop camera streaming// The returned object should be released with rs2_delete_pipeline(...)rs2_pipeline* pipeline =  rs2_create_pipeline(ctx, &e);check_error(e);// Create a config instance, used to specify hardware configuration// The retunred object should be released with rs2_delete_config(...)rs2_config* config = rs2_create_config(&e);check_error(e);// Request a specific configurationrs2_config_enable_stream(config, STREAM, STREAM_INDEX, WIDTH, HEIGHT, FORMAT, FPS, &e);check_error(e);// Start the pipeline streaming// The retunred object should be released with rs2_delete_pipeline_profile(...)rs2_pipeline_profile* pipeline_profile = rs2_pipeline_start_with_config(pipeline, config, &e);if (e){printf("The connected device doesn't support depth streaming!\n");exit(EXIT_FAILURE);}while (1){// This call waits until a new composite_frame is available// composite_frame holds a set of frames. It is used to prevent frame drops// The returned object should be released with rs2_release_frame(...)rs2_frame* frames = rs2_pipeline_wait_for_frames(pipeline, RS2_DEFAULT_TIMEOUT, &e);check_error(e);// Returns the number of frames embedded within the composite frameint num_of_frames = rs2_embedded_frames_count(frames, &e);check_error(e);int i;for (i = 0; i < num_of_frames; ++i){// The retunred object should be released with rs2_release_frame(...)rs2_frame* frame = rs2_extract_frame(frames, i, &e);check_error(e);// Check if the given frame can be extended to depth frame interface// Accept only depth frames and skip other framesif (0 == rs2_is_frame_extendable_to(frame, RS2_EXTENSION_DEPTH_FRAME, &e))continue;// Get the depth frame's dimensionsint width = rs2_get_frame_width(frame, &e);check_error(e);int height = rs2_get_frame_height(frame, &e);check_error(e);// Query the distance from the camera to the object in the center of the imagefloat dist_to_center = rs2_depth_frame_get_distance(frame, width / 2, height / 2, &e);check_error(e);// Print the distanceprintf("The camera is facing an object %.3f meters away.\n", dist_to_center);rs2_release_frame(frame);}rs2_release_frame(frames);}// Stop the pipeline streamingrs2_pipeline_stop(pipeline, &e);check_error(e);// Release resourcesrs2_delete_pipeline_profile(pipeline_profile);rs2_delete_config(config);rs2_delete_pipeline(pipeline);rs2_delete_device(dev);rs2_delete_device_list(device_list);rs2_delete_context(ctx);return EXIT_SUCCESS;
}
http://www.dtcms.com/a/282907.html

相关文章:

  • 中国旅行社协会在京召开“文旅人工智能应用研讨会”,助力文旅创新发展
  • 分块(chunked) vs 滑动窗口(windowed)
  • 【25-cv-07929】Keith携Lisa Audit向日葵画作发案
  • 性能测试过程中监控linux服务器资源情况
  • c语言常用的字符串函数简介概括
  • 【基于飞浆训练车牌识别模型】
  • 2.库操作
  • cookie和session、favicon.ico
  • 管家婆辉煌ERP登录报错:HTTP 错误 404.2
  • 嵌入式下快速傅里叶变换(FFT)C语言库kissfft
  • 图机器学习(9)——图正则化算法
  • 2. nth-child 和 nth-of-type 区别
  • xss-labs通关
  • k8s之持久化存储流程
  • 小架构step系列16:代码文档
  • 深度点评:2025 年机床钣金加工 TOP10 终极点评
  • 批量文件重命名工具 香菇重命名v0.2
  • Linux运维新手的修炼手扎之第21天
  • 调试bug记录
  • 如何用山海鲸轻松构建3D智慧大屏?
  • 【开源.NET】一个 .NET 开源美观、灵活易用、功能强大的图表库
  • 3DGS之COLMAP
  • 能行为监测算法:低成本下的高效管理
  • LIN通信协议入门
  • AI学习笔记三十一:YOLOv8 C++编译测试(OpenVINO)
  • 构建足球实时比分APP:REST API与WebSocket接入方案详解
  • PandaWiki与GitBook深度对比:AI时代的知识管理工具,选谁好?
  • 自动控制原理知识地图:舵轮、路径与导航图
  • 经典排序算法之归并排序(Merge Sort)
  • Linux内核IPv4路由查找:LPC-Trie算法的深度实践