深度相机的图像深度实时显示

最近有了一个intel 的 D435i 深度相机,网上找了一个实时显示图片和深度图的python 程序,最开始是一个博客,后来发现在intel 的github里也有。都找不到链接了,觉得不错,修改整理成本文。

首先要安装好pyrealsense2 ,链接是: realsense.intel.com/get-started

官网提供的软件工具与文档资料介绍
https://realsense.intel.com/intel-realsense-downloads/#firmware
1、Intel RealSense Viewer.exe 是最主要的软件,功能是查看查看视频流,并可以对视频流进行后期处理,这个官方GitHub有提供完整的c++工程,编译了可以直接运行;
2、Depth Quality Tool for Intel RealSense Cameras.exe 用于测试z方向的深度数据是否准,只需要把rgbd相机正对着白色的墙面等就可以了,然后对比卷尺量的距离与软件上显示的距离,来判断是否需要校准相机;
3、Intel RealSense D400 Series Dynamic Calibration Software Tools这个是校准工具,如果前面计算的距离不准,可以利用这个软件进行校准,在苹果或安卓手机搜realsense这个软件,安装打开,就是显示一张类似棋盘格标定板的图片,然后配合校准工具,按照上面的提示晃动手机即可校准,输出外参R\T。

下面是实时显示图像和深度的python 代码:

请注意,深度是转换成图片显示的。

import pyrealsense2 as rs
import numpy as np
import cv2

# Configure depth and color streams
pipeline = rs.pipeline()
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)
# Start streaming
pipeline.start(config)
try:
    while True:
        # Wait for a coherent pair of frames: depth and color
        frames = pipeline.wait_for_frames()
        depth_frame = frames.get_depth_frame()
        color_frame = frames.get_color_frame()
        if not depth_frame or not color_frame:
            continue
        # Convert images to numpy arrays

        depth_image = np.asanyarray(depth_frame.get_data())

        color_image = np.asanyarray(color_frame.get_data())

        # Apply colormap on depth image (image must be converted to 8-bit per pixel first)
        depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)
        # Stack both images horizontally
        images = np.hstack((color_image, depth_colormap))
        # Show images
        cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)
        cv2.imshow('RealSense', images)
        key = cv2.waitKey(1)
        # Press esc or 'q' to close the image window
        if key & 0xFF == ord('q') or key == 27:
            cv2.destroyAllWindows()
            break
        elif key & 0xFF == ord('s'):
            cv2.imwrite('savefile.jpg',images)
finally:
    # Stop streaming
    pipeline.stop()

运行的效果如下(左边,图片,右边,深度图片):

如何把这2张图片转换为3D呢?

首先把这2张图片,分别存在各自的一个文件里,然后参考我的另一博文: 使用opencv以及pcl将2D图像转换为3D点云

如何检测图中物体的深度或者坐标呢?

先要了解相机的基本参数如下(我上文c++代码):

// 相机内参,这个参数不是D435i 的,仅仅做个参考
const double camera_factor = 1000;
const double camera_cx = 325.5;
const double camera_cy = 253.5;
const double camera_fx = 518.0;
const double camera_fy = 519.0;

然后呢,就可以计算了(我上文c++代码):

// 获取深度图中(m,n)处的值
ushort d = depth.ptr<ushort>(m)[n];
// d 可能没有值,若如此,跳过此点
if (d == 0)continue; //说明这点没有深度值

// 计算这个点的空间坐标
p.z = double(d) / camera_factor;
p.x = (n - camera_cx) * p.z / camera_fx;
p.y = (m - camera_cy) * p.z / camera_fy;

也可以参考 https://github.com/IntelRealSense/librealsense/blob/jupyter/notebooks/distance_to_object.ipynb

或者等待我的下一个博客。

发布了131 篇原创文章 · 获赞 112 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/leon_zeng0/article/details/103209251