奥比中光Geminipro相机使用

相机使用入门,使用python获取深度图和颜色图并显示。


#安装依赖
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
import sys

#%matplotlib inline
#配置日志等级
ctx = Context.Context(None)
ctx.setLoggerSeverity(OB_PY_LOG_SEVERITY_ERROR)
# 创建图像管道
try:
    pipe = Pipeline.Pipeline(None, None)
    # 配置视频流, 控制开启/关闭
    config = Pipeline.Config()
    # 配置视频流并开启通道
    # 获取彩色相机的所有流配置,包括流的分辨率,帧率,以及帧的格式
    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)
    # 配置深度相机
    # 获取彩色相机的所有流配置,包括流的分辨率,帧率,以及帧的格式
    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)
    # 配置对齐
    # 设置对齐模式 - 硬件D2C
    config.setAlignMode(OB_PY_ALIGN_D2C_HW_MODE)
    # 开启管道
    pipe.start(config, None)
    # 采集图像
except ObException as e:
    print("function: %s\nargs: %s\nmessage: %s\ntype: %d\nstatus: %d" % (
    e.getName(), e.getArgs(), e.getMessage(), e.getExceptionType(), e.getStatus()))
    print("Current device is not support depth sensor!")
    sys.exit()



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)
# 获取相机内参
cam_param = pipe.getCameraParam()
print(cam_param)
# 彩色相机的内参
cam_param["rgbIntrinsic"]


你可能感兴趣的:(python,开发语言)