【问题标题】:Exact depth distance from Realsense D435 with X,Y coordinates带有 X、Y 坐标的 Realsense D435 的精确深度距离
【发布时间】:2019-03-11 15:52:31
【问题描述】:

我想用检测对象的 x,y 坐标计算深度距离。

在下图中,我在 opencv 中使用背景减法来检测和跟踪进入相机视图的新对象。我能够相对轻松地获得 x,y 坐标,但无法使用 realsense sdk 获得 z 深度。我是否可以以任何方式将 x,y 坐标输入到 realsense sdk 并从那里获取 z 深度?

我正在使用 opencv python 和 realsense sdk 2 作为参考。

Getting the z depth in the midpoint of the bounding box

import numpy as np
import cv2 as cv
import pyrealsense2 as rs

# Create a pipeline
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.depth, 1280, 720, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 1280, 720, rs.format.bgr8, 30)

#Start pipeline
profile = pipeline.start(config)

erodeKernel = cv.getStructuringElement(cv.MORPH_RECT, (5,5))

fgbg = cv.createBackgroundSubtractorMOG2()
while True:
    frames = pipeline.wait_for_frames()
    depth_frame = frames.get_depth_frame()
    colour_frame = frames.get_color_frame()

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

    # Apply colormap on depth image (image must be converted to 8-bit per pixel first)
    depth_colormap = cv.applyColorMap(cv.convertScaleAbs(depth_image, alpha=0.03), cv.COLORMAP_JET)

    blur = cv.GaussianBlur(color_image,(5,5),0)
    fgmask = fgbg.apply(blur)

    im2, contours, hierarchy = cv.findContours(fgmask.copy(), cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
    for c in contours:
        if cv.contourArea(c) < 200:
            continue

        (x,y,w,h) = cv.boundingRect(c)
        cv.rectangle(color_image, (x,y), (x + w, y + h), (0,255,0), 2)

    cv.imshow('RealSense', color_image)
    cv.imshow("Depth", depth_colormap)
    cv.imshow('Mask', fgmask)
    if cv.waitKey(25) == ord('q'):
        break
cv.destroyAllWindows()
pipeline.stop()

【问题讨论】:

  • 你试过这个answer吗?还有一个很好的github链接
  • @Antonino 感谢您的回复。我找到了解决方案,但我也会检查您的建议。谢谢

标签: python opencv realsense


【解决方案1】:

看来这是一个相当简单的解决方案。在浏览了 c++ 示例之后,realsense sdk 提供了一个名为 get_distance(x,y) 的函数,它将根据 x,y 坐标返回深度距离。

注意这个函数在python中是完全一样的,但是必须从深度帧中调用,并且x和y必须强制转换为整数

pipeline = rs.pipeline()
config = rs.config()
profile = pipeline.start(config)
frames = pipeline.wait_for_frames()
    while True:
        depth_frame = frames.get_depth_frame()
        zDepth = depth_frame.get_distance(int(x),int(y))

【讨论】:

  • 我们可以用这段代码得到色框像素的Z吗?
  • @Schütze,主要来自深度框架。如果您使用颜色框架中的 x,y 坐标,则需要对齐颜色和深度框架,以获得准确的深度读数。
  • 距离是米吗?
猜你喜欢
  • 2019-06-13
  • 2011-10-01
  • 1970-01-01
  • 2014-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多