opencv_viewer_example.py
Go to the documentation of this file.
1 ## License: Apache 2.0. See LICENSE file in root directory.
2 ## Copyright(c) 2015-2017 Intel Corporation. All Rights Reserved.
3 
4 ###############################################
5 ## Open CV and Numpy integration ##
6 ###############################################
7 
8 import pyrealsense2 as rs
9 import numpy as np
10 import cv2
11 
12 # Configure depth and color streams
13 pipeline = rs.pipeline()
14 config = rs.config()
15 
16 # Get device product line for setting a supporting resolution
17 pipeline_wrapper = rs.pipeline_wrapper(pipeline)
18 pipeline_profile = config.resolve(pipeline_wrapper)
19 device = pipeline_profile.get_device()
20 device_product_line = str(device.get_info(rs.camera_info.product_line))
21 
22 config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
23 
24 if device_product_line == 'L500':
25  config.enable_stream(rs.stream.color, 960, 540, rs.format.bgr8, 30)
26 else:
27  config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
28 
29 # Start streaming
30 pipeline.start(config)
31 
32 try:
33  while True:
34 
35  # Wait for a coherent pair of frames: depth and color
36  frames = pipeline.wait_for_frames()
37  depth_frame = frames.get_depth_frame()
38  color_frame = frames.get_color_frame()
39  if not depth_frame or not color_frame:
40  continue
41 
42  # Convert images to numpy arrays
43  depth_image = np.asanyarray(depth_frame.get_data())
44  color_image = np.asanyarray(color_frame.get_data())
45 
46  # Apply colormap on depth image (image must be converted to 8-bit per pixel first)
47  depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)
48 
49  depth_colormap_dim = depth_colormap.shape
50  color_colormap_dim = color_image.shape
51 
52  # If depth and color resolutions are different, resize color image to match depth image for display
53  if depth_colormap_dim != color_colormap_dim:
54  resized_color_image = cv2.resize(color_image, dsize=(depth_colormap_dim[1], depth_colormap_dim[0]), interpolation=cv2.INTER_AREA)
55  images = np.hstack((resized_color_image, depth_colormap))
56  else:
57  images = np.hstack((color_image, depth_colormap))
58 
59  # Show images
60  cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)
61  cv2.imshow('RealSense', images)
62  cv2.waitKey(1)
63 
64 finally:
65 
66  # Stop streaming
67  pipeline.stop()


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