Skip to content

Instantly share code, notes, and snippets.

@ochilab
Last active June 15, 2020 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ochilab/5d784f33fab2b7c04e4948992136a881 to your computer and use it in GitHub Desktop.
Save ochilab/5d784f33fab2b7c04e4948992136a881 to your computer and use it in GitHub Desktop.
RealsenseでRGBとデプスの映像を表示する
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment