国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【Intel Realsense D435】16位深度圖和RGB顏色圖的視頻顯示、錄制和保存(Python)

這篇具有很好參考價值的文章主要介紹了【Intel Realsense D435】16位深度圖和RGB顏色圖的視頻顯示、錄制和保存(Python)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

文章可以轉(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圖

d435深度圖格式,數(shù)碼相機,python,算法,視頻

16位深度圖

d435深度圖格式,數(shù)碼相機,python,算法,視頻

?偽彩色圖

d435深度圖格式,數(shù)碼相機,python,算法,視頻

總結:

對比上一篇文章,該程序完整的保存了16位深度圖,而非將其轉(zhuǎn)換為8位深度圖;

16位深度圖保存采用的h5py格式,在后續(xù)進行圖片抽幀時需要對應的圖片抽取程序。

具體代碼可查看我后續(xù)的博客

?文章來源地址http://www.zghlxwxcb.cn/news/detail-522022.html

到了這里,關于【Intel Realsense D435】16位深度圖和RGB顏色圖的視頻顯示、錄制和保存(Python)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • Ubuntu 20.04 Intel RealSense D435i 相機標定教程

    Ubuntu 20.04 Intel RealSense D435i 相機標定教程

    報錯:sumpixel_test.cpp:2:10: fatal error: backward.hpp: 沒有那個文件或目錄,將sumpixel_test.cpp中# include \\\"backward.hpp\\\"改為:#include “code_utils/backward.hpp”。 報錯 創(chuàng)建rs_imu_calibration.launch 找到realsense-ros包,進入/catkin_ws/src/realsense2_camera/launch(路徑僅供參考),復制其中的rs_camera.launch,并重

    2024年01月16日
    瀏覽(59)
  • 【深度相機D435i】Windows+Ubuntu下調(diào)用D435i利用Python讀取、保存RGB、Depth圖片

    【深度相機D435i】Windows+Ubuntu下調(diào)用D435i利用Python讀取、保存RGB、Depth圖片

    最近組里面的項目需要用到D435i深度相機采集深度圖片,所以記錄一下在Windows+Ubuntu的環(huán)境下使用D435i深度相機的流程,以及如何利用python讀取、保存常見的RGB、Depth圖片。 D435i 在小巧外形中采用英特爾模塊和視覺處理器,是一個功能強大的一體產(chǎn)品,可與可定制軟件配合使用

    2024年02月02日
    瀏覽(36)
  • (已修正精度 1mm左右)Realsense d435i深度相機+Aruco+棋盤格+OpenCV手眼標定全過程記錄

    (已修正精度 1mm左右)Realsense d435i深度相機+Aruco+棋盤格+OpenCV手眼標定全過程記錄

    最近幫別人做了個手眼標定,然后我標定完了大概精度能到1mm左右。所以原文中誤差10mm可能是當時那個臂本身的坐標系有問題。然后用的代碼改成了基于python的,放在下面。 新來的小伙伴可以只參考前面的代碼就可以完成標定了。 有問題的話可以留言,一起交流~ 手眼標定

    2024年02月04日
    瀏覽(49)
  • Realsense d435i驅(qū)動安裝、配置及校準

    Realsense d435i驅(qū)動安裝、配置及校準

    寫在前面 本文是在ubuntu20.04下安裝,其它版本大同小異??赡艹霈F(xiàn)的問題,主要由各自安裝相關庫版本不一致導致,故問題不一,但一般很好解決,正常情況下不會出現(xiàn)。 Intel Realsense 深度攝像頭D435i將D435強大的深度感知能力和慣性測量單元(IMU)結合起來,可滿足RGBD、單目、

    2024年02月02日
    瀏覽(56)
  • SLAM算法與工程實踐——相機篇:RealSense D435使用(2)

    SLAM算法與工程實踐——相機篇:RealSense D435使用(2)

    下面是SLAM算法與工程實踐系列文章的總鏈接,本人發(fā)表這個系列的文章鏈接均收錄于此 下面是專欄地址: 這個系列的文章是分享SLAM相關技術算法的學習和工程實踐 參考: realsense相機兩種獲取相機內(nèi)外參的方式 使用Realsense D435i運行VINS-Fusion kalibr標定realsenseD435i --多相機標定

    2024年02月03日
    瀏覽(27)
  • C++ api調(diào)用realsense d435相機,并計算真實世界坐標值

    C++ api調(diào)用realsense d435相機,并計算真實世界坐標值

    ????????在使用Intel RealSense相機進行編程時,首先需要創(chuàng)建一個 rs2::pipeline 對象,并使用該對象啟動相機的數(shù)據(jù)流。在啟動數(shù)據(jù)流后,相機將根據(jù)配置的參數(shù)生成相應的數(shù)據(jù)流,例如深度、彩色或紅外流,并將這些數(shù)據(jù)傳輸?shù)接嬎銠C中。 RS2_STREAM_DEPTH :指定啟用的流類型為

    2024年02月11日
    瀏覽(24)
  • 機械臂手眼標定realsense d435相機——眼在手上python、matlab

    機械臂手眼標定realsense d435相機——眼在手上python、matlab

    兩周內(nèi)看了好多博客,博客上的代碼甚至github上的代碼都試過了一遍,各種語言matlab、c++、python,了解到了許多做手眼標定的平臺——halcon、ros(這倆還需要從頭開始學,時間不太夠用),最后看到了魚香ros的博客,參考了一下并總結完整,附鏈接 此博客僅記錄學習過程總結

    2024年02月15日
    瀏覽(59)
  • realsense D435i 實現(xiàn)外部時鐘觸發(fā)硬件同步多相機數(shù)據(jù)采集

    realsense D435i 實現(xiàn)外部時鐘觸發(fā)硬件同步多相機數(shù)據(jù)采集

    最近有一個調(diào)試D435i相機的工作,需要使得三個相機能夠完成硬件觸發(fā)的同步,具體來說,就是有一個固定頻率的外部脈沖信號,使得三個相機能夠根據(jù)外部脈沖信號的硬件觸發(fā)完成雙目圖片、深度圖片、彩色圖片、IMU數(shù)據(jù)的實時響應采集,因為外部脈沖信號是通過一個精確

    2024年01月16日
    瀏覽(301)
  • C++ api調(diào)用realsense d435相機,將坐標轉(zhuǎn)換到相機坐標系

    C++ api調(diào)用realsense d435相機,將坐標轉(zhuǎn)換到相機坐標系

    ????????在使用Intel RealSense相機進行編程時,首先需要創(chuàng)建一個 rs2::pipeline 對象,并使用該對象啟動相機的數(shù)據(jù)流。在啟動數(shù)據(jù)流后,相機將根據(jù)配置的參數(shù)生成相應的數(shù)據(jù)流,例如深度、彩色或紅外流,并將這些數(shù)據(jù)傳輸?shù)接嬎銠C中。 RS2_STREAM_DEPTH :指定啟用的流類型為

    2024年02月16日
    瀏覽(23)
  • Realsense D435i Yolov5目標檢測實時獲得目標三維位置信息

    Realsense D435i Yolov5目標檢測實時獲得目標三維位置信息

    - Colorimage: - Colorimage and depthimage: 1.一個可以運行YOLOv5的python環(huán)境 2.一個realsense相機和pyrealsense2庫 在下面兩個環(huán)境中測試成功 win10 python 3.8 Pytorch 1.10.2+gpu CUDA 11.3 NVIDIA GeForce MX150 ubuntu16.04 python 3.6 Pytorch 1.7.1+cpu 修改模型配置文件,以yolov5s為例。 如果使用自己訓練的模型,需要進

    2024年02月04日
    瀏覽(50)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包