align-depth2color.py
Go to the documentation of this file.
1 
3 
4 
7 
8 # First import the library
9 import pyrealsense2 as rs
10 # Import Numpy for easy array manipulation
11 import numpy as np
12 # Import OpenCV for easy image rendering
13 import cv2
14 
15 # Create a pipeline
16 pipeline = rs.pipeline()
17 
18 # Create a config and configure the pipeline to stream
19 # different resolutions of color and depth streams
20 config = rs.config()
21 
22 # Get device product line for setting a supporting resolution
23 pipeline_wrapper = rs.pipeline_wrapper(pipeline)
24 pipeline_profile = config.resolve(pipeline_wrapper)
25 device = pipeline_profile.get_device()
26 device_product_line = str(device.get_info(rs.camera_info.product_line))
27 
28 found_rgb = False
29 for s in device.sensors:
30  if s.get_info(rs.camera_info.name) == 'RGB Camera':
31  found_rgb = True
32  break
33 if not found_rgb:
34  print("The demo requires Depth camera with Color sensor")
35  exit(0)
36 
37 config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
38 
39 if device_product_line == 'L500':
40  config.enable_stream(rs.stream.color, 960, 540, rs.format.bgr8, 30)
41 else:
42  config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
43 
44 # Start streaming
45 profile = pipeline.start(config)
46 
47 # Getting the depth sensor's depth scale (see rs-align example for explanation)
48 depth_sensor = profile.get_device().first_depth_sensor()
49 depth_scale = depth_sensor.get_depth_scale()
50 print("Depth Scale is: " , depth_scale)
51 
52 # We will be removing the background of objects more than
53 # clipping_distance_in_meters meters away
54 clipping_distance_in_meters = 1 #1 meter
55 clipping_distance = clipping_distance_in_meters / depth_scale
56 
57 # Create an align object
58 # rs.align allows us to perform alignment of depth frames to others frames
59 # The "align_to" is the stream type to which we plan to align depth frames.
60 align_to = rs.stream.color
61 align = rs.align(align_to)
62 
63 # Streaming loop
64 try:
65  while True:
66  # Get frameset of color and depth
67  frames = pipeline.wait_for_frames()
68  # frames.get_depth_frame() is a 640x360 depth image
69 
70  # Align the depth frame to color frame
71  aligned_frames = align.process(frames)
72 
73  # Get aligned frames
74  aligned_depth_frame = aligned_frames.get_depth_frame() # aligned_depth_frame is a 640x480 depth image
75  color_frame = aligned_frames.get_color_frame()
76 
77  # Validate that both frames are valid
78  if not aligned_depth_frame or not color_frame:
79  continue
80 
81  depth_image = np.asanyarray(aligned_depth_frame.get_data())
82  color_image = np.asanyarray(color_frame.get_data())
83 
84  # Remove background - Set pixels further than clipping_distance to grey
85  grey_color = 153
86  depth_image_3d = np.dstack((depth_image,depth_image,depth_image)) #depth image is 1 channel, color is 3 channels
87  bg_removed = np.where((depth_image_3d > clipping_distance) | (depth_image_3d <= 0), grey_color, color_image)
88 
89  # Render images:
90  # depth align to color on left
91  # depth on right
92  depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)
93  images = np.hstack((bg_removed, depth_colormap))
94 
95  cv2.namedWindow('Align Example', cv2.WINDOW_NORMAL)
96  cv2.imshow('Align Example', images)
97  key = cv2.waitKey(1)
98  # Press esc or 'q' to close the image window
99  if key & 0xFF == ord('q') or key == 27:
100  cv2.destroyAllWindows()
101  break
102 finally:
103  pipeline.stop()
static std::string print(const transformation &tf)
static const textual_icon exit
Definition: model-views.h:255


librealsense2
Author(s): LibRealSense ROS Team
autogenerated on Thu Dec 22 2022 03:41:41