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來(lái)同時(shí)流式傳輸和渲染多個(gè)設(shè)備的能力。
C/C++源碼:
代碼概述
與任何SDK應(yīng)用程序一樣,我們包括英特爾實(shí)感跨平臺(tái)API:
在此示例中,我們還將使用以下輔助庫(kù)example.hpp:
#include "example.hpp" // Include short list of convenience functions for renderingexamples.hpp 讓我們輕松地打開(kāi)一個(gè)新窗口并準(zhǔn)備要渲染的紋理。
我們使用的第一個(gè)對(duì)象是window,它將用于顯示所有攝像機(jī)的圖像。
// Create a simple OpenGL window for rendering: window app(1280, 960, "CPP Multi-Camera Example");本window類(lèi)駐留在example.hpp,讓我們輕松地打開(kāi)一個(gè)新的窗口,為渲染準(zhǔn)備紋理。
接下來(lái),我們定義示例中要使用的對(duì)象。
rs2::context ctx; // Create librealsense context for managing devicesrs2::colorizer colorizer; // Utility class to convert depth data RGBstd::vector pipelines;該rs2::context封裝封裝了所有設(shè)備和傳感器,并提供了一些其他功能。我們使用rs2::colorizer將深度數(shù)據(jù)轉(zhuǎn)換為RGB格式。
在示例中,我們使用多個(gè)rs2::pipeline對(duì)象,每個(gè)對(duì)象控制單個(gè)硬件設(shè)備的壽命。
該示例的流程從列出并激活所有連接的英特爾?實(shí)感?設(shè)備開(kāi)始:
// 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); }首先,我們r(jià)s2::pipeline為每個(gè)識(shí)別的設(shè)備分配對(duì)象。請(qǐng)注意,我們r(jià)s2::context在所有rs2::pipeline實(shí)例之間共享對(duì)象。
rs2::pipeline pipe(ctx);要將特定設(shè)備映射到新分配的管道,我們定義rs2::config對(duì)象,并為其分配設(shè)備的序列號(hào)。
然后,我們請(qǐng)求rs::pipeline開(kāi)始流式傳輸并產(chǎn)生幀。
由于我們未指定明確的流請(qǐng)求,因此在內(nèi)部將每個(gè)設(shè)備配置為運(yùn)行針對(duì)該特定設(shè)備推薦的一組預(yù)定義流配置文件。
添加設(shè)備后,我們開(kāi)始應(yīng)用程序的主循環(huán):
while (app)在每個(gè)應(yīng)用周期中,我們遍歷已注冊(cè)的設(shè)備并檢索所有可用的幀:
// 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一個(gè)都針對(duì)為其分配的設(shè)備配置的所有流產(chǎn)生同步的幀集合。這些都包含在rs2::frameset對(duì)象中。
的rs2::frameset本身為一個(gè)包裝composite_frame,它可以保存比單個(gè)類(lèi)型的幀的更多。
為了最大程度地減少對(duì)UI的影響,我們使用非阻塞幀輪詢(xún)方法:
if (pipe.poll_for_frames(&fs))為了簡(jiǎn)化演示,我們將這些rs2::frameset容器分成單獨(dú)的幀集,并將它們存儲(chǔ)在標(biāo)準(zhǔn)C ++容器中,以備后用:
for (rs2::frame& f : fs)new_frames.emplace_back(f);深度數(shù)據(jù)以u(píng)int16_t類(lèi)型傳送,無(wú)法直接渲染,因此我們r(jià)s2::colorizer將深度表示轉(zhuǎn)換為人類(lèi)可讀的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對(duì)象的空列表: pipelines = []# Start a streaming pipe per each connected device for dev in ctx.query_devices():# print(dev)# 接了兩個(gè)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))# 接了兩個(gè)D435攝像頭,顯示:# 827312071726# 838212074152pipe.start(cfg)pipelines.append(pipe)# print(pipelines)# 接了兩個(gè)D435攝像頭,因?yàn)槭窃谘h(huán)內(nèi),第一次打印了一個(gè)元素,第二次打印了兩個(gè)元素:# [ < 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)''' # 老的,我要把它們?nèi)米约旱姆绞礁膶?xiě)了!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,將復(fù)合幀拆分成深度幀和RGB幀,然后將他們堆疊起來(lái)顯示。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("開(kāi)始打印")# print(pipe)# print(f)# new_frames.append(f)# 每個(gè)pipe輸出一個(gè)幀地址,兩個(gè)輸出兩個(gè),遍歷完后重新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) '''接兩個(gè)攝像頭,最后生成結(jié)果:
正常來(lái)說(shuō),無(wú)論多接幾個(gè)攝像頭,都會(huì)像上面那樣垂直疊加,因?yàn)闆](méi)有設(shè)置自適應(yīng)屏幕,所以某些情況下畫(huà)面過(guò)多有可能會(huì)超出屏幕范圍之外而無(wú)法被肉眼正常看到。
總結(jié)
以上是生活随笔為你收集整理的Intel Realsense C/C++ 转 python (9)rs-multicam 多摄像头可视化窗体显示的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Intel Realsense C/C+
- 下一篇: python 列表,元祖,字典的区别