import pyrealsense2 as rs import numpy as np import cv2 conf = rs.config() width = 640 height = 480 pipe = rs.pipeline() profile = pipe.start(conf) # 普通の描画 conf.enable_stream(rs.stream.depth, width, height, rs.format.z16, 30) conf.enable_stream(rs.stream.color, width, height, rs.format.bgr8, 30) # 深度センサー補正のための数値 depth_sensor = profile.get_device().first_depth_sensor() depth_scale = depth_sensor.get_depth_scale() print("Depth Scale is: " , depth_scale) # スケール値と設定した値を元に切り取り距離を確定 clipping_distance_in_meters = 1 #1 meter clipping_distance = clipping_distance_in_meters / depth_scale # 深度フレームの情報を色画像フレームに適用する処理 align_to = rs.stream.color align = rs.align(align_to) #深度センサーの背景とする値(0-254) grey_color = 153 while True: frames = pipe.wait_for_frames() aligned_frames = align.process(frames) aligned_depth_frame = aligned_frames.get_depth_frame() color_frame = aligned_frames.get_color_frame() # フレームが来るまでお行儀よくまつ if not aligned_depth_frame or not color_frame: continue depth_image = np.asanyarray(aligned_depth_frame.get_data()) color_image = np.asanyarray(color_frame.get_data()) # 設定値をもとに背景を消す depth_image_3d = np.dstack((depth_image,depth_image,depth_image)) #depth image is 1 channel, color is 3 channels bg_removed = np.where((depth_image_3d > clipping_distance) | (depth_image_3d <= 0), grey_color, color_image) cv2.imshow('RS',bg_removed) #ESC keyS if cv2.waitKey(1) & 0xff == 27: cv2.destroyAllWindows() break
動かすと、こんな塩梅に