文章可以轉(zhuǎn)載,但是必須表明出處!
本文使用Intel?Realsense?D435深度相機,拍攝RGB顏色圖及16位深度圖。
同時實現(xiàn)了以深度圖和RGB顏色圖為圖像幀的視頻顯示、錄制與保存。
以下為總體程序:
'''
使用realsense相機錄制視頻
'''
#!/usr/bin/env python
# coding=utf-8
import time
import h5py # 深度圖格式所在庫
import pyrealsense2 as rs
import numpy as np
import cv2
import os
class Camera(object):
'''
realsense相機處理類
'''
def __init__(self, width=1280, height=720, fps=30): # 圖片格式可根據(jù)程序需要進行更改
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.config.enable_stream(rs.stream.infrared, 1, self.width, self.height, rs.format.y8, fps)
# self.config.enable_stream(rs.stream.infrared, 2, self.width, self.height, rs.format.y8, fps)
self.pipeline.start(self.config) # 獲取圖像視頻流
def get_frame(self):
frames = self.pipeline.wait_for_frames() # 獲得frame (包括彩色,深度圖)
colorizer = rs.colorizer() # 創(chuàng)建偽彩色圖對象
depth_to_disparity = rs.disparity_transform(True)
disparity_to_depth = rs.disparity_transform(False)
# 創(chuàng)建對齊對象
align_to = rs.stream.color # rs.align允許我們執(zhí)行深度幀與其他幀的對齊
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()
# left_frame = frames.get_infrared_frame(1)
# right_frame = frames.get_infrared_frame(2)
color_image = np.asanyarray(color_frame.get_data())
colorizer_depth = np.asanyarray(colorizer.colorize(aligned_depth_frame).get_data())
depthx_image = np.asanyarray(aligned_depth_frame.get_data()) # 原始深度圖
# left_frame = np.asanyarray(left_frame.get_data())
# right_frame = np.asanyarray(right_frame.get_data())
return color_image, depthx_image, colorizer_depth
# left_frame, right_frame
def release(self):
self.pipeline.stop()
if __name__ == '__main__':
# 視頻保存路徑
os.mkdir(f'D://Realsense//Video//{int(time.time())}')
video_path = f'D://Realsense//Video//{int(time.time())}//targetvideo_rgb.mp4'
video_depthc_path = f'D://Realsense//Video//{int(time.time())}//targetvideo_depthcolor.mp4'
video_depth16_path = f'D://Realsense//Video//{int(time.time())}//targetvideo_depth.h5'
# video_left_path = f'D://Realsense//Video//Stereo_left//{int(time.time())}_left.mp4'
# video_right_path = f'D://Realsense//Video//Stereo_right//{int(time.time())}_right.mp4'
# 初始化參數(shù)
fps, w, h = 30, 1280, 720
# fps, w, h = 30, 640, 480
mp4 = cv2.VideoWriter_fourcc(*'mp4v') # 視頻格式
# 視頻保存而建立對象
# wr_left = cv2.VideoWriter(video_left_path, mp4, fps, (w, h), isColor=False)
# wr_right = cv2.VideoWriter(video_right_path, mp4, fps, (w, h), isColor=False)
# 完成相機初始化
cam = Camera(w, h, fps)
flag_V = 0
idx = 0
id = 0
print('錄制視頻請按: s, 保存視頻或退出請按:q')
while True:
# 讀取圖像幀,包括RGB圖和深度圖
color_image, depthxy_image, colorizer_depth = cam.get_frame()
cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)
cv2.imshow('RealSense', color_image)
key = cv2.waitKey(1)
if key & 0xFF == ord('s') :
flag_V = 1
# 創(chuàng)建視頻文件
wr = cv2.VideoWriter(video_path, mp4, fps, (w, h), isColor=True)
wr_colordepth = cv2.VideoWriter(video_depthc_path, mp4, fps, (w, h), isColor=True)
wr_depth = h5py.File(video_depth16_path, 'w')
print('...錄制視頻中...')
if flag_V == 1:
# 保存圖像幀
wr.write(color_image) # 保存RGB圖像幀
wr_colordepth.write(colorizer_depth) # 保存相機自身著色深度圖
# wr_left.write(left_image) # 保存左幀深度圖
# wr_right.write(right_image) # 保存右?guī)疃葓D
# res, depth16_image = cv2.imencode('.png', depthxy_image) # 深度圖解碼方式一:點云小,但是出錯
depth16_image = cv2.imencode('.png', depthxy_image)[1] # 深度圖解碼方式二:文件較大,測試穩(wěn)定
depth_map_name = str(id).zfill(5) + '_depth.png'
# wr_depth[str(idx).zfill(5)] = depth16_image # 儲存方法:1 前3幀和沒11幀出現(xiàn)高質(zhì)量點云,其他錯誤
wr_depth[depth_map_name] = depth16_image # 儲存方法:2 所有點云準確,但是點云質(zhì)量不高
idx += 1
id = id + 1
if key & 0xFF == ord('q') or key == 27:
cv2.destroyAllWindows()
print('...錄制結束/直接退出...')
break
# 錄制完畢,釋放對象
wr.release()
wr_colordepth.release()
# wr_left.release()
# wr_right.release()
wr_depth.close()
cam.release()
print(f'若保存視頻,則視頻保存在:{video_path}')
程序閱讀如下:
1.庫引用
import time # 獲取時間信息,用于創(chuàng)建不同文件名稱的文件夾
import h5py # 深度圖格式所在庫
import pyrealsense2 as rs # 相機集成庫,用于相機初始化與相機視頻流獲取
import numpy as np # 將圖像幀轉(zhuǎn)換為數(shù)據(jù)信息以進行圖像保存
import cv2 # 視覺圖像庫,用于保存圖像幀與視頻
import os # 系統(tǒng)操作庫,用于系統(tǒng)路徑尋覓與文件夾創(chuàng)建
2.相機初始化函數(shù)
class Camera(object):
'''
realsense相機處理類
'''
def __init__(self, width=1280, height=720, fps=30): # 圖片格式可根據(jù)程序需要進行更改
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.config.enable_stream(rs.stream.infrared, 1, self.width, self.height, rs.format.y8, fps)
# self.config.enable_stream(rs.stream.infrared, 2, self.width, self.height, rs.format.y8, fps)
self.pipeline.start(self.config) # 獲取圖像視頻流
3.圖像流獲取函數(shù)?
def get_frame(self):
frames = self.pipeline.wait_for_frames() # 獲得frame (包括彩色,深度圖)
colorizer = rs.colorizer() # 創(chuàng)建偽彩色圖對象
depth_to_disparity = rs.disparity_transform(True)
disparity_to_depth = rs.disparity_transform(False)
# 創(chuàng)建對齊對象
align_to = rs.stream.color # rs.align允許我們執(zhí)行深度幀與其他幀的對齊
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()
# left_frame = frames.get_infrared_frame(1)
# right_frame = frames.get_infrared_frame(2)
color_image = np.asanyarray(color_frame.get_data()) # 將圖像幀轉(zhuǎn)換為矩陣格式
colorizer_depth = np.asanyarray(colorizer.colorize(aligned_depth_frame).get_data())
depthx_image = np.asanyarray(aligned_depth_frame.get_data())
# left_frame = np.asanyarray(left_frame.get_data())
# right_frame = np.asanyarray(right_frame.get_data())
return color_image, depthx_image, colorizer_depth # 返回圖像數(shù)據(jù)值
# left_frame, right_frame
4.相機功能釋放函數(shù)
def release(self):
self.pipeline.stop() # 停止相機拍攝
5.主程序:文件路徑需要根據(jù)本地情況進行修改,圖片格式根據(jù)需求進行修改
if __name__ == '__main__':
# 視頻保存路徑 路徑需要根據(jù)本地情況進行修改
os.mkdir(f'D://Realsense//Video//{int(time.time())}')
video_path = f'D://Realsense//Video//{int(time.time())}//targetvideo_rgb.mp4'
video_depthc_path = f'D://Realsense//Video//{int(time.time())}//targetvideo_depthcolor.mp4'
video_depth16_path = f'D://Realsense//Video//{int(time.time())}//targetvideo_depth.h5'
# video_left_path = f'D://Realsense//Video//Stereo_left//{int(time.time())}_left.mp4'
# video_right_path = f'D://Realsense//Video//Stereo_right//{int(time.time())}_right.mp4'
# 初始化參數(shù)
fps, w, h = 30, 1280, 720
# fps, w, h = 30, 640, 480
mp4 = cv2.VideoWriter_fourcc(*'mp4v') # 視頻格式
# 視頻保存對象
# wr_left = cv2.VideoWriter(video_left_path, mp4, fps, (w, h), isColor=False)
# wr_right = cv2.VideoWriter(video_right_path, mp4, fps, (w, h), isColor=False)
# 相機初始化
cam = Camera(w, h, fps)
flag_V = 0
idx = 0
id = 0
print('錄制視頻請按: s, 保存視頻或退出請按:q')
# 循環(huán)保存圖像幀以建立視頻
while True:
# 讀取圖像幀,包括RGB圖和深度圖
color_image, depthxy_image, colorizer_depth = cam.get_frame()
cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)
cv2.imshow('RealSense', color_image)
key = cv2.waitKey(1)
if key & 0xFF == ord('s') :
flag_V = 1
# 創(chuàng)建視頻文件
wr = cv2.VideoWriter(video_path, mp4, fps, (w, h), isColor=True)
wr_colordepth = cv2.VideoWriter(video_depthc_path, mp4, fps, (w, h), isColor=True)
wr_depth = h5py.File(video_depth16_path, 'w')
print('...錄制視頻中...')
if flag_V == 1:
# 保存圖像幀
wr.write(color_image) # 保存RGB圖像幀
wr_colordepth.write(colorizer_depth) # 保存相機自身著色深度圖
# wr_left.write(left_image) # 保存左幀深度圖
# wr_right.write(right_image) # 保存右?guī)疃葓D
depth16_image = cv2.imencode('.png', depthxy_image)[1] # 深度圖解碼
depth_map_name = str(id).zfill(5) + '_depth.png'
wr_depth[depth_map_name] = depth16_image # 所有點云準確
id = id + 1
if key & 0xFF == ord('q') or key == 27:
cv2.destroyAllWindows()
print('...錄制結束/直接退出...')
break
# 錄制完畢,釋放視頻對象
wr.release()
wr_colordepth.release()
# wr_left.release()
# wr_right.release()
wr_depth.close()
cam.release()
print(f'若保存視頻,則視頻保存在:{video_path}')
圖片拍攝結果:
RGB圖
16位深度圖
?偽彩色圖
總結:
對比上一篇文章,該程序完整的保存了16位深度圖,而非將其轉(zhuǎn)換為8位深度圖;
16位深度圖保存采用的h5py格式,在后續(xù)進行圖片抽幀時需要對應的圖片抽取程序。
具體代碼可查看我后續(xù)的博客文章來源:http://www.zghlxwxcb.cn/news/detail-522022.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-522022.html
到了這里,關于【Intel Realsense D435】16位深度圖和RGB顏色圖的視頻顯示、錄制和保存(Python)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!