具有深度的 OpenCV 绘图

OpenCV drawing with depth

我希望我能解释我的问题。我有一个来自 Intel Realsense Cam 的视频,其中包含深度信息。每当我的相机和地板之间的距离低于某个值时,我都需要绘制一个矩形。我的问题是视频是在循环中逐帧处理的。我编写了一个 if 语句,它在正确的时刻绘制矩形,但是如果下一帧出现并且 if 语句不再填充,我的 recangle 就会消失。我知道为什么(当然 ;))但是我怎样才能永远“固定”我的矩形在那个位置。所以最后我想有很多 recangles。这里有足够多的矩形是我的代码:非常感谢 <3

while True:
    frames = pipeline.wait_for_frames()

#Depth_Stream

    depth_frame = frames.get_depth_frame()

    #depth_color_frame = rs.colorizer().colorize(depth_frame) #Stream Einfärben
    depth_color_frame = depth_frame

    depth_color_image = np.asanyarray(depth_color_frame.get_data())  #Erstellen Numpy Array


    cv2.circle(depth_color_image,(x_Koordinate, y_Koordinate), 10, (0,0,255), 1) #Kreis zeichnen
    cv2.imshow("Depth Stream", depth_color_image)


    #array_ausgabe.write(np.array_str(depth_color_image)) #Export RGB-Werte für jedes Pixel
    #numpy.savetxt('test.out', depth_color_image, )

#RGB_Stream

    rgb_frame = frames.get_color_frame()
    rgb_color_image = np.asanyarray(rgb_frame.get_data())

    cv2.circle(rgb_color_image,(x_Koordinate,y_Koordinate), 10, (0,0,255), 1) #Kreis zeichnen

    hight_over_table = korrigierter_abstand_zur_Kamera(x_Koordinate, y_Koordinate) - Abstand_zum_Tisch


    if hight_over_table > 0:
        cv2.rectangle(rgb_color_image,(x1_bounding_box,y1_bounding_box),(x2_bounding_box,y2_bounding_box),(0,255,0),3)
    else: print("test")



    cv2.imshow("RGB Stream", rgb_color_image)

    key = cv2.waitKey(1)
    if key == 27:
        #cv2.destroyAllWindows()
        break

这似乎是您要问的,但它的行为可能与您预期的不同。

在 while 循环上方创建一个列表。 还要添加一个布尔变量(可选 - 见下文)

rectangles = []
detected = False

while True:

当距离足够小时,将矩形的坐标添加到列表中——使用元组。使用 detected 变量来防止在每一帧的相同位置添加一个矩形。仅在范围内的第一帧上添加一个矩形。如果您打算在屏幕上跟踪某些内容,请删除该代码。

if hight_over_table > 0:
    if not detected:
        detected = True
        rectangles.append(((x1_bounding_box,y1_bounding_box),(x2_bounding_box,y2_bounding_box)))
else: 
    detected = False
    print("test")

每一帧,在显示图像之前绘制所有矩形

for p1, p2 in rectangles:
    cv2.rectangle(rgb_color_image,p1,p2,(0,255,0),3)

cv2.imshow("RGB Stream", rgb_color_image)

请注意,矩形是绘制到屏幕上的,而不是绘制到表面上的。如果将相机指向天空,矩形仍会存在。 技术上 可以将直角贴到表面上,但这要复杂得多。