Loading [MathJax]/extensions/tex2jax.js

2020-03-04

Python: RealsenseのRGBとデプスのカメラ映像をOpenCVを利用して表示する

おちラボではMicrosoft Kinectを利用したソフトウェア開発をしていますが、御存知の通りKinectは要求するマシンスペックが高いので、なかなか使いづらいというのがあります。単純にデプスカメラとして使うなら、IntelのRealSenseがオススメです。

SDKとライブラリのインストール
リンク先を見てもらえばわかりますが(特にpyrealsense2の方)、Windows環境では何も気にすることなくSDKをとってきて、ライブラリはpipでインストールできます。Macの場合、いろいろ手間がかかりますね。
なおSDKについては、旧ライブラリでは各種認識系のサンプルプログラムが掲載されていたようですが、2.0になってからはないようです。おそらく、認識系のやつはサードパーティの優秀なライブラリを使ってねということなんでしょう(OpenPoseとかOpenFaceとか)。

ソースコードとポイント
1. configの設定
ここでどのストリームを読み込むのかを設定します。解像度やフレームレートなどを決めますが、それ以外のパラメーターについてもconfigで設定するんだと思います。

2.開始後はループ処理で
pipelineクラスを呼び出してstart呼び出し後、ループでフレームを呼び出します。

3.フレームの変換
numpyのasanyarrayで配列変換。depthフレームの場合は、CV2.applyColorMapでカラーマップに変換

あとの処理は通常のOpenCVプログラミングの世界、、ということで思った以上に簡単です。C#でKinectのプログラムを書いたときは、もっと面倒だったような気がしますが。。。。
以下にサンプルを載せます。クリックで全表示です。

import pyrealsense2 as rs
import numpy as np
import cv2
# Configulation of Realsense
config = rs.config()
#The case of reading from bag gile
config.enable_device_from_file('xxxx.bag')
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
# start
pipeline = rs.pipeline()
pipeline.start(config)
try:
while True:
frames = pipeline.wait_for_frames()
color_frame = frames.get_color_frame()
depth_frame = frames.get_depth_frame()
if not color_frame or not depth_frame:
continue
# translation to OpenCV Format
color_image = np.asanyarray(color_frame.get_data())
depth_image = np.asanyarray(depth_frame.get_data())
# depth image to Colormap
depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.08), cv2.COLORMAP_JET)
# Show Color images
cv2.namedWindow('color_image', cv2.WINDOW_AUTOSIZE)
cv2.imshow('color_image', color_image)
# Show Depth images
cv2.namedWindow('depth_image', cv2.WINDOW_AUTOSIZE)
cv2.imshow('depth_image', depth_colormap)
#enter q key to exit
key = cv2.waitKey(1) & 0xff
if key == ord("q"):
break
#If it does not work, try below code
#cv2.waitKey(1)
finally:
pipeline.stop()


0 件のコメント:

コメントを投稿