intel Realsense D435i如何在Python环境中使用预设


前言

在使用intel Realsense D435i相机时想使用不同的官方的预设如下图,但是资源比较少 ,我自己摸索了一下找到了怎么使用。
在这里插入图片描述


一、preset是什么?

就是相机在使用的过程中侧重点,侧重精度啦 ,侧重密集程度啦。可以在intel Realsense viewer中大家可以自己测试一下具体每个是什么作用,我粘个链接给大家。
Intel RealSense D435i:简介

二、使用步骤

1.整体验证代码

代码如下(示例):

import pyrealsense2 as rs


def main():
    # 创建管道
    pipeline = rs.pipeline()

    # 创建配置对象
    config = rs.config()

    # 启动相机管道
    pipeline_profile = pipeline.start(config)

    # 获取设备传感器
    device = pipeline_profile.get_device()
    depth_sensor = device.first_depth_sensor()

    # 检查传感器是否支持视觉预设
    if depth_sensor.supports(rs.option.visual_preset):
        # 选择一个预设,例如 high_accuracy
        preset = rs.rs400_visual_preset.high_accuracy

        # 设置预设
        depth_sensor.set_option(rs.option.visual_preset, preset)

        print(f"视觉预设已设置为:{preset}")
    else:
        print("深度传感器不支持视觉预设选项")

    # 捕获帧
    try:
        while True:
            # 等待帧
            frames = pipeline.wait_for_frames()

            # 获取深度帧
            depth_frame = frames.get_depth_frame()
            if not depth_frame:
                continue

            # 处理深度帧(示例代码中只是打印帧信息)
            print(f"深度帧:宽度 {depth_frame.get_width()},高度 {depth_frame.get_height()}")

    except KeyboardInterrupt:
        # 停止管道
        pipeline.stop()


if __name__ == "__main__":
    main()

2.核心

代码如下(示例):

    # 检查传感器是否支持视觉预设
    if depth_sensor.supports(rs.option.visual_preset):
        # 选择一个预设,例如 high_accuracy
        preset = rs.rs400_visual_preset.high_accuracy

        # 设置预设
        depth_sensor.set_option(rs.option.visual_preset, preset)

        print(f"视觉预设已设置为:{preset}")
    else:
        print("深度传感器不支持视觉预设选项")

除了high_accuracy还有其他的也可以直接替换。

3.测试代码

import pyrealsense2 as rs
import numpy as np
import cv2
# 定义深度图像的最小和最大值,用于伪彩色映射
DEPTH_MIN = 0  # 深度最小值
DEPTH_MAX = 1000  # 深度最大值

def depth_to_color(depth_image):
    # 将深度值线性映射到0-255范围内
    normalized_depth = np.clip((depth_image - DEPTH_MIN) / (DEPTH_MAX - DEPTH_MIN), 0, 1)
    # 使用OpenCV的applyColorMap函数将灰度图像映射到伪彩色图像
    pseudo_color = cv2.applyColorMap((normalized_depth * 255).astype(np.uint8), cv2.COLORMAP_JET)
    return pseudo_color
# Configure depth stream only
pipeline = rs.pipeline()
config = rs.config()

# Get device product line for setting a supporting resolution
pipeline_wrapper = rs.pipeline_wrapper(pipeline)
pipeline_profile = config.resolve(pipeline_wrapper)
device = pipeline_profile.get_device()
device_product_line = str(device.get_info(rs.camera_info.product_line))
# 获取设备传感器
depth_sensor = device.first_depth_sensor()

# 检查传感器是否支持视觉预设
if depth_sensor.supports(rs.option.visual_preset):
    # 选择一个预设,例如 high_accuracy
    preset = rs.rs400_visual_preset.high_density

    # 设置预设
    depth_sensor.set_option(rs.option.visual_preset, preset)

    print(f"视觉预设已设置为:{preset}")
else:
    print("深度传感器不支持视觉预设选项")
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)

# Start streaming
pipeline.start(config)

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

        # Convert depth image to numpy array
        depth_image = np.asanyarray(depth_frame.get_data())
        depth_image = depth_to_color(depth_image)
        # Normalize the depth image to fall between 0 (black) and 255 (white)
        depth_image_8bit = cv2.normalize(depth_image, None, 0, 255, cv2.NORM_MINMAX)
        depth_image_8bit = np.uint8(depth_image_8bit)

        # Apply colormap on depth image (image must be converted to 8-bit per pixel first)
        depth_colormap = cv2.applyColorMap(depth_image_8bit, cv2.COLORMAP_JET)

        # Show depth image
        cv2.namedWindow('RealSense Depth', cv2.WINDOW_AUTOSIZE)
        cv2.imshow('RealSense Depth', depth_colormap)
        cv2.waitKey(1)

finally:
    # Stop streaming
    pipeline.stop()

修改preset = rs.rs400_visual_preset.high_density即可测试结果


总结

这是一个自用的记录,有问题大家也可以问。

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值