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


相关推荐

  • elasticsearch集群安装插件滚动重启步骤「建议收藏」

    elasticsearch集群安装插件滚动重启步骤「建议收藏」elasticsearch集群安装插件滚动重启步骤1.先安装好插件(每个节点都要装,root用户执行)2.禁用分片分配。这可以防止Elasticsearch重新平衡(reblance)丢失的分片,可以按如下方式禁用分配3.为了提升集群恢复速度,尽可能先让数据刷盘.如果有任何挂起(pending状态)的索引操作,这个操作将会失败,但可以安全地多次重新执行.如果应用有数据写入最好先暂时停止数据写入4.重启节点确认是否已经加入集群5.重新启用分片分配,分片再平衡可能需要一些时间。等待群集恢复到green状态后再继

    2025年8月3日
    4
  • Using MSAgent to Scan the Start Menu 选择自 wm_ni 的 Blog

    Using MSAgent to Scan the Start Menu 选择自 wm_ni 的 BlogNotethiscodewillignoreduplicateshortcuts.ForexampleIhave4or5shortcutsinmyStartMenuthatarenamed”Readme.txt.”Onlythefirstinstanceofthesewillgetaddedtothecommandsallot

    2022年6月21日
    23
  • Ajax获取 Request 对象

    Ajax获取 Request 对象获取Request对象有了上面的基础知识后,我们来看看一些具体的例子。XMLHttpRequest 是Ajax应用程序的核心,而且对很多读者来说可能还比较陌生,我们就从这里开始吧。从 清单1 可以看出,创建和使用这个对象非常简单,不是吗?等一等。还记得几年前的那些讨厌的浏览器战争吗?没有一样东西在不同的浏览器上得到同样的结果。不管您是否相信,这些战争仍然在继续,虽

    2022年5月2日
    48
  • VMware12安装_vmwarewin10安装

    VMware12安装_vmwarewin10安装vmware10安装fedora1232位安装这个链接安装可行https://blog.csdn.net/Babsir/article/details/78014042

    2025年12月3日
    6
  • redis和zk实现分布式锁有什么区别_redis分布式锁和zk分布式锁区别

    redis和zk实现分布式锁有什么区别_redis分布式锁和zk分布式锁区别前言本文介绍下分布式锁的一个使用场景分享本文的缘由是因为今天在写代码时需要处理一个原子性问题,场景是:业务功能需要先查询数据,再根据数据判断是否要更新数据,在这个查询+更新的过程必然会存在高并发下的原子性问题那么如何解决这个问题呢,那么就要说到我们的主角:分布式锁了分布式锁介绍分布式锁:即在多集群多节点环境下确保只有一个线程可以拿到锁,防止并发出现的问题,类似于synchronized,只不过synchronized不能处理多节点的问题解决上述问题的一种解决方式就是使用分布式锁,虽然性能会比较低

    2025年10月2日
    5
  • 2021最强Python学习教程,从零基础入门到精通

    2021最强Python学习教程,从零基础入门到精通你准备好了吗???areyouready???前言01.python介绍02.项目开发完整流程(详解版)03.项目开发流程(精简版)第一篇计算机核心基础01计算机组成原理第二篇编程语言01编程语言介绍第三篇python入门01python介绍及IDE集成开发环境02python是解释型的强类型动态语言03python语法之变量、常量04python语法之注释05python垃圾回收机制GC06Python语法入门之基本数据类型07Python语法

    2026年1月26日
    4

发表回复

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

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