文章可以转载,但是必须表明出处!
最近在学习如何使用Intel Realsense D435深度相机,由此记录一下程序的开发过程。
以下为总体程序:
'''
使用realsense相机录制视频
'''
#!/usr/bin/env python
# coding=utf-8
import time
import pyrealsense2 as rs
import numpy as np
import cv2
class Camera(object):
'''
realsense相机处理类
'''
def __init__(self, width=1280, height=720, fps=30):
self.width = width
self.height = height
self.pipeline = rs.pipeline()
self.config = rs.config()
self.config.enable_stream(rs.stream.color, self.width, self.height, rs.format.bgr8, fps)
self.config.enable_stream(rs.stream.depth, self.width, self.height, rs.format.z16, fps)
# self.align = rs.align(rs.stream.color) # depth2rgb
self.pipeline.start(self.config) # 开始连接相机
def get_frame(self):
frames = self.pipeline.wait_for_frames() # 获得frame (包括彩色,深度图)
# 创建对齐对象
align_to = rs.stream.color # rs.align允许我们执行深度帧与其他帧的对齐
align = rs.align(align_to) # “align_to”是我们计划对齐深度帧的流类型。
aligned_frames = align.process(frames)
# 获取对齐的帧
aligned_depth_frame = aligned_frames.get_depth_frame() # aligned_depth_frame是对齐的深度图
color_frame = aligned_frames.get_color_frame()
colorizer = rs.colorizer()
depthx_image = np.asanyarray(aligned_depth_frame.get_data())
color_image = np.asanyarray(color_frame.get_data())
colorizer_depth = np.asanyarray(colorizer.colorize(aligned_depth_frame).get_data())
return color_image, depthx_image,colorizer_depth
def release(self):
self.pipeline.stop()
if __name__=='__main__':
# 视频保存路径
video_path = f'D://Realsense//rgb_data//{int(time.time())}.mp4'
video_depth_path = f'D://Realsense//depth_data//{int(time.time())}_depth.mp4'
video_depthcolor_path = f'D://Realsense//depthcolor_data//{int(time.time())}_depthcolor.mp4'
video_depthcolor_camera_path = f'D://Realsense//camera_colordepth//{int(time.time())}_depthcolor.mp4'
# 初始化参数
fps, w, h = 30, 1280, 720
mp4 = cv2.VideoWriter_fourcc(*'mp4v') # 视频格式
wr = cv2.VideoWriter(video_path, mp4, fps, (w, h), isColor=True) # 视频保存而建立对象
wr_depth = cv2.VideoWriter(video_depth_path, mp4, fps, (w, h), isColor=False)
wr_depthcolor = cv2.VideoWriter(video_depthcolor_path, mp4, fps, (w, h), isColor=True)
wr_camera_colordepth = cv2.VideoWriter(video_depthcolor_camera_path, mp4, fps, (w, h), isColor=True)
cam = Camera(w, h, fps)
print('录制视频请按: s, 保存视频或退出请按:q')
flag_V = 0
while True:
color_image, depth_image, colorizer_depth = cam.get_frame() # 读取图像帧,包括RGB图和深度图
#深度图数据格式转换,uint16→uint8
depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)
images = np.hstack((color_image, depth_colormap))
cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)
cv2.imshow('RealSense', colorizer_depth)
# print('ll')
key = cv2.waitKey(1)
if key & 0xFF == ord('s') :
flag_V = 1
if flag_V == 1:
wr.write(color_image) # 保存RGB图像帧
wr_depth.write(depth_image) # 保存基于灰度深度图
wr_depthcolor.write(depth_colormap) # 保存计算所得着色深度图
wr_camera_colordepth.write(colorizer_depth) # 保存相机自行计算的着色深度图
print('...录制视频中...')
if key & 0xFF == ord('q') or key == 27:
cv2.destroyAllWindows()
print('...录制结束/直接退出...')
break
wr_depthcolor.release()
wr_depth.release()
wr.release()
wr_camera_colordepth.release()
cam.release()
print(f',若保存视频,则视频保存在:{video_path}')
步骤分解如下:
①库引用
'''
使用realsense相机录制视频
'''
#!/usr/bin/env python
# coding=utf-8
import time
import pyrealsense2 as rs
import numpy as np
import cv2
②相机初始化、图像流数据获取与截止:由类实现。
class Camera(object):
'''
realsense相机处理类
'''
def __init__(self, width=1280, height=720, fps=30):
self.width = width
self.height = height
self.pipeline = rs.pipeline()
self.config = rs.config()
self.config.enable_stream(rs.stream.color, self.width, self.height, rs.format.bgr8, fps)
self.config.enable_stream(rs.stream.depth, self.width, self.height, rs.format.z16, fps)
self.pipeline.start(self.config) # 开始连接相机
def get_frame(self):
frames = self.pipeline.wait_for_frames() # 获得frame (包括彩色,深度图)
# 创建对齐对象
align_to = rs.stream.color # rs.align允许我们执行深度帧与其他帧的对齐
align = rs.align(align_to) # “align_to”是我们计划对齐深度帧的流类型。
aligned_frames = align.process(frames)
# 获取对齐的帧
aligned_depth_frame = aligned_frames.get_depth_frame() # aligned_depth_frame是对齐的深度图
color_frame = aligned_frames.get_color_frame()
colorizer = rs.colorizer()
depthx_image = np.asanyarray(aligned_depth_frame.get_data())
color_image = np.asanyarray(color_frame.get_data())
colorizer_depth = np.asanyarray(colorizer.colorize(aligned_depth_frame).get_data())
return color_image, depthx_image,colorizer_depth
def release(self):
self.pipeline.stop()
③主程序保存视频:根据键盘命令进行拍摄,保存路径需要根据本地情况进行修改。
if __name__=='__main__':
# 视频保存路径
video_path = f'D://Realsense//rgb_data//{int(time.time())}.mp4'
video_depth_path = f'D://Realsense//depth_data//{int(time.time())}_depth.mp4'
video_depthcolor_path = f'D://Realsense//depthcolor_data//{int(time.time())}_depthcolor.mp4'
video_depthcolor_camera_path = f'D://Realsense//camera_colordepth//{int(time.time())}_depthcolor.mp4'
# 初始化参数
fps, w, h = 30, 1280, 720
mp4 = cv2.VideoWriter_fourcc(*'mp4v') # 视频格式
wr = cv2.VideoWriter(video_path, mp4, fps, (w, h), isColor=True) # 视频保存而建立对象
wr_depth = cv2.VideoWriter(video_depth_path, mp4, fps, (w, h), isColor=False)
wr_depthcolor = cv2.VideoWriter(video_depthcolor_path, mp4, fps, (w, h), isColor=True)
wr_camera_colordepth = cv2.VideoWriter(video_depthcolor_camera_path, mp4, fps, (w, h), isColor=True)
cam = Camera(w, h, fps)
print('录制视频请按: s, 保存视频或退出请按:q')
flag_V = 0
while True:
color_image, depth_image, colorizer_depth = cam.get_frame() # 读取图像帧,包括RGB图和深度图
#深度图数据格式转换,uint16→uint8
depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)
images = np.hstack((color_image, depth_colormap))
cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)
cv2.imshow('RealSense', images)
# print('ll')
key = cv2.waitKey(1)
if key & 0xFF == ord('s') :
flag_V = 1
if flag_V == 1:
wr.write(color_image) # 保存RGB图像帧
wr_depth.write(depth_image) # 保存基于灰度深度图
wr_depthcolor.write(depth_colormap) # 保存计算所得着色深度图
wr_camera_colordepth.write(colorizer_depth) # 保存相机自行计算的着色深度图
print('...录制视频中...')
if key & 0xFF == ord('q') or key == 27:
cv2.destroyAllWindows()
print('...录制结束/直接退出...')
break
wr_depthcolor.release()
wr_depth.release()
wr.release()
wr_camera_colordepth.release()
cam.release()
print(f',若保存视频,则视频保存在:{video_path}')
视频录制截图:
RGB图像
深度图极其伪彩色图
文章来源:https://www.toymoban.com/news/detail-525348.html
文章来源地址https://www.toymoban.com/news/detail-525348.html
到了这里,关于【Intel Realsense D435】实现视频显示、录制和保存(Python)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!