§ 2.3 彩图与深度图同时读取
1. 导入依赖
import cv2
import numpy as np
from matplotlib import pyplot as plt
# 奥比中光 Orbbec Python SDK
from ObTypes import *
from Property import *
import Pipeline
import StreamProfile
from Error import ObException
import Context
%matplotlib inline
2. 配置日志等级
ctx = Context.Context(None)
ctx.setLoggerSeverity(OB_PY_LOG_SEVERITY_ERROR)
3. 创建管道
# 创建图像管道
pipe = Pipeline.Pipeline(None, None)
# 配置视频流, 控制开启/关闭
config = Pipeline.Config()
4. 配置视频流并开启管道
4.1 配置彩色相机
# 获取彩色相机的所有流配置,包括流的分辨率,帧率,以及帧的格式
color_profiles = pipe.getStreamProfileList(OB_PY_SENSOR_COLOR)
# 选择默认的彩色视频流配置
color_profile = color_profiles.getProfile(0)
# 构建更具体的彩色相机的信息
color_profile = color_profile.toConcreteStreamProfile(OB_PY_STREAM_VIDEO)
color_img_width = color_profile.width()
color_img_height = color_profile.height()
print(f"彩色图像宽度: {color_img_width}")
print(f"彩色图像高度: {color_img_height}")
# 使能彩色视频流
config.enableStream(color_profile)
输出日志 :
彩色图像宽度: 1280 彩色图像高度: 720
4.2 配置深度相机
# 获取彩色相机的所有流配置,包括流的分辨率,帧率,以及帧的格式
depth_profiles = pipe.getStreamProfileList(OB_PY_SENSOR_DEPTH)
# 选择默认的彩色视频流配置
depth_profile = depth_profiles.getProfile(0)
# 构建更具体的彩色相机的信息
depth_profile = depth_profile.toConcreteStreamProfile(OB_PY_STREAM_VIDEO)
depth_img_width = depth_profile.width()
depth_img_height = depth_profile.height()
print(f"深度图像宽度: {depth_img_width}")
print(f"深度图像高度: {depth_img_height}")
# 使能深度视频流
config.enableStream(depth_profile)
输出日志 :
深度图像宽度: 1280 深度图像高度: 800
4.3 配置对齐
# 设置对齐模式 - 硬件D2C
config.setAlignMode(OB_PY_ALIGN_D2C_HW_MODE)
4.4 开启管道
pipe.start(config, None)
5. 采集图像
def get_color_image(frames):
# 获取彩图
color_frame = frames.colorFrame()
if color_frame is None:
print("彩图获取失败")
return None
# 获取数据尺寸
size = color_frame.dataSize()
# 获取数据
data = color_frame.data()
# 图像解码
data_decode = cv2.imdecode(data, 1)
# 重新缩放
color_img = np.resize(data_decode,(color_img_height, color_img_width, 3))
return color_img
def get_depth_image(frames):
# 获取深度图
depth_frame = frames.depthFrame()
if depth_frame is None:
print("深度图获取失败")
return None
# 获取数据尺寸
size = depth_frame.dataSize()
# 获取数据
data = depth_frame.data()
# 修改深度图数据Data的尺寸
data = np.resize(data,(depth_img_height, depth_img_width, 2))
# 将两组8bit数据,转换为16bit
depth_img = (data[:,:,0]+data[:,:,1]*256) * depth_frame.getValueScale()
# 乘上缩放因子,将单位转换为1mm
depth_img = (depth_img).astype('float64')
return depth_img
采集图像
# 等待数据输入
timeout_ms = 100 # 超时等待时间, 单位ms
# 等待数据传入
frames = pipe.waitForFrames(timeout_ms)
展示彩图
color_img = get_color_image(frames)
plt.imshow(color_img[:, :, ::-1])
输出日志 :
展示深度图
depth_img = get_depth_image(frames)
plt.imshow(depth_img)
输出日志 :
6. 获取相机内参
# 获取相机内参
cam_param = pipe.getCameraParam()
print(cam_param)
输出日志 :
{'depthIntrinsic': {'fx': 690.4722900390625, 'fy': 690.0003662109375, 'cx': 639.1478271484375, 'cy': 360.1729736328125, 'width': 1280, 'height': 720}, 'rgbIntrinsic': {'fx': 690.4722900390625, 'fy': 690.0003662109375, 'cx': 639.1478271484375, 'cy': 360.1729736328125, 'width': 1280, 'height': 720}, 'depthDistortion': {'k1': 0.0, 'k2': 0.0, 'k3': 0.0, 'k4': 0.0, 'k5': 0.0, 'k6': 0.0, 'p1': 0.0, 'p2': 0.0}, 'rgbDistortion': {'k1': 51.501216888427734, 'k2': -103.04834747314453, 'k3': 119.6786880493164, 'k4': 51.35124588012695, 'k5': -102.76608276367188, 'k6': 120.11896514892578, 'p1': 0.00042185044731013477, 'p2': 0.0002477868692949414}, 'transform': {'rot': [0.999869704246521, -0.015809841454029083, -0.0032656663097441196, 0.01582268625497818, 0.9998670220375061, 0.0039456682279706, 0.003202851628884673, -0.003996825776994228, 0.9999868869781494], 'trans': [-14.101344108581543, -0.3078933358192444, -1.9329965114593506]}, 'isMirrored': False}
# 彩色相机的内参
cam_param["rgbIntrinsic"]
输出日志 :
{'fx': 690.4722900390625, 'fy': 690.0003662109375, 'cx': 639.1478271484375, 'cy': 360.1729736328125, 'width': 1280, 'height': 720}