文章目录

  • ​​对齐深度图和RGB图并可视化深度图​​
  • ​​获取某点的深度距离​​

本文采用的相机是Intel Realsense D435i。

对齐深度图和RGB图并可视化深度图

需要注意的是,采用该方法实现后,程序执行的帧率只有3帧/s,所以不推荐在实际使用该方法进行可视化。

​depth_img_show.py​

import pyrealsense2 as rs
import numpy as np
import cv2

def show_colorizer_depth_img():
colorizer = rs.colorizer()
hole_filling = rs.hole_filling_filter()
filled_depth = hole_filling.process(depth_frame)
colorized_depth = np.asanyarray(colorizer.colorize(filled_depth).get_data())
cv2.imshow('filled depth',colorized_depth)


if __name__ == "__main__":
# Configure depth and color streams
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
# Start streaming
pipeline.start(config)
#深度图像向彩色对齐
align_to_color=rs.align(rs.stream.color)

try:
while True:
# Wait for a coherent pair of frames: depth and color
frames = pipeline.wait_for_frames()

frames = align_to_color.process(frames)

depth_frame = frames.get_depth_frame()
color_frame = frames.get_color_frame()
if not depth_frame or not color_frame:
continue
# Convert images to numpy arrays
depth_image = np.asanyarray(depth_frame.get_data())
color_image = np.asanyarray(color_frame.get_data())

show_colorizer_depth_img()

# Press esc or 'q' to close the image window
key = cv2.waitKey(1)
if key & 0xFF == ord('q') or key == 27:
cv2.destroyAllWindows()
break
finally:
# Stop streaming
pipeline.stop()

获取某点的深度距离

该方法可以用来探测目标物体的距离。
​​​depth_measure.py​

import cv2
import numpy as np
import pyrealsense2 as rs
import time

def get_center_depth(depth):
# Get the depth frame's dimensions
width = depth.get_width()
height = depth.get_height()

center_x = int(width / 2)
center_y = int(height / 2)

print(width, " ", height)
dis_center = round(depth.get_distance(center_x, center_y)*100, 2)
print("The camera is facing an object ", dis_center, " cm away.")
return dis_center, (center_x, center_y)

if __name__ == '__main__':
# Configure depth and color streams
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
# Configure and start the pipeline
pipeline.start(config)

while True:
start = time.time()
# Block program until frames arrive
frames = pipeline.wait_for_frames()

# RGB image
color_frame = frames.get_color_frame()
color_image = np.asanyarray(color_frame.get_data())
# depth image
depth = frames.get_depth_frame()
dis_center, center_coordinate = get_center_depth(depth)

print("color_image:", color_image.shape)
cv2.circle(color_image, center_coordinate, 5, (0,0,255), 0)
cv2.imshow("color_image", color_image)

print("FPS:", 1/(time.time()-start), "/s")

# press Esc to stop
k = cv2.waitKey(5) & 0xFF
if k == 27:
break

cv2.destroyAllWindows()

参考文章:

  • ​​Intel Realsense C/C++ 转 python (1)rs-hello-realsense 获取摄像头正中心对应的深度数据 get_distance()​​
  • ​​深度图像的可视化​​