Intel Realsense D435 opencv 为什么将color图转换成灰度图后,再与depth图水平堆叠,其结果一片黑色?(数据未map到0-255)
生活随笔
收集整理的這篇文章主要介紹了
Intel Realsense D435 opencv 为什么将color图转换成灰度图后,再与depth图水平堆叠,其结果一片黑色?(数据未map到0-255)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
相關代碼
# -*- coding: utf-8 -*- """ @File : obstacle_detection.py @Time : 2019/12/11 10:24 @Author : Dontla @Email : sxana@qq.com @Software: PyCharm """ import timeimport numpy as np import pyrealsense2 as rs import cv2 import sysclass ObstacleDetection(object):def __init__(self):self.cam_serials = ['838212073161', '827312071726']def obstacle_detection(self):# 攝像頭個數(在這里設置所需使用攝像頭的總個數)cam_num = 6ctx = rs.context()'''連續驗證機制'''# D·C 1911202:創建最大驗證次數max_veri_times;創建連續穩定值continuous_stable_value,用于判斷設備重置后是否處于穩定狀態max_veri_times = 100continuous_stable_value = 10print('\n', end='')print('開始連續驗證,連續驗證穩定值:{},最大驗證次數:{}:'.format(continuous_stable_value, max_veri_times))continuous_value = 0veri_times = 0while True:devices = ctx.query_devices()connected_cam_num = len(devices)if connected_cam_num == cam_num:continuous_value += 1if continuous_value == continuous_stable_value:breakelse:continuous_value = 0veri_times += 1if veri_times == max_veri_times:print("檢測超時,請檢查攝像頭連接!")sys.exit()print('攝像頭個數:{}'.format(connected_cam_num))'''循環reset攝像頭'''# hardware_reset()后是不是應該延遲一段時間?不延遲就會報錯print('\n', end='')print('開始初始化攝像頭:')for dev in ctx.query_devices():# 先將設備的序列號放進一個變量里,免得在下面for循環里訪問設備的信息過多(雖然不知道它會不會每次都重新訪問)dev_serial = dev.get_info(rs.camera_info.serial_number)# 匹配序列號,重置我們需重置的特定攝像頭(注意兩個for循環順序,哪個在外哪個在內很重要,不然會導致剛重置的攝像頭又被訪問導致報錯)for serial in self.cam_serials:if serial == dev_serial:dev.hardware_reset()# 像下面這條語句居然不會報錯,不是剛剛才重置了dev嗎?莫非區別在于沒有通過for循環ctx.query_devices()去訪問?# 是不是剛重置后可以通過ctx.query_devices()去查看有這個設備,但是卻沒有存儲設備地址?如果是這樣,# 也就能夠解釋為啥能夠通過len(ctx.query_devices())函數獲取設備數量,但訪問序列號等信息就會報錯的原因了print('攝像頭{}初始化成功'.format(dev.get_info(rs.camera_info.serial_number)))'''連續驗證機制'''# D·C 1911202:創建最大驗證次數max_veri_times;創建連續穩定值continuous_stable_value,用于判斷設備重置后是否處于穩定狀態print('\n', end='')print('開始連續驗證,連續驗證穩定值:{},最大驗證次數:{}:'.format(continuous_stable_value, max_veri_times))continuous_value = 0veri_times = 0while True:devices = ctx.query_devices()connected_cam_num = len(devices)if connected_cam_num == cam_num:continuous_value += 1if continuous_value == continuous_stable_value:breakelse:continuous_value = 0veri_times += 1if veri_times == max_veri_times:print("檢測超時,請檢查攝像頭連接!")sys.exit()print('攝像頭個數:{}'.format(connected_cam_num))'''配置各個攝像頭的基本對象'''pipeline1 = rs.pipeline(ctx)pipeline2 = rs.pipeline(ctx)config1 = rs.config()config2 = rs.config()config1.enable_device(self.cam_serials[0])config2.enable_device(self.cam_serials[1])config1.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)config2.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)config1.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)config2.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)pipeline1.start(config1)pipeline2.start(config2)align1 = rs.align(rs.stream.color)align2 = rs.align(rs.stream.color)'''運行攝像頭'''try:while True:frames1 = pipeline1.wait_for_frames()# print(np.asanyarray(frames1.get_depth_frame().get_data()).shape) # (480, 640)frames2 = pipeline2.wait_for_frames()aligned_frames1 = align1.process(frames1)aligned_frames2 = align2.process(frames2)aligned_depth_frame1 = aligned_frames1.get_depth_frame()# print(np.asanyarray(aligned_depth_frame1.get_data()).shape) # (480, 640)aligned_depth_frame2 = aligned_frames2.get_depth_frame()color_frame1 = aligned_frames1.get_color_frame()color_frame2 = aligned_frames2.get_color_frame()if not aligned_depth_frame1 \or not color_frame1 \or not aligned_depth_frame2 \or not color_frame2:continuecolor_profile1 = color_frame1.get_profile()color_profile2 = color_frame2.get_profile()cvsprofile1 = rs.video_stream_profile(color_profile1)cvsprofile2 = rs.video_stream_profile(color_profile2)color_intrin1 = cvsprofile1.get_intrinsics()color_intrin2 = cvsprofile2.get_intrinsics()color_intrin_part1 = [color_intrin1.ppx, color_intrin1.ppy, color_intrin1.fx, color_intrin1.fy]color_intrin_part2 = [color_intrin2.ppx, color_intrin2.ppy, color_intrin2.fx, color_intrin2.fy]color_image1 = np.asanyarray(color_frame1.get_data())# 【191216 轉換成灰度圖并二值化】color_image1_gray = cv2.cvtColor(color_image1, cv2.COLOR_BGR2GRAY)# print(color_image1_gray.shape) # (480, 640)print(color_image1_gray)color_image2 = np.asanyarray(color_frame2.get_data())depth_image1 = np.asanyarray(aligned_depth_frame1.get_data())# print(depth_image1.shape) # (480, 640)print(depth_image1)depth_image2 = np.asanyarray(aligned_depth_frame2.get_data())# depth_colormap1 = cv2.applyColorMap(cv2.convertScaleAbs(depth_image1, alpha=0.0425), cv2.COLORMAP_JET)depth_colormap2 = cv2.applyColorMap(cv2.convertScaleAbs(depth_image2, alpha=0.03), cv2.COLORMAP_JET)# image1 = np.hstack((color_image1, depth_colormap1))# print(depth_image1.shape) # (480, 640)image1 = np.hstack((color_image1_gray, depth_image1))# print(image1.shape) # (480, 1280)image2 = np.hstack((color_image2, depth_colormap2))# cv2.imshow('win1', image1)cv2.imshow('win1', image1)cv2.imshow('win2', image2)cv2.waitKey(1)finally:pipeline1.stop()pipeline2.stop()if __name__ == '__main__':ObstacleDetection().obstacle_detection()說明
color圖轉換成灰度圖后其shape為(480, 640),depth圖的shape也是(480, 640),按理說水平堆疊不會出問題。
打印數據后才發現,原來depth中的數據沒有被map到[0, 255],導致cv2.imshow()函數自動map堆疊后的數據了(估計是將最大的值對應于255,然后得出比例系數,然后將所有數據乘以這個比例系數,所以這就是為啥灰度圖沒法正常顯示了),所以打印成了下面這樣:
解決辦法
手動將depth數據map到[0,255]:
好了:
修改后代碼
# -*- coding: utf-8 -*- """ @File : obstacle_detection.py @Time : 2019/12/11 10:24 @Author : Dontla @Email : sxana@qq.com @Software: PyCharm """ import timeimport numpy as np import pyrealsense2 as rs import cv2 import sysclass ObstacleDetection(object):def __init__(self):self.cam_serials = ['838212073161', '827312071726']def obstacle_detection(self):# 攝像頭個數(在這里設置所需使用攝像頭的總個數)cam_num = 6ctx = rs.context()'''連續驗證機制'''# D·C 1911202:創建最大驗證次數max_veri_times;創建連續穩定值continuous_stable_value,用于判斷設備重置后是否處于穩定狀態max_veri_times = 100continuous_stable_value = 10print('\n', end='')print('開始連續驗證,連續驗證穩定值:{},最大驗證次數:{}:'.format(continuous_stable_value, max_veri_times))continuous_value = 0veri_times = 0while True:devices = ctx.query_devices()connected_cam_num = len(devices)if connected_cam_num == cam_num:continuous_value += 1if continuous_value == continuous_stable_value:breakelse:continuous_value = 0veri_times += 1if veri_times == max_veri_times:print("檢測超時,請檢查攝像頭連接!")sys.exit()print('攝像頭個數:{}'.format(connected_cam_num))'''循環reset攝像頭'''# hardware_reset()后是不是應該延遲一段時間?不延遲就會報錯print('\n', end='')print('開始初始化攝像頭:')for dev in ctx.query_devices():# 先將設備的序列號放進一個變量里,免得在下面for循環里訪問設備的信息過多(雖然不知道它會不會每次都重新訪問)dev_serial = dev.get_info(rs.camera_info.serial_number)# 匹配序列號,重置我們需重置的特定攝像頭(注意兩個for循環順序,哪個在外哪個在內很重要,不然會導致剛重置的攝像頭又被訪問導致報錯)for serial in self.cam_serials:if serial == dev_serial:dev.hardware_reset()# 像下面這條語句居然不會報錯,不是剛剛才重置了dev嗎?莫非區別在于沒有通過for循環ctx.query_devices()去訪問?# 是不是剛重置后可以通過ctx.query_devices()去查看有這個設備,但是卻沒有存儲設備地址?如果是這樣,# 也就能夠解釋為啥能夠通過len(ctx.query_devices())函數獲取設備數量,但訪問序列號等信息就會報錯的原因了print('攝像頭{}初始化成功'.format(dev.get_info(rs.camera_info.serial_number)))'''連續驗證機制'''# D·C 1911202:創建最大驗證次數max_veri_times;創建連續穩定值continuous_stable_value,用于判斷設備重置后是否處于穩定狀態print('\n', end='')print('開始連續驗證,連續驗證穩定值:{},最大驗證次數:{}:'.format(continuous_stable_value, max_veri_times))continuous_value = 0veri_times = 0while True:devices = ctx.query_devices()connected_cam_num = len(devices)if connected_cam_num == cam_num:continuous_value += 1if continuous_value == continuous_stable_value:breakelse:continuous_value = 0veri_times += 1if veri_times == max_veri_times:print("檢測超時,請檢查攝像頭連接!")sys.exit()print('攝像頭個數:{}'.format(connected_cam_num))'''配置各個攝像頭的基本對象'''pipeline1 = rs.pipeline(ctx)pipeline2 = rs.pipeline(ctx)config1 = rs.config()config2 = rs.config()config1.enable_device(self.cam_serials[0])config2.enable_device(self.cam_serials[1])config1.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)config2.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)config1.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)config2.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)pipeline1.start(config1)pipeline2.start(config2)align1 = rs.align(rs.stream.color)align2 = rs.align(rs.stream.color)'''運行攝像頭'''try:while True:frames1 = pipeline1.wait_for_frames()# print(np.asanyarray(frames1.get_depth_frame().get_data()).shape) # (480, 640)frames2 = pipeline2.wait_for_frames()aligned_frames1 = align1.process(frames1)aligned_frames2 = align2.process(frames2)aligned_depth_frame1 = aligned_frames1.get_depth_frame()# print(np.asanyarray(aligned_depth_frame1.get_data()).shape) # (480, 640)aligned_depth_frame2 = aligned_frames2.get_depth_frame()color_frame1 = aligned_frames1.get_color_frame()color_frame2 = aligned_frames2.get_color_frame()if not aligned_depth_frame1 \or not color_frame1 \or not aligned_depth_frame2 \or not color_frame2:continuecolor_profile1 = color_frame1.get_profile()color_profile2 = color_frame2.get_profile()cvsprofile1 = rs.video_stream_profile(color_profile1)cvsprofile2 = rs.video_stream_profile(color_profile2)color_intrin1 = cvsprofile1.get_intrinsics()color_intrin2 = cvsprofile2.get_intrinsics()color_intrin_part1 = [color_intrin1.ppx, color_intrin1.ppy, color_intrin1.fx, color_intrin1.fy]color_intrin_part2 = [color_intrin2.ppx, color_intrin2.ppy, color_intrin2.fx, color_intrin2.fy]color_image1 = np.asanyarray(color_frame1.get_data())# 【191216 轉換成灰度圖并二值化】color_image1_gray = cv2.cvtColor(color_image1, cv2.COLOR_BGR2GRAY)color_image2 = np.asanyarray(color_frame2.get_data())depth_image1 = np.asanyarray(aligned_depth_frame1.get_data())depth_image1_convertScaleAbs = cv2.convertScaleAbs(depth_image1, alpha=0.03)depth_image2 = np.asanyarray(aligned_depth_frame2.get_data())depth_colormap1 = cv2.applyColorMap(cv2.convertScaleAbs(depth_image1, alpha=0.0425), cv2.COLORMAP_JET)depth_colormap2 = cv2.applyColorMap(cv2.convertScaleAbs(depth_image2, alpha=0.03), cv2.COLORMAP_JET)# image1 = np.hstack((color_image1, depth_colormap1))image1 = np.hstack((color_image1_gray, depth_image1_convertScaleAbs))image2 = np.hstack((color_image2, depth_colormap2))cv2.imshow('win1', image1)cv2.imshow('win2', image2)cv2.waitKey(1)finally:pipeline1.stop()pipeline2.stop()if __name__ == '__main__':ObstacleDetection().obstacle_detection()總結
以上是生活随笔為你收集整理的Intel Realsense D435 opencv 为什么将color图转换成灰度图后,再与depth图水平堆叠,其结果一片黑色?(数据未map到0-255)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python numpy hstack(
- 下一篇: Intel Realsense D435