图像采集

realsense直接读取出来的彩色图片和深度图片是没有对齐的,读取出来的两张图片像素之间没有一一对应。但是一般使用两张图片是需要对齐的,并且直接利用深度信息。

以下程序为了更加方便的采集数据。
程序运行后q退出,s保存图片。

import pyrealsense2 as rs
import numpy as np
import cv2
import time
import ospipeline = rs.pipeline()#Create a config并配置要流​​式传输的管道
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)profile = pipeline.start(config)depth_sensor = profile.get_device().first_depth_sensor()
depth_scale = depth_sensor.get_depth_scale()
print("Depth Scale is: " , depth_scale)align_to = rs.stream.color
align = rs.align(align_to)# 按照日期创建文件夹
save_path = os.path.join(os.getcwd(), "out", time.strftime("%Y_%m_%d_%H_%M_%S", time.localtime()))
os.mkdir(save_path)
os.mkdir(os.path.join(save_path, "color"))
os.mkdir(os.path.join(save_path, "depth"))# 保存的图片和实时的图片界面
cv2.namedWindow("live", cv2.WINDOW_AUTOSIZE)
cv2.namedWindow("save", cv2.WINDOW_AUTOSIZE)
saved_color_image = None # 保存的临时图片
saved_depth_mapped_image = None
saved_count = 0# 主循环
try:while True:frames = pipeline.wait_for_frames()aligned_frames = align.process(frames)aligned_depth_frame = aligned_frames.get_depth_frame()color_frame = aligned_frames.get_color_frame()if not aligned_depth_frame or not color_frame:continuedepth_data = np.asanyarray(aligned_depth_frame.get_data(), dtype="float16")depth_image = np.asanyarray(aligned_depth_frame.get_data())color_image = np.asanyarray(color_frame.get_data())depth_mapped_image = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)cv2.imshow("live", np.hstack((color_image, depth_mapped_image)))key = cv2.waitKey(30)# s 保存图片if key & 0xFF == ord('s'):saved_color_image = color_imagesaved_depth_mapped_image = depth_mapped_image# 彩色图片保存为png格式cv2.imwrite(os.path.join((save_path), "color", "{}.png".format(saved_count)), saved_color_image)# 深度信息由采集到的float16直接保存为npy格式np.save(os.path.join((save_path), "depth", "{}".format(saved_count)), depth_data)saved_count+=1cv2.imshow("save", np.hstack((saved_color_image, saved_depth_mapped_image)))# q 退出if key & 0xFF == ord('q') or key == 27:cv2.destroyAllWindows()break
finally:pipeline.stop()

保存后的图像读取

import cv2
import numpy as np
import matplotlib.pyplot as pltif __name__ == "__main__":color_image = cv2.imread("./0.png")depth_image = np.load("./0.npy")cv2.imshow("color", color_image)# 读取到的深度信息/1000 为真实的深度信息,单位为m# truth_depth = depth_image[x, y]/1000# 如果深度信息为0, 则说明没有获取到plt.imshow(depth_image.astype(np.int), "gray")plt.show()cv2.waitKey()如果深度信息为0, 则说明没有获取到plt.imshow(depth_image.astype(np.int), "gray")plt.show()cv2.waitKey()

realsense深度图像读取对齐与保存相关推荐

  1. realsense深度图像保存方法

    一般使用realsense时会保存视频序列,当保存深度图像时,需要注意保存的图像矩阵的格式,不然可能造成深度值的丢失. 在众多图像库中,一般会使用opencv中的imwrite() 函数进行深度图像的 ...

  2. Realsense SR300 和 R200 提取深度图像并保存

    本博客适用于SR300和R200的深度图像和RGB图像的提取. OpenCV的安装 首先要安装opencv,在这里不做多讲,网上有很多教程. SDK的安装 Realsense SR300 和 R200 ...

  3. Intel Realsense 深度流向彩色流对齐 color_aligned_to_depth和depth_aligned_to_color

    为啥要对齐 由于RGB图像数据与深度图像数据的空间坐标系是不同的,前者的原点是RGB摄像头,后者的原点是红外摄像头,因此两者会有相应的误差. 对齐原理 深度图上的2D点转换到世界坐标的3D点,世界坐标 ...

  4. C#使用EmguCV库(图像读取、显示、保存)(二)

    使用C#+EmguCV处理图像入门(图像读取_显示_保存)二 上个随笔已经介绍EmguCV的一些常用库和程序安装以及环境变量的配置,这次写的是如何使用这个类库对图像进行操作. EmguCV图像处理系统 ...

  5. OpenCV中保存不同深度图像的技巧

    什么是图像深度?   很多人开始学习OpenCV之后,接触的几个方法就包括imwrite函数,而且很快知道需要传入一个ndarray类型的mat对象作为实参,常规代码如下: imwrite(" ...

  6. 深度图像+rgb转化点云数据、点云数据打开、显示以及保存

    头文件 #include<iostream> #include <fstream> #include <stdio.h> #include <string.h ...

  7. realsense系列(一):快速查看realsense相机深度图像和RGB图像

    realsense系列[一]:快速查看realsense相机深度图像和RGB图像 0.本次任务 1.环境准备 2.查看相机内容 0.本次任务 使用realsenseviewer快速查看相机内容 1.环 ...

  8. ROS kinect:OpenNI读取深度图像与彩色图像

    参考:小斤的博客 1.创建你的包 catkin_create_pkg depth_rgb_image 2. 将以下代码粘贴到src/depth_rgb_image.cpp #include <s ...

  9. 图像读取、显示和保存

    使用opencv库进行演示 1. 图像读取 cv2.imread( ) 注:cv2.imread的返回结果是按照bgr顺序排列的 2. 图像显示 cv2.imshow(windowName,img ) ...

最新文章

  1. 初识C语言---(2)
  2. matlab shortest函数,MATLAB函數graphallshortestpaths不返回對稱矩陣
  3. activiti部署、执行,完成简单示例
  4. 数字信号处理中的归一化频率
  5. php 正三角塔,PHP 环境塔建与数据类型转换
  6. JavaFX其他事件
  7. LeetCode 530. 二叉搜索树的最小绝对差 思考分析
  8. android 后台耗时,android教程之使用asynctask在后台运行耗时任务
  9. FPGA _Verilog HDL_8位加法器设计实验
  10. Vagrant搭建虚拟化开发环境(五)虚拟机优化 PHP升级 打包分发
  11. 大萧条来临前的几大征兆
  12. 一键卡iPhoneQQ在线
  13. android 401,Android HttpClient身份验证始终返回401代码
  14. 记一篇IT培训日记005-Hello Java
  15. vba批量合并指定的sheet_利用VBA实现多个Excel工作簿快速合并方法
  16. Proteus 8.12 仿真软件安装
  17. STM32要按复位键才能下载问题解决
  18. 微服务与Spring Cloud简介
  19. mysql5.7.23绿色版安装
  20. 测试用例支持思维导图编辑模式,MeterSphere开源持续测试平台v1.8.0发布丨Release Notes

热门文章

  1. Java Map排序详解
  2. fegin需要实现类_Fegin
  3. 图书管理系统(C语言)
  4. Vim教程2 在文件中的移动
  5. javascript深拷贝
  6. 设计模式——(12)组合模式
  7. 服务器系统镜像怎么安装系统,云服务器镜像安装系统
  8. 使用Excel VBA查找最后一行
  9. win10写java工具_jdk环境变量一键配置工具(Win10可用)
  10. Fireworks(whole page)