关于文件存储:realsense深度图像读取对齐与保存

11次阅读

共计 2351 个字符,预计需要花费 6 分钟才能阅读完成。

图像采集

realsense 间接读取进去的彩色图片和深度图片是没有对齐的,读取进去的两张图片像素之间没有一一对应。然而个别应用两张图片是须要对齐的,并且间接利用深度信息。

以下程序为了更加不便的采集数据。
程序运行后 q 退出,s 保留图片。

import pyrealsense2 as rs
import numpy as np
import cv2
import time
import os

pipeline = rs.pipeline()

#Create a config 并配置要流​​式传输的管道
config = rs.config()
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)

profile = pipeline.start(config)

depth_sensor = profile.get_device().first_depth_sensor()
depth_scale = depth_sensor.get_depth_scale()
print("Depth Scale is:" , depth_scale)

align_to = rs.stream.color
align = rs.align(align_to)

# 依照日期创立文件夹
save_path = os.path.join(os.getcwd(), "out", time.strftime("%Y_%m_%d_%H_%M_%S", time.localtime()))
os.mkdir(save_path)
os.mkdir(os.path.join(save_path, "color"))
os.mkdir(os.path.join(save_path, "depth"))

# 保留的图片和实时的图片界面
cv2.namedWindow("live", cv2.WINDOW_AUTOSIZE)
cv2.namedWindow("save", cv2.WINDOW_AUTOSIZE)
saved_color_image = None # 保留的长期图片
saved_depth_mapped_image = None
saved_count = 0

# 主循环
try:
    while True:
        frames = pipeline.wait_for_frames()

        aligned_frames = align.process(frames)

        aligned_depth_frame = aligned_frames.get_depth_frame()
        color_frame = aligned_frames.get_color_frame()

        if not aligned_depth_frame or not color_frame:
            continue
        
        depth_data = np.asanyarray(aligned_depth_frame.get_data(), dtype="float16")
        depth_image = np.asanyarray(aligned_depth_frame.get_data())
        color_image = np.asanyarray(color_frame.get_data())
        depth_mapped_image = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)
        cv2.imshow("live", np.hstack((color_image, depth_mapped_image)))
        key = cv2.waitKey(30)

        # s 保留图片
        if key & 0xFF == ord('s'):
            saved_color_image = color_image
            saved_depth_mapped_image = depth_mapped_image

            # 彩色图片保留为 png 格局
            cv2.imwrite(os.path.join((save_path), "color", "{}.png".format(saved_count)), saved_color_image)
            # 深度信息由采集到的 float16 间接保留为 npy 格局
            np.save(os.path.join((save_path), "depth", "{}".format(saved_count)), depth_data)
            saved_count+=1
            cv2.imshow("save", np.hstack((saved_color_image, saved_depth_mapped_image)))

        # q 退出
        if key & 0xFF == ord('q') or key == 27:
            cv2.destroyAllWindows()
            break    
finally:
    pipeline.stop()

保留后的图像读取

import cv2
import numpy as np
import matplotlib.pyplot as plt

if __name__ == "__main__":
    color_image = cv2.imread("./0.png")
    depth_image = np.load("./0.npy")

    cv2.imshow("color", color_image)

    # 读取到的深度信息 /1000 为实在的深度信息,单位为 m
    # truth_depth = depth_image[x, y]/1000
    # 如果深度信息为 0, 则阐明没有获取到
    plt.imshow(depth_image.astype(np.int), "gray")
    plt.show()
    cv2.waitKey()
正文完
 0