opencv_viewer_example.py(读取读取彩色图和深度图)

## License: Apache 2.0. See LICENSE file in root directory.
## Copyright(c) 2015-2017 Intel Corporation. All Rights Reserved.

###############################################
##      Open CV and Numpy integration        ##
###############################################

import pyrealsense2 as rs
import numpy as np
import cv2

# Configure depth and color streams
#配置深度和视频流
#创建一个pipeline对象,用于管理数据流,以及一个config对象,用于配置相机流。
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))
# #
# pipeline_wrapper是pipeline对象的一个包装器,它提供了对pipeline的额外控制。
# config.resolve(pipeline_wrapper)用于根据配置文件解析pipeline_wrapper,创建一个pipeline_profile对象,该对象包含了流配置的详细信息。
# device是从pipeline_profile中获取的RealSense设备对象。
# device_product_line获取设备的产品线信息,并将其转换为字符串。产品线信息可以是“L500”或“D400”等,这有助于确定设备的型号和功能。
# #


#查设备是否包含RGB相机。如果不含,则打印消息并退出程序。
found_rgb = False
for s in device.sensors:
    if s.get_info(rs.camera_info.name) == 'RGB Camera':
        found_rgb = True
        break
if not found_rgb:
    print("The demo requires Depth camera with Color sensor")
    exit(0)
# #
# 如果found_rgb仍然是False,这意味着没有找到RGB相机。
# 打印一条消息,说明演示需要带有颜色传感器的深度相机。
# 调用exit(0)以终止程序。exit(0)表示程序正常退出,其中0是退出状态码,通常表示成功。
# #


#配置深度和颜色流的参数,包括分辨率、格式和帧率。
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 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()
        color_frame = frames.get_color_frame()

        #1. pipeline.wait_for_frames()
        # 函数作用:这个函数的作用是阻塞当前线程,直到摄像头或传感器捕获到一组完整的帧。这些帧可能包括深度帧(depth frame)、彩色帧(color frame)、红外帧(infrared frame)等,具体取决于你的摄像头配置和pipeline的配置。
        # 返回值:该函数返回一个frameset对象,这个对象包含了所有可用的帧。frameset是一个容器,用于管理和访问捕获到的不同类型的帧。
        # 2. depth_frame = frames.get_depth_frame()
        # 函数作用:从frameset对象中获取深度帧。深度帧是一个图像,其中的每个像素值代表相应位置在场景中的物体到摄像头的距离。这些距离通常以毫米(mm)为单位。
        # 返回值:如果frameset中包含深度帧,则返回一个depth_frame对象,该对象包含了深度图像的数据。如果没有深度帧,则返回None。
        # 3. color_frame = frames.get_color_frame()
        # 函数作用:从frameset对象中获取彩色帧。彩色帧是普通的RGB图像,与我们日常看到的照片或视频帧类似。
        # 返回值:如果frameset中包含彩色帧,则返回一个color_frame对象,该对象包含了彩色图像的数据。如果没有彩色帧,则返回None。
        if not depth_frame or not color_frame:
            continue

        # Convert images to numpy arrays
        #将深度和颜色帧转换为NumPy数组。
        depth_image = np.asanyarray(depth_frame.get_data())
        color_image = np.asanyarray(color_frame.get_data())

        # Apply colormap on depth image (image must be converted to 8-bit per pixel first)
        #将深度图像转换为彩色图像,以便于可视化。
        depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)

        #获取深度和颜色图像的尺寸。
        depth_colormap_dim = depth_colormap.shape
        color_colormap_dim = color_image.shape

        # If depth and color resolutions are different, resize color image to match depth image for display
        #如果深度和颜色图像的尺寸不同,则调整颜色图像的尺寸以匹配深度图像,然后将它们并排显示。
        if depth_colormap_dim != color_colormap_dim:
            resized_color_image = cv2.resize(color_image, dsize=(depth_colormap_dim[1], depth_colormap_dim[0]), interpolation=cv2.INTER_AREA)
            images = np.hstack((resized_color_image, depth_colormap))
        else:
            images = np.hstack((color_image, depth_colormap))

        # Show images
        #创建一个窗口并显示合并后的图像。cv2.waitKey(1)用于等待按键事件,参数1表示等待1毫秒。

        #cv2.WINDOW_AUTOSIZE: 创建一个自动调整大小的窗口。窗口的大小会自动调整以适应显示的图像,用户不能手动更改窗口的大小。
        #cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)#无法调整窗口大小
        #cv2.namedWindow('RealSense', 1)#无法调整窗口大小

        # cv2.WND_PROP_FULLSCREEN: 全屏模式。如果使用此标志,窗口将以全屏模式打开。
        #cv2.namedWindow('RealSense', cv2.WND_PROP_FULLSCREEN)#可调整窗口大小

        #cv2.WINDOW_NORMAL: 创建一个可调整大小的窗口。用户可以手动更改窗口的大小。
        #cv2.namedWindow('RealSense', cv2.WINDOW_NORMAL)#可调窗口大小
        cv2.namedWindow('RealSense', 0)#可调窗口大小


        cv2.imshow('RealSense', images)#单独运行,无cv2.namedWindow(),
        # 可以显示窗口,但无法调整大小(opencv-python版本为4.8),可能是opencv安装在虚拟机的问题
        cv2.waitKey(1)
        # 函数cv2.waitKey()用来等待按键,当用户按下键盘后,该语句会被执行,并获取返回值。其语法格式为:
        # retval = cv2.waitKey( [delay] )
        # 式中:
        #(1)retval表示返回值。如果在指定的等待时间(参数delay指定)内,有按键被按下,则返回该按键的ASCII码(然后执行后续语句)。
        # 在等待时间(参数delay指定)结束后,如果没有按键被按下,则返回-1(然后继续执行后续语句);
        #(2)delay表示等待键盘触发的时间,单位是ms。当该值是负数或者零时,表示无限等待。该值默认为0
        
        #注意:可以利用(1)实现按下特定按键,新建一个窗口或摧毁所有窗口等

        #本次程序因为是无限循环,所以每次相机拍摄的画面会保存1ms,1ms后继续刷新画面(设置相机读取画面30帧),重新显示。如果相机关闭,则退出。

#在退出循环或出现异常时,停止相机流。这确保了即使在脚本异常结束时,相机资源也能被正确释放。
finally:
    # Stop streaming
    pipeline.stop()

结果图

参考资料:

官方源码

librealsense/wrappers/python/examples at master · IntelRealSense/librealsense (github.com) 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值