LoginSignup
1
1

RealSense入門: カラーとデプス映像の取得方法

Last updated at Posted at 2023-12-31

Intel RealSenseは、デブスカメラの1つです。カラーカメラの映像とデプスカメラの映像を取得でき、これにより、3Dスキャンや物体検出などのタスクを実行できます。本記事は、Pythonを使用してRealSenseからカラーカメラとデプスカメラの映像を取得していきます。

必要な物
RealSense: Intel RealSenseカメラ(例:D415、D435)を用意してください。
RealSense SDK 2.0:動作確認用に Intelの公式サイトからRealSense SDK 2.0をインストールしてください。(なくてもOK)
Python: Pythonがインストールされていることを確認してください。
pyrealsense2: Python用RealSenseライブラリをインストールしてください。

pip install pyrealsense2

1: ライブラリのインポート

import pyrealsense2 as rs
import numpy as np
import cv2

2: ストリーミングの設定

RealSenseデバイスのストリーミングを設定します。カラーとデプスの両方のストリームを有効にします。

# ストリームの設定
pipeline = rs.pipeline()
config = rs.config()

# カラーストリームを設定
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)

# デプスストリームを設定
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)

# ストリーミング開始
pipeline.start(config)

3: フレームの取得と表示

カラーとデプスのデータを取得して、OpenCVを使用して表示します。

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

        # Numpy配列に変換
        color_image = np.asanyarray(color_frame.get_data())
        depth_image = np.asanyarray(depth_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.imshow('RealSense', images)

        # 'q'を押してウィンドウを閉じる
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

finally:
    # ストリーミング停止
    pipeline.stop()
    cv2.destroyAllWindows()

上記のコードは、RealSenseカメラからカラーカメラとデプスカメラの両方のデータをリアルタイムで取得し、それらを画面に表示します。'q'キーを押すと、コードの実行は終了します。

コード全文

import pyrealsense2 as rs
import numpy as np
import cv2

# ストリームの設定
pipeline = rs.pipeline()
config = rs.config()

# カラーストリームを設定
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)

# デプスストリームを設定
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)

# ストリーミング開始
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

        # Numpy配列に変換
        color_image = np.asanyarray(color_frame.get_data())
        depth_image = np.asanyarray(depth_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.imshow('RealSense', images)

        # 'q'を押してウィンドウを閉じる
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

finally:
    # ストリーミング停止
    pipeline.stop()
    cv2.destroyAllWindows()
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1