Intel Realsense C/C++ 转 python (9)rs-multicam 多摄像头可视化窗体显示
https://dev.intelrealsense.com/docs/code-samples
The multicam sample demonstrates the ability to use the SDK for streaming and rendering multiple devices simultaneously.
多攝像頭示例演示了使用SDK來同時流式傳輸和渲染多個設備的能力。
C/C++源碼:
代碼概述
與任何SDK應用程序一樣,我們包括英特爾實感跨平臺API:
在此示例中,我們還將使用以下輔助庫example.hpp:
#include "example.hpp" // Include short list of convenience functions for renderingexamples.hpp 讓我們輕松地打開一個新窗口并準備要渲染的紋理。
我們使用的第一個對象是window,它將用于顯示所有攝像機的圖像。
// Create a simple OpenGL window for rendering: window app(1280, 960, "CPP Multi-Camera Example");本window類駐留在example.hpp,讓我們輕松地打開一個新的窗口,為渲染準備紋理。
接下來,我們定義示例中要使用的對象。
rs2::context ctx; // Create librealsense context for managing devicesrs2::colorizer colorizer; // Utility class to convert depth data RGBstd::vector pipelines;該rs2::context封裝封裝了所有設備和傳感器,并提供了一些其他功能。我們使用rs2::colorizer將深度數(shù)據(jù)轉換為RGB格式。
在示例中,我們使用多個rs2::pipeline對象,每個對象控制單個硬件設備的壽命。
該示例的流程從列出并激活所有連接的英特爾?實感?設備開始:
// Start a streaming pipe per each connected device for (auto&& dev : ctx.query_devices()) {rs2::pipeline pipe(ctx);rs2::config cfg;cfg.enable_device(dev.get_info(RS2_CAMERA_INFO_SERIAL_NUMBER));pipe.start(cfg);pipelines.emplace_back(pipe); }首先,我們rs2::pipeline為每個識別的設備分配對象。請注意,我們rs2::context在所有rs2::pipeline實例之間共享對象。
rs2::pipeline pipe(ctx);要將特定設備映射到新分配的管道,我們定義rs2::config對象,并為其分配設備的序列號。
然后,我們請求rs::pipeline開始流式傳輸并產生幀。
由于我們未指定明確的流請求,因此在內部將每個設備配置為運行針對該特定設備推薦的一組預定義流配置文件。
添加設備后,我們開始應用程序的主循環(huán):
while (app)在每個應用周期中,我們遍歷已注冊的設備并檢索所有可用的幀:
// Collect the new frames from all the connected devices std::vector new_frames; for (auto &&pipe : pipelines) {rs2::frameset fs;if (pipe.poll_for_frames(&fs)){for (rs2::frame& f : fs)new_frames.emplace_back(f);} }每rs::pipeline一個都針對為其分配的設備配置的所有流產生同步的幀集合。這些都包含在rs2::frameset對象中。
的rs2::frameset本身為一個包裝composite_frame,它可以保存比單個類型的幀的更多。
為了最大程度地減少對UI的影響,我們使用非阻塞幀輪詢方法:
if (pipe.poll_for_frames(&fs))為了簡化演示,我們將這些rs2::frameset容器分成單獨的幀集,并將它們存儲在標準C ++容器中,以備后用:
for (rs2::frame& f : fs)new_frames.emplace_back(f);深度數(shù)據(jù)以uint16_t類型傳送,無法直接渲染,因此我們rs2::colorizer將深度表示轉換為人類可讀的RGB貼圖:
// Convert the newly-arrived frames to render-friendly format for (const auto& frame : new_frames) {render_frames[frame.get_profile().unique_id()] = colorizer.process(frame); }最后發(fā)送收集的幀以更新openGl馬賽克:
app.show(render_frames);python:
# Include short list of convenience functions for rendering import pyrealsense2 as rs# 自己編的 20191009 import cv2 import numpy as np# Create a simple OpenGL window for rendering: # window app(1280, 960, "CPP Multi-Camera Example");# Create librealsense context for managing devices ctx = rs.context()# Utility class to convert depth data RGB # colorizer = rs.colorizer()# std::vector pipelines; # 創(chuàng)建存放pipeline對象的空列表: pipelines = []# Start a streaming pipe per each connected device for dev in ctx.query_devices():# print(dev)# 接了兩個D435攝像頭:# <pyrealsense2.device: Intel RealSense D435 (S/N: 827312071726)># <pyrealsense2.device: Intel RealSense D435 (S/N: 838212074152)>pipe = rs.pipeline(ctx)cfg = rs.config()cfg.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 15)cfg.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 15)cfg.enable_device(dev.get_info(rs.camera_info.serial_number))# print(dev.get_info)# < bound method PyCapsule.get_info of < pyrealsense2.device: Intel RealSense D435(S / N: 827312071726) >># < bound method PyCapsule.get_info of < pyrealsense2.device: Intel RealSense D435(S / N: 838212074152) >># print(dev.get_info(rs.camera_info.serial_number))# 接了兩個D435攝像頭,顯示:# 827312071726# 838212074152pipe.start(cfg)pipelines.append(pipe)# print(pipelines)# 接了兩個D435攝像頭,因為是在循環(huán)內,第一次打印了一個元素,第二次打印了兩個元素:# [ < pyrealsense2.pyrealsense2.pipeline object at 0x0000023DD3CE5D88 >]# [ < pyrealsense2.pyrealsense2.pipeline object at 0x0000023DD3CE5D88 >, < pyrealsense2.pyrealsense2.pipeline object at 0x0000023DC44EBA08 >]# reformed by Dontla at 20191009 while True:image_set = []for pipe in pipelines:frames = pipe.wait_for_frames()depth_frame = frames.get_depth_frame()color_frame = frames.get_color_frame()if not depth_frame or not color_frame:continuedepth_image = np.asanyarray(depth_frame.get_data())color_image = np.asanyarray(color_frame.get_data())depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)images = np.hstack((color_image, depth_colormap))image_set.append(images)image_stack = image_set[0]for images in image_set[1:]:image_stack = np.vstack((image_stack, images))cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)cv2.imshow('RealSense', image_stack)cv2.waitKey(1)''' # 老的,我要把它們全用自己的方式改寫了!20191009while True:# Collect the new frames from all the connected devices# std::vector new_frames;new_frames = []for pipe in pipelines:# rs2::frameset fs;# 到底是用frame還是composite_frame還是frame_queue?# fs = rs.frame()if pipe.poll_for_frames():fs = pipe.poll_for_frames()new_frames.append(fs)# 自己編的 20191009# 思路:遍歷new_frames,將復合幀拆分成深度幀和RGB幀,然后將他們堆疊起來顯示。for frames in new_frames:depth_frame = frames.get_depth_frame()color_frame = frames = frames.get_color_frame()depth_image = np.asanyarray(depth_frame.get_data())color_image = np.asanyarray(color_frame.get_data())depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)images = np.hstack((color_image, depth_colormap))cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)cv2.imshow('RealSense', images)cv2.waitKey(1)# for f in fs:# print("開始打印")# print(pipe)# print(f)# new_frames.append(f)# 每個pipe輸出一個幀地址,兩個輸出兩個,遍歷完后重新while True,new_frames清空。# print(new_frames)# # Convert the newly-arrived frames to render-friendly format# render_frames = []# for frame in new_frames:# # print(frame.get_profile().unique_id())# render_frames[frame.get_profile().unique_id()] = colorizer.process(frame)# print(render_frames) '''接兩個攝像頭,最后生成結果:
正常來說,無論多接幾個攝像頭,都會像上面那樣垂直疊加,因為沒有設置自適應屏幕,所以某些情況下畫面過多有可能會超出屏幕范圍之外而無法被肉眼正常看到。
總結
以上是生活随笔為你收集整理的Intel Realsense C/C++ 转 python (9)rs-multicam 多摄像头可视化窗体显示的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Intel Realsense C/C+
- 下一篇: C++中的 auto类型详解