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


相关推荐

  • console.writeline的功能(writeline用法)

    输入cw,然后按两次tab效果图

    2022年4月16日
    58
  • 编译原理文法详解_编译原理为什么存在递归文法

    编译原理文法详解_编译原理为什么存在递归文法引言学完了词法分析,我们知道词法分析器将正则表达式转换成词法单元流,但对于这个记号流我们不知道是否能由正确的文法产生,因此我们需要通过语法分析器来检测其合法性。语法分析器的输出是一棵语法分析树(无论显性还是隐性),并且进行一些语法纠错处理。语法分析的整个过程大概就是我们先定义一个语法,再用相应的算法来检测我们的词法单元流是否符合该语法。这里主要讨论上下文无关文法构成的语法和自顶向下、自底向上的语…

    2025年7月9日
    2
  • LSM树详解_黑龙江野生鱼品种

    LSM树详解_黑龙江野生鱼品种LSM树(Log-Structured-Merge-Tree)的名字往往会给初识者一个错误的印象,事实上,LSM树并不像B+树、红黑树一样是一颗严格的树状数据结构,它其实是一种存储结构,目前HBase,LevelDB,RocksDB这些NoSQL存储都是采用的LSM树。LSM树的核心特点是利用顺序写来提高写性能,但因为分层(此处分层是指的分为内存和文件两部分)的设计会稍微降低读性能,但是通过牺牲小部分读性能换来高性能写,使得LSM树成为非常流行的存储结构。1、LSM树的核心思想如上图所示,LSM树有

    2025年6月27日
    2
  • mysql优化的几种方法_sgd优化算法

    mysql优化的几种方法_sgd优化算法1.SGDBatchGradientDescent在每一轮的训练过程中,BatchGradientDescent算法用整个训练集的数据计算costfuction的梯度,并用该梯度对模型参数进行更新:Θ=Θ−α⋅▽ΘJ(Θ)\Theta=\Theta-\alpha\cdot\triangledown_\ThetaJ(\Theta)优点:costfuction若为凸函数,能

    2025年8月18日
    3
  • python基础知识点(精心整理)_python编程基础知识

    python基础知识点(精心整理)_python编程基础知识在Python里,标识符有字母、数字、下划线组成。在Python中,所有标识符可以包括英文、数字以及下划线(_),但不能以数字开头。Python中的标识符是区分大小写的。以下划线开头的标识符是有特殊意义的。以单下划线开头_foo的代表不能直接访问的类属性,需通过类提供的接口进行访问,不能用fromxxximport*而导入;以双下划线开头的__foo代表类的私有成员;以双下划线开头和结尾的foo代表Python里特殊方法专用的标识,如init()代表类的构造函

    2022年10月8日
    2
  • meta标签设置用极速模式打开网页

    meta标签设置用极速模式打开网页1浏览器集成了多种浏览器内核,需要强制使用极速模式<metaname=”renderer”content=”webkit”/>2meta标签中X-UA-Compatible属性的使用的极速模式<metahttp-equiv=”X-UA-Compatible”content=”IE=edge,chrome=1″/>…

    2025年6月13日
    4

发表回复

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

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