【问题标题】:OpenCV drawing with depthOpenCV 深度绘图
【发布时间】:2022-01-20 16:32:23
【问题描述】:

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

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

【问题讨论】:

  • 继续画吗?
  • @ChristophRackwitz 继续画是什么意思?

标签: python opencv


【解决方案1】:

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

在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)

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

【讨论】:

  • 哇,这简直太完美了。正是认为我需要的。矩形粘在屏幕上不是问题,因为相机固定在其位置。非常非常非常感谢:)
猜你喜欢
  • 1970-01-01
  • 2020-07-21
  • 2013-07-21
  • 1970-01-01
  • 1970-01-01
  • 2013-10-13
  • 2015-10-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多