realsense之将深度与颜色对齐

## License: Apache 2.0. See LICENSE file in root directory.
## Copyright(c) 2017 Intel Corporation. All Rights Reserved.

#####################################################
##              将深度与颜色对齐               ##
#####################################################

#首先导入库
import pyrealsense2 as rs
#导入Numpy以便于数组操作
import numpy as np
#导入OpenCV以便于图像渲染
import cv2

#创建一个管道
pipeline = rs.pipeline()

#Create a config并配置要流​​式传输的管道
#颜色和深度流的不同分辨率
config = rs.config()
config.enable_stream(rs.stream.depth, 640, 360, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)

#开始流式传输
profile = pipeline.start(config)

#获取深度传感器的深度标尺(参见rs-align示例进行说明)
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 meters away
clipping_distance_in_meters = 1 #1 meter
clipping_distance = clipping_distance_in_meters / depth_scale

#创建对齐对象
#rs.align允许我们执行深度帧与其他帧的对齐
#“align_to”是我们计划对齐深度帧的流类型。
align_to = rs.stream.color
align = rs.align(align_to)

#Streaming循环
try:
    while True:
        #获取颜色和深度的框架集
        frames = pipeline.wait_for_frames()
        #frames.get_depth_frame()是640x360深度图像
        
        #将深度框与颜色框对齐
        aligned_frames = align.process(frames)
        
        #获取对齐的帧
        aligned_depth_frame = aligned_frames.get_depth_frame()#aligned_depth_frame是640x480深度图像
        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())
        
        #remove background - 将clips_distance以外的像素设置为灰色
        grey_color = 153
        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)
        
        #渲染图像
        depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)
        images = np.hstack((bg_removed, depth_colormap))
        cv2.namedWindow('Align Example', cv2.WINDOW_AUTOSIZE)
        cv2.imshow('Align Example', images)
        key = cv2.waitKey(1)
        #按esc或'q'关闭图像窗口
        if key & 0xFF == ord('q') or key == 27:
            cv2.destroyAllWindows()
            break
finally:
    pipeline.stop()

你可能感兴趣的:(Intel,Realsense,D435)