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

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


相关推荐

  • Windows驱动程序开发语言「建议收藏」

    Windows驱动程序开发语言「建议收藏」Windows驱动程序和Win32应用程序一样,都是PE格式,所以说,只要某种语言的编译器能够编译出PE格式的二进制格式文件,并且能够设置驱动程序的入口地址,那么这种语言就可以用来开发Windows驱动程序,所以可以选择C,C++,甚至是Delphi开发。但是由于微软提供的DDK开发环境中的头文件和链接库都是只支持C,C++。因此,大部分时候Windows驱动程序都是用C/C++进行开发的

    2022年10月9日
    2
  • matlab画三维图像函数_matlab怎么画图像

    matlab画三维图像函数_matlab怎么画图像PAT甲级1001.A+BFormat(20)题目原文:Calculatea+bandoutputthesuminstandardformat–thatis,thedigitsmustbeseparated…ASP.NETCore管道深度剖析(3…

    2022年10月10日
    3
  • 聚类分析在SPSS上的实现及分析「建议收藏」

    聚类分析在SPSS上的实现及分析「建议收藏」聚类分析在SPSS上的操作导入数据具体聚类分析操作第一步这里选择的应该是系统聚类,图上搞错了第二步2.1关于经济的自变量导入变量栏,地区导入个案标注数据第二步2.2聚类方法可以选择组间联接区间距离可以选择欧氏距离最后确定即可数据结果可视化及分析所得结果主要依赖于树状图1.分成两类,上海和其它地区2.分成三类,上海辽宁和其它地区3.分成四类,上海,辽宁,天津,和其它地区…

    2022年8月31日
    4
  • postgresql connection refused 5432 win10[通俗易懂]

    postgresql connection refused 5432 win10[通俗易懂]一个小问题困扰了我很久,最后解决了,可是具体问题在哪里我还是没明白。我使用的win10系统,之前eclipsejdbcpostgresql连接完全没有问题,有天发现屏幕下端的搜索框不能使用了,在网上找了解决方法,在powershell上重装了微软小娜,解决了这个搜索框不能使用的问题。可是后来发现eclipse使用jdbc一直连不上postgresql,报错java.net,Conn…

    2022年6月19日
    83
  • POI jar包下载

    POI jar包下载POIjar包下载

    2022年5月15日
    115
  • C# SqlTransaction的使用[通俗易懂]

    C# SqlTransaction的使用[通俗易懂]Sqltransaction是用在多sql任务写数据库时的Codeusing(SqlConnectionconn=newSqlConnection(SqlHelper.ConnectionString)){conn.Open();…

    2022年5月1日
    105

发表回复

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

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