intel realsense保存16位深度图与rgb图程序
生活随笔
收集整理的這篇文章主要介紹了
intel realsense保存16位深度图与rgb图程序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
IntelRealsense庫安裝
pip install pyrealsense2代碼解釋
import pyrealsense2 as rs import numpy as np import cv2 import json import png # 開啟攝像頭通信管道 pipeline = rs.pipeline() # 獲取配置 config = rs.config() # 設置深度圖為16位,fps為30,尺寸640x480 config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30) # 設置RGB圖為bgr格式(每個通道8位共24位),fps為30,尺寸640x480 config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30) # 開啟通道 profile = pipeline.start(config) # 獲取顏色對齊 align_to = rs.stream.color align = rs.align(align_to) depth_intrin = None# 該函數作用是獲取對齊后的圖像(rgb和深度圖對齊) def get_aligned_images():# 獲取一幀圖像frames = pipeline.wait_for_frames()# 將圖像對齊aligned_frames = align.process(frames)# 獲取對齊后的深度圖aligned_depth_frame = aligned_frames.get_depth_frame()# 深度相關參數# depth_intrin = aligned_depth_frame.profile.as_video_stream_profile().intrinsicscolor_frame = aligned_frames.get_color_frame()intr = color_frame.profile.as_video_stream_profile().intrinsicscamera_parameters = {'fx': intr.fx, 'fy': intr.fy,'ppx': intr.ppx, 'ppy': intr.ppy,'height': intr.height, 'width': intr.width,'depth_scale': profile.get_device().first_depth_sensor().get_depth_scale()}with open('./intrinsics.json', 'w') as fp:json.dump(camera_parameters, fp)# 獲取16位深度圖depth_image = np.asanyarray(aligned_depth_frame.get_data())# 轉為8位深度圖# 2^8為256,2^16為65536, 2^8/2^16約等于0.004,alpha=0.004,cv2.convertScaleAbs作用是所有像素點的值乘以alphadepth_image_8bit = cv2.convertScaleAbs(depth_image, alpha=0.004)pos=np.where(depth_image_8bit==0)depth_image_8bit[pos]=255# 獲取彩色圖color_image = np.asanyarray(color_frame.get_data())# 獲取到圖像中心的距離d = aligned_depth_frame.get_distance(320, 240)return color_image, depth_image,if __name__ == "__main__":n=0while 1:# 獲取rgb和深度圖rgb, depth = get_aligned_images()cv2.imshow('RGB image',rgb)key = cv2.waitKey(1)if key & 0xFF == ord('q') or key == 27:pipeline.stop()breakelif key==ord('s'):n=n+1# 保存rgb圖cv2.imwrite('./bd/rgb' + str(n)+'.jpg',rgb)# 保存16位深度圖with open('./bd/rgb' + str(n) + "_d.jpg", 'wb') as f:writer = png.Writer(width=depth.shape[1], height=depth.shape[0],bitdepth=16, greyscale=True)zgray2list = depth.tolist()writer.write(f, zgray2list)cv2.destroyAllWindows()總結
以上是生活随笔為你收集整理的intel realsense保存16位深度图与rgb图程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: win7原版镜像_聊聊专注于win7系统
- 下一篇: 【ALM】软件应用生命周期管理——Pol