realsense深度图像保存方法

realsense深度图像保存方法一般使用realsense时会保存视频序列,当保存深度图像时,需要注意保存的图像矩阵的格式,不然可能造成深度值的丢失。在众多图像库中,一般会使用opencv中的imwrite()函数进行深度图像的保存。一般深度图像中深度值的单位是mm,因此一般使用np.uint16作为最终数据格式保存。例子:importnumpyasnpimportcv2deffun1(…

大家好,又见面了,我是你们的朋友全栈君。

  1. 一般使用realsense时会保存视频序列,当保存深度图像时,需要注意保存的图像矩阵的格式,不然可能造成深度值的丢失。

  2. 在众多图像库中,一般会使用opencv中的imwrite() 函数进行深度图像的保存。

  3. 一般深度图像中深度值的单位是mm,因此一般使用np.uint16作为最终数据格式保存。

例子:

import numpy as np
import cv2

def fun1(im):
	im=np.asarray(im,np.float32)
	return im
def fun2(im):
	im=np.asarray(im,np.uint16)
	return im
if __name__ == '__main__':
	#set a depth map using np.random
	im=np.random.randint(100,800,size=(96,96))
	#1. float save
	im1=fun1(im)
	cv2.imwrite('float_saved.png',im1)
	im2=fun2(im)
	cv2.imwrite('uint_saved.png',im2)
	

重新读取保存的图像:

import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
def load_image(filename):
	im=Image.open(filename)
	return im
if __name__ == '__main__':
	im1=load_image('float_saved.png')
	im2=load_image('uint_saved.png')
	plt.subplot(121)
	plt.imshow(im1)
	plt.subplot(122)
	plt.imshow(im2)
	plt.show()

结果显示:
左边是float,右边是uint16保存方法,左边数据出现了数据压缩,被压缩在0-255之间,而右边值正常。
在这里插入图片描述
附上完整的realsense采集深度图像的代码

import pyrealsense2 as rs
import numpy as np
import cv2


class realsense_im(object):
    def __init__(self,image_size=(640,480)):
        self.pipeline = rs.pipeline()
        config = rs.config()
        config.enable_stream(rs.stream.depth, image_size[0], image_size[1], rs.format.z16, 30)
        config.enable_stream(rs.stream.color, image_size[0], image_size[1], rs.format.bgr8, 30)
        self.profile = self.pipeline.start(config)

    def __get_depth_scale(self):
        depth_sensor = self.profile.get_device().first_depth_sensor()

        depth_scale = depth_sensor.get_depth_scale()

        return depth_scale

    def get_image(self):

        frames = self.pipeline.wait_for_frames()
        depth_frame = frames.get_depth_frame()
        color_frame = frames.get_color_frame()
        depth_image = np.asarray(depth_frame.get_data(), dtype=np.float32)
        color_image = np.asarray(color_frame.get_data(), dtype=np.uint8)
        color_image_pad = np.pad(color_image, ((20, 0), (0, 0), (0, 0)), "edge")
        depth_map_end = depth_image * self.__get_depth_scale() * 1000
        return depth_map_end,color_image

    def process_end(self):
        self.pipeline.stop()

rs_t=realsense_im()

i=0
try:
    while True:

        depth_map,rgb_map=rs_t.get_image()
        print  rgb_map.shape
        cv2.imwrite('./examples/savefig/rgb/image_r_{}.png'.format(str(i).zfill(5)), rgb_map)
        i+=1

        cv2.imwrite('./examples/savefig/depth/Tbimage_d_{}.png'.format(str(0).zfill(5)), np.asarray(depth_map,np.uint16))

        cv2.namedWindow('RGB Example', cv2.WINDOW_AUTOSIZE)
        cv2.imshow('RGB Example', rgb_map)
        key = cv2.waitKey(1)
        # Press esc or 'q' to close the image window
        if key & 0xFF == ord('q') or key == 27:
            cv2.destroyAllWindows()
            break

finally:
    pass


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/129861.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • apache 虚拟主机配置详解_linux 配置虚拟主机

    apache 虚拟主机配置详解_linux 配置虚拟主机文章目录Apache虚拟主机企业应用部署一个端口不同域名调试worker工作模式进行压力测试权限设置Apache配置文件详解ApacheRewrite规则讲解Apache配置文件权限操作总结Apache虚拟主机企业应用企业真实环境中,一台WEB服务器发布单个网站会非常浪费资源,所以一台WEB服务器上会发布多个网站,少则3-5个,多则2-30。在一台服务器上发布多哥网站,也称之为部署多个虚拟主机,WEB虚拟主机配置方法有三种:基于单个IP地址多个Socket端口基于

    2022年9月17日
  • softreference 回收_method verification

    softreference 回收_method verification一、softReference的作用    SoftReference的主要特点就是在当内存不够的时候,GC会回收SoftReference所引用的对象。所以,在memorysensitive的项目中将某些数据设置成SoftReference可以避免内存的溢出。1、创建一个SoftReferenceSoftReference>softReference=newSof

    2022年9月8日
  • java遍历List

    初始数据,例如DAO层返回的商品列表List,如下:List<Goods>list=goodMapper.getGoodsBySid(sid);1、迭代器遍历,方便删除元素Iterator<Goods>iterator=list.iterator();while(iterator.hasNext()){ Goodsgood=it…

    2022年4月4日
  • 通俗、有逻辑的写一篇说下Xgboost的原理,供讨论参考

    通俗、有逻辑的写一篇说下Xgboost的原理,供讨论参考初看Xgboost,翻了多篇博客发现关于xgboost原理的描述实在难以忍受,缺乏逻辑性,写一篇供讨论。——以下是抛砖引玉。观其大略,而后深入细节,一开始扎进公式反正我是觉得效率不高,还容易打消人的积极性。首先说下决策树决策树是啥?举个例子,有一堆人,我让你分出男女,你依靠头发长短将人群分为两拨,长发的为“女”,短发为“男”,你是不是依靠一个指标“头发长短”将人群进行了划分,你就形成了一个

    2022年5月7日
  • Python获取Websocket接口的数据

    Python获取Websocket接口的数据作者:小小明在前面的用Tornado实现web聊天室一文中介绍了python实现websocket的方法,这篇文章将要分享如何用python作为客户端获取websocket接口的数据。前文链接:https://blog.csdn.net/as604049322/article/details/112386560websocket的使用WebSocket是一种在单个TCP/TSL连接上,进行全双工、双向通信的协议。WebSocket可以让客户端与服务器之间的数据交换变得更加简单高效,服务端.

    2022年7月15日
  • haxm failed to open driver

    haxm failed to open driver今天在新的PC机上,安装android环境。PC的配置是CPU:i5,8G内存。在装intelhaxm-android.exe时候出错(见下图)。尝试:首先,查看了BIOS中对VT的支持,确定是支持并且开启状态。然后在网上试了安装haxm_extra_workaround.zip的方式,仍然不起作用。解决办法:重新安装另外一个版本的HAXM,见网址。安装后,成功解决。

    2022年6月28日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号