Intel Realsense C/C++ 转 python (1)rs-hello-realsense 获取摄像头正中心对应的深度数据 get_distance()
https://dev.intelrealsense.com/docs/code-samples
Demonstrates the basics of connecting to a RealSense device and using depth data
演示連接到RealSense設(shè)備和使用深度數(shù)據(jù)的基礎(chǔ)知識(shí)
C/C++源碼:
代碼概述
首先,我們包括英特爾?實(shí)感?跨平臺(tái)API。
除高級(jí)功能外,所有功能都通過單個(gè)標(biāo)頭提供:
接下來,我們創(chuàng)建并啟動(dòng)RealSense管道。管道是控制攝像機(jī)枚舉和流式傳輸?shù)闹饕呒?jí)基本命令。
// Create a Pipeline - this serves as a top-level API for streaming and processing frames rs2::pipeline p;// Configure and start the pipeline p.start();一旦管道配置完成,我們就可以循環(huán)等待新幀。
RealSense攝像機(jī)通常提供多個(gè)視頻,運(yùn)動(dòng)或姿勢流。wait_for_frames功能將阻塞,直到來自各種已配置流的下一組相干幀為止。
// Block program until frames arrive rs2::frameset frames = p.wait_for_frames();要從深度數(shù)據(jù)流中獲取第一幀,可以使用get_depth_frame輔助函數(shù):
// Try to get a frame of a depth image rs2::depth_frame depth = frames.get_depth_frame();接下來,我們查詢默認(rèn)的深度框架尺寸(每個(gè)傳感器的深度尺寸可能有所不同):
// Get the depth frame's dimensions float width = depth.get_width(); float height = depth.get_height();要獲取特定像素(幀中心)的距離,可以使用get_distance函數(shù):
// Query the distance from the camera to the object in the center of the image float dist_to_center = depth.get_distance(width / 2, height / 2);剩下的唯一一件事就是將所得的距離打印到屏幕上:
// Print the distance std::cout << "The camera is facing an object " << dist_to_center << " meters away \r";python:
# Include RealSense Cross Platform API import pyrealsense2 as rs# Create a Pipeline - this serves as a top-level API for streaming and processing frames p = rs.pipeline()# Configure and start the pipeline p.start()# Block program until frames arrive frames = p.wait_for_frames()# Try to get a frame of a depth image depth = frames.get_depth_frame()# Get the depth frame's dimensions width = depth.get_width() height = depth.get_height()print(width, " ", height)# Query the distance from the camera to the object in the center of the image dist_to_center = depth.get_distance(int(width / 2), int(height / 2))# Print the distance print("The camera is facing an object ", dist_to_center, " meters away \r")# 640 480 # The camera is facing an object 65.53500366210938 meters away# 輸出不是0就是65.53500366210938,不知道啥情況 # 原因:usb2.0不行,換成usb3.0就能正常獲取深度了。注意不要太近,太近(<30cm)也不能獲取深度(值為0)。注:get_distance()函數(shù)有可能把窗口搞奔潰,不建議使用!!!!
與50位技術(shù)專家面對面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的Intel Realsense C/C++ 转 python (1)rs-hello-realsense 获取摄像头正中心对应的深度数据 get_distance()的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何查看电脑显卡参数? gpuZ
- 下一篇: Intel Realsense C/C+