intel D435i 双目相机 拍摄图片并保存 python调用示例
生活随笔
收集整理的這篇文章主要介紹了
intel D435i 双目相机 拍摄图片并保存 python调用示例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
程序說明:
本程序提供了一個最簡單的python調用Intel雙目相機D435/D435i的方法,作用是調用相機拍攝圖片并保存到本地。
可拍攝的圖片有:左目圖像/右目圖像/RGB圖像/深度圖像/深度顏色渲染圖像
若要調整 保存路徑、圖片分辨率 可以根據需要修改代碼。
運行程序前需要在當前路徑創建一個名為“camera_shot”的文件夾,如果文件夾名稱錯誤或者路徑錯誤是找不到拍攝的圖片的。
?python 示例代碼如下:
import pyrealsense2 as rs import numpy as np import cv2counter = 0 # 若要保存到其他文件夾,修改此路徑 folder = './camera_shot/' def shot(pos, frame):global counterpath = folder + pos + "_" + str(counter) + ".png"cv2.imwrite(path, frame)print("snapshot saved into: " + path)pipeline = rs.pipeline() config = rs.config() # 配置深度和顏色流 # 10、15或者30可選,20或者25會報錯,其他幀率未嘗試 # 配置顏色相機 config.enable_stream(rs.stream.color, 848, 480, rs.format.bgr8, 15) # 配置紅外相機 config.enable_stream(rs.stream.infrared, 1, 848, 480, rs.format.y8, 15) config.enable_stream(rs.stream.infrared, 2, 848, 480, rs.format.y8, 15) # 配置深度圖像 config.enable_stream(rs.stream.depth, 848, 480, rs.format.z16, 15) # Start streaming profile = pipeline.start(config)# 創建對齊對象, rs.align 允許我們將深度幀與其他幀對齊, "align_to" 是計劃對其深度幀的流類型 align_to = rs.stream.color align = rs.align(align_to)try:while True:frames = pipeline.wait_for_frames()# 將深度框與顏色框對齊aligned_frames = align.process(frames)# 獲取對齊幀aligned_depth_frame = aligned_frames.get_depth_frame()if not aligned_depth_frame:continuedepth_frame = 50*np.asanyarray(aligned_depth_frame.get_data())# 將深度圖轉化為偽彩色圖方便觀看depth_colormap = cv2.applyColorMap\(cv2.convertScaleAbs(depth_frame, alpha=0.008), cv2.COLORMAP_JET)# cv2.imshow('1 depth', depth_colormap)# color framescolor_frame = aligned_frames.get_color_frame()if not color_frame:continuecolor_frame = np.asanyarray(color_frame.get_data())# cv2.imshow('2 color', color_frame)# left framesleft_frame = frames.get_infrared_frame(1)if not left_frame:continueleft_frame = np.asanyarray(left_frame.get_data())cv2.imshow('3 left_frame', left_frame)# right framesright_frame = frames.get_infrared_frame(2)if not right_frame:continueright_frame = np.asanyarray(right_frame.get_data())cv2.imshow('4 right_frame', right_frame)c = cv2.waitKey(1)# 如果按下ESC則關閉窗口(ESC的ascii碼為27),同時跳出循環if c == 27:cv2.destroyAllWindows()breakif c == ord('t'):# 默認保存五張圖片,如果不需要保存某圖像把對應那行代碼注釋掉即可shot('left_shot', left_frame)shot('right_shot', right_frame)shot('color_shot', color_frame)shot('depth_colormap', depth_colormap)shot('depth_frame', depth_frame)counter += 1finally:# Stop streamingpipeline.stop()總結
以上是生活随笔為你收集整理的intel D435i 双目相机 拍摄图片并保存 python调用示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用Unity同时开发【微信小游戏】【安卓
- 下一篇: Python学习实验报告(1)