D415的使用
本文參考
【OpenCV 4】偽色彩 applyColorMap() 函數使用_kingkee的博客-CSDN博客_applycolormap函數
利用D415讀取 需要標記的人臉face_recognition的距離 Python + wind10_weixin_44576543的博客-CSDN博客
目錄
1.環境安裝
2.獲取雙目圖像
3.深度圖像的獲取
?
D415是深度圖像的攝像機
我們使用python進行驅動
1.環境安裝
python版本3.7,下面這些包使用pip都可以安裝上,我是使用conda創建的環境,可能有的與使用別的環境不太一樣
2.獲取雙目圖像
我們使用普通彩色圖像與紅外線圖像
import pyrealsense2 as rs import numpy as np import cv2pipeline = rs.pipeline() config = rs.config() config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30) config.enable_stream(rs.stream.infrared, 640, 480, rs.format.y8, 30) pipeline.start(config)try:while True:# Wait for a coherent pair of frames: depth and colorframes = pipeline.wait_for_frames()color_frame = frames.get_color_frame()depth_frame = frames.get_infrared_frame()# Convert images to numpy arrays 把圖像轉換為numpy datadepth_image = np.asanyarray(depth_frame.get_data())color_image = np.asanyarray(color_frame.get_data())color_image = cv2.cvtColor(color_image,cv2.COLOR_BGR2GRAY)# Stack both images horizontally 把兩個圖片水平拼在一起images = np.hstack((color_image, depth_image))# Show images 展示一下圖片cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)cv2.imshow('RealSense', images)key = cv2.waitKey(1)if key == ord(' '):breakfinally:# Stop streamingpipeline.stop()運行后的效果是這樣的,左側本來是彩色的圖像,我將其轉換為灰度圖像?
紅外線圖像中會有紋路一樣的東西
?當我用手擋住其中彩色的攝像頭,紅外線攝像頭不受影響
3.深度圖像的獲取
我使用了彩色原圖像與深度圖像進行對比,要不然效果不明顯
import pyrealsense2 as rs import numpy as np import cv2pipeline = rs.pipeline() config = rs.config() 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 colorframes = pipeline.wait_for_frames()depth_frame = frames.get_depth_frame()color_frame = frames.get_color_frame()if not depth_frame or not color_frame:continue# Convert images to numpy arrays 把圖像轉換為numpy datadepth_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 = cv2.blur(depth_colormap,(3,3))# Stack both images horizontally 把兩個圖片水平拼在一起images = np.hstack((color_image, depth_colormap))# Show images 展示一下圖片cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)cv2.imshow('RealSense', images)key = cv2.waitKey(1)if key == ord(' '):break finally:# Stop streamingpipeline.stop()- 代碼中的blur濾波不是官方的demo,是我后來加的,加入后感覺上可以識別到更短的距離?
這里使用到了偽彩色,如果不加入偽彩色的話,圖像是這樣的
偽彩色的用法可以參考?【OpenCV 4】偽色彩 applyColorMap() 函數使用_風語留痕-CSDN博客
深度圖像是兩個攝像頭的綜合結果,擋上兩個中的一個攝像頭,深度圖像就會消失
總結
- 上一篇: realsense D435 D435i
- 下一篇: 采用直线逼近方式的圆弧插补