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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 2021年度最受推荐的10本Python书籍

    2021年度最受推荐的10本Python书籍Python是一种通用的解释型编程,主要用于Web开发、机器学习和复杂数据分析。Python对初学者来说是一种完美的语言,因为它易于学习和理解,随着这种语言的普及,Python程序员的机会也越来越大。更多Python视频、源码、资料加群683380553免费获取如果你想学习Python编程,市场上就有很多的书籍。近日,hackr社区推荐了10本最佳的Python书籍。是2018年最受编…

    2022年6月14日
    47
  • linux 下一个 osw先从操作系统和标准脚本主动发起

    linux 下一个 osw先从操作系统和标准脚本主动发起

    2022年1月3日
    43
  • 杭电 1142 十字链表存储

    杭电 1142 十字链表存储  本来是想用二维数组实现的,但是想了一下发现,如果数据是稀疏矩阵的话,用二维数组存就会造成很多的空间浪费,而且遍历的时候也很浪费时间。学数据结构的时候书上教我们使用十字链表来存储稀疏矩阵,于是就想着用十字链表来实现。然后我发现我忘了十字链表的代码实现了…默默地去翻书,捣置了好久,终于写好了,乐滋滋的去oj提交代码,结果时间超限……  哎~把代码贴上来,就当加深一下十字链表的记忆吧~~#in…

    2022年6月18日
    24
  • JAVA中String的深入研究

    每次上网冲杯Java时,都能看到关于String无休无止的争论。还是觉得有必要让这个讨厌又很可爱的String美眉,赤裸裸的站在我们这些Java色狼面前了。嘿嘿….众所周知,String是由字符组成的串,在程序中使用频率很高。Java中的String是一个类,而并非基本数据类型。 不过她却不是普通的类哦!!! 【镜头1】 String对象的创建       1、关于

    2022年3月11日
    43
  • linux查看并杀死进程_ubuntu查看进程命令

    linux查看并杀死进程_ubuntu查看进程命令今天在netbeans中关闭webrick时,发现没有关闭掉,打入localhost:3000依然显示页面,发现无法从nb中再次关闭只有进入ubuntu的进程下关闭查看进程:1,ps-e命令2,feng@feng:~$sudonetstat-antupActiveInternetconnections(serversandestablished)ProtoRecv-QSe…

    2022年9月21日
    2
  • LoopBox 用于包装循环的盒子

    LoopBox 用于包装循环的盒子LoopBox 用于包装循环的盒子

    2022年4月22日
    54

发表回复

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

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