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

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


相关推荐

  • 安全狗云备份客户端小版本更新v1.0.05502

    安全狗云备份客户端小版本更新v1.0.05502

    2021年8月25日
    60
  • awstats 安装

    awstats 安装来自http://www.cnblogs.com/fnng/archive/2012/08/31/2666175.htmlAwstats是一个非常简洁而且强大的统计工具。它可以统计您站点的如下信息:一:访问量,访问次数,页面浏览量,点击数,数据流量等精确到每月、每日、每小时的数据二:访问者国家、访问者IP、操作系统、浏览器等三:Robots/Spiders的统计四:…

    2022年7月16日
    13
  • Python中if __name__ == ‘__main__‘:的作用和原理「建议收藏」

    Python中if __name__ == ‘__main__‘:的作用和原理「建议收藏」if__name__==’__main__’:的作用一个python文件通常有两种使用方法,第一是作为脚本直接执行,第二是import到其他的python脚本中被调用(模块重用)执行。因此if__name__==’main’:的作用就是控制这两种情况执行代码的过程,在if__name__==’main’:下的代码只有在第一种情况下(即文件作为脚本直接执行)才会…

    2022年6月1日
    27
  • 手把手教你python画图(精简实例,一看就懂)

    手把手教你python画图(精简实例,一看就懂)1、不叨叨,直接上代码importmatplotlib.pyplotaspltx=[1,2,3,4,5]y=[0,3,2,7,9]plt.figure()plt.plot(x,y,’r-‘,lw=5)plt.show()2、结果图

    2022年6月2日
    42
  • centos7安装vim命令(vim命令怎么退出)

    那么如何安裝vim呢?输入rpm-qa|grepvim命令,如果vim已经正确安裝,会返回下面的三行代码:root@server1[~]#rpm-qa|grepvimvim-enhanced-7.0.109-7.el5vim-minimal-7.0.109-7.el5vim-common-7.0.109-7.el5

    2022年4月12日
    42
  • IDEA使用教程_超级cd使用教程

    IDEA使用教程_超级cd使用教程idea启动后会在cpan当前用户下生成一个C:\Users\Crystal.IntelliJIdea2018.1文件夹,这个文件夹里面有两个子文件夹config和system。删除这两个文件夹,idea在启动时候会重新配置。idea的project类似于eclipse的workspace;idea的modue类似于eclipse的project;配置都是在setti…

    2022年8月26日
    5

发表回复

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

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