align-depth2color.py
Go to the documentation of this file.
1 ## License: Apache 2.0. See LICENSE file in root directory.
2 ## Copyright(c) 2017 Intel Corporation. All Rights Reserved.
3 
4 #####################################################
5 ## Align Depth to Color ##
6 #####################################################
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 config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
29 
30 if device_product_line == 'L500':
31  config.enable_stream(rs.stream.color, 960, 540, rs.format.bgr8, 30)
32 else:
33  config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
34 
35 # Start streaming
36 profile = pipeline.start(config)
37 
38 # Getting the depth sensor's depth scale (see rs-align example for explanation)
39 depth_sensor = profile.get_device().first_depth_sensor()
40 depth_scale = depth_sensor.get_depth_scale()
41 print("Depth Scale is: " , depth_scale)
42 
43 # We will be removing the background of objects more than
44 # clipping_distance_in_meters meters away
45 clipping_distance_in_meters = 1 #1 meter
46 clipping_distance = clipping_distance_in_meters / depth_scale
47 
48 # Create an align object
49 # rs.align allows us to perform alignment of depth frames to others frames
50 # The "align_to" is the stream type to which we plan to align depth frames.
51 align_to = rs.stream.color
52 align = rs.align(align_to)
53 
54 # Streaming loop
55 try:
56  while True:
57  # Get frameset of color and depth
58  frames = pipeline.wait_for_frames()
59  # frames.get_depth_frame() is a 640x360 depth image
60 
61  # Align the depth frame to color frame
62  aligned_frames = align.process(frames)
63 
64  # Get aligned frames
65  aligned_depth_frame = aligned_frames.get_depth_frame() # aligned_depth_frame is a 640x480 depth image
66  color_frame = aligned_frames.get_color_frame()
67 
68  # Validate that both frames are valid
69  if not aligned_depth_frame or not color_frame:
70  continue
71 
72  depth_image = np.asanyarray(aligned_depth_frame.get_data())
73  color_image = np.asanyarray(color_frame.get_data())
74 
75  # Remove background - Set pixels further than clipping_distance to grey
76  grey_color = 153
77  depth_image_3d = np.dstack((depth_image,depth_image,depth_image)) #depth image is 1 channel, color is 3 channels
78  bg_removed = np.where((depth_image_3d > clipping_distance) | (depth_image_3d <= 0), grey_color, color_image)
79 
80  # Render images:
81  # depth align to color on left
82  # depth on right
83  depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)
84  images = np.hstack((bg_removed, depth_colormap))
85 
86  cv2.namedWindow('Align Example', cv2.WINDOW_NORMAL)
87  cv2.imshow('Align Example', images)
88  key = cv2.waitKey(1)
89  # Press esc or 'q' to close the image window
90  if key & 0xFF == ord('q') or key == 27:
91  cv2.destroyAllWindows()
92  break
93 finally:
94  pipeline.stop()
static std::string print(const transformation &tf)


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:45:06