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 config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
39 
40 # Start streaming
41 profile = pipeline.start(config)
42 
43 # Getting the depth sensor's depth scale (see rs-align example for explanation)
44 depth_sensor = profile.get_device().first_depth_sensor()
45 depth_scale = depth_sensor.get_depth_scale()
46 print("Depth Scale is: " , depth_scale)
47 
48 # We will be removing the background of objects more than
49 # clipping_distance_in_meters meters away
50 clipping_distance_in_meters = 1 #1 meter
51 clipping_distance = clipping_distance_in_meters / depth_scale
52 
53 # Create an align object
54 # rs.align allows us to perform alignment of depth frames to others frames
55 # The "align_to" is the stream type to which we plan to align depth frames.
56 align_to = rs.stream.color
57 align = rs.align(align_to)
58 
59 # Streaming loop
60 try:
61  while True:
62  # Get frameset of color and depth
63  frames = pipeline.wait_for_frames()
64  # frames.get_depth_frame() is a 640x360 depth image
65 
66  # Align the depth frame to color frame
67  aligned_frames = align.process(frames)
68 
69  # Get aligned frames
70  aligned_depth_frame = aligned_frames.get_depth_frame() # aligned_depth_frame is a 640x480 depth image
71  color_frame = aligned_frames.get_color_frame()
72 
73  # Validate that both frames are valid
74  if not aligned_depth_frame or not color_frame:
75  continue
76 
77  depth_image = np.asanyarray(aligned_depth_frame.get_data())
78  color_image = np.asanyarray(color_frame.get_data())
79 
80  # Remove background - Set pixels further than clipping_distance to grey
81  grey_color = 153
82  depth_image_3d = np.dstack((depth_image,depth_image,depth_image)) #depth image is 1 channel, color is 3 channels
83  bg_removed = np.where((depth_image_3d > clipping_distance) | (depth_image_3d <= 0), grey_color, color_image)
84 
85  # Render images:
86  # depth align to color on left
87  # depth on right
88  depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)
89  images = np.hstack((bg_removed, depth_colormap))
90 
91  cv2.namedWindow('Align Example', cv2.WINDOW_NORMAL)
92  cv2.imshow('Align Example', images)
93  key = cv2.waitKey(1)
94  # Press esc or 'q' to close the image window
95  if key & 0xFF == ord('q') or key == 27:
96  cv2.destroyAllWindows()
97  break
98 finally:
99  pipeline.stop()
read_bag_example.str
str
Definition: read_bag_example.py:21
realdds::print
std::string print(dds_guid const &guid, dds_guid_prefix const &base_prefix, bool readable_name)
Definition: dds-guid.cpp:75
rs2::textual_icons::exit
static const textual_icon exit
Definition: device-model.h:222


librealsense2
Author(s): LibRealSense ROS Team
autogenerated on Fri Aug 2 2024 08:30:00