realsense深度图像读取对齐与保存

图像采集

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, 则说明没有获取到
    plt.imshow(depth_image.astype(np.int), "gray")
    plt.show()
    cv2.waitKey()
OpenCV是一个开源的计算机视觉库,可以用于处理图像和视频数据,包括读取和写入图像数据。 realsense d435i是英特尔推出的一款深度相机,可以获取深度信息和RGB图像。 要读取realsense d435i的RGB图像,可以使用OpenCV中的cv::VideoCapture类来实现。首先,需要安装realsense SDK并配置环境,然后在代码中初始化VideoCapture对象,并设置参数。 下面是一个简单的示例代码: ``` #include <opencv2/opencv.hpp> #include <librealsense2/rs.hpp> using namespace cv; using namespace rs2; int main(int argc, char* argv[]) { // Declare RealSense pipeline, encapsulating the actual device and sensors pipeline pipe; // Start streaming with default configuration pipe.start(); // Create a VideoCapture object to read from the RealSense camera VideoCapture cap(pipe); // Check if camera opened successfully if (!cap.isOpened()) { std::cout << "Error opening video stream or file" << std::endl; return -1; } // Read and display frames from camera while (1) { Mat frame; // Capture frame-by-frame cap >> frame; // If the frame is empty, break immediately if (frame.empty()) break; // Display the resulting frame imshow("RGB Image", frame); // Press Esc on keyboard to exit if (waitKey(1) == 27) break; } // When everything done, release the video capture object cap.release(); // Closes all the frames destroyAllWindows(); return 0; } ``` 这个程序中,我们首先声明一个RealSense pipeline对象pipe,然后启动流水线。接着我们创建一个VideoCapture对象cap,并将pipe传递给它以读取RGB图像。最后,在while循环中读取每一帧图像并显示它们。
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ximikang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值