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/129870.html原文链接:https://javaforall.net

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


相关推荐

  • HTML+CSS制作二级菜单栏

    HTML+CSS制作二级菜单栏今天我们来练习一下二级菜单栏,说实话比较简单,但是自己一个人写的时候错误百出,逻辑混乱,于是乎网上找了几个案例,借鉴了一下思路,才整明白,鄙人确实不才,哈哈!效果图附上:首先:我已链接了外部样式重置,所以无需自己亲自写:reset.css网上有很多,我用的是下面这个,免费分享给大家,永久有效哦!链接:https://pan.baidu.com/s/1doPA17vy–Qt…

    2022年4月27日
    36
  • Http响应头里Cache-Control:no-cache、max-age=””和no-store

    Http响应头里Cache-Control:no-cache、max-age=””和no-store响应头:Cache-Control:no-cache,强制每次请求直接发送给源服务器,而不经过本地缓存版本的校验。这对于需要确认认证应用很有用(可以和public结合使用),或者严格要求使用最新数据 的应用(不惜牺牲使用缓存的所有好处)通俗解释:浏览器通知服务器,本地没有缓存数据//======================================================…

    2022年6月13日
    32
  • docker dockerfile详解_进入docker容器命令

    docker dockerfile详解_进入docker容器命令前言Dockerfile是一个用来构建镜像的文本文件,文本内容包含了一条条构建镜像所需的指令和说明。Dockerfile简介Dockerfile是用来构建Docker镜像的构建文件,是由一系列

    2022年7月28日
    9
  • docker 常用操作命令_docker命令大全

    docker 常用操作命令_docker命令大全Docker常用命令使用1容器生命周期管理1.1dockerrun1.2dockerstart/stop/restart1.3dockerkill1.4dockerpause/unpause1.5dockerexec1.6dockerrm2容器操作2.1dockerps2.2dockerinspect2.3dockerlogs-查看docker容器日志2.4dockerexport2.5dockerport3本地镜像管理3.1dockerima

    2022年9月17日
    2
  • JAVA下载文件代码「建议收藏」

    JAVA下载文件代码「建议收藏」publicstaticHttpServletResponsedownload(Stringpath,HttpServletResponseresponse,StringfileName){try{//path是指欲下载的文件的路径。Filefile=newFile(path);…

    2022年7月8日
    20
  • BP神经网络原理及matlab实例

    BP神经网络原理及matlab实例转载:http://blog.csdn.net/u013007900/article/details/50118945上一次我们讲了M-P模型,它实际上就是对单个神经元的一种建模,还不足以模拟人脑神经系统的功能。由这些人工神经元构建出来的网络,才能够具有学习、联想、记忆和模式识别的能力。BP网络就是一种简单的人工神经网络。 本文具体来介绍一下一种非常常见的神经网络模型——反

    2022年7月20日
    16

发表回复

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

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