Major23
Major23

Reputation: 53

OpenCV drawing with depth

i hope i can explain my problem. I have a Video from a Intel Realsense Cam with depth information. I need to draw a rectangle everytime the distance between my camera and the floor is under a certain value. My problem is the video is proccessed frame after frame in a loop. I programmed a if-statement which draws the rectangle at the right moment but if the next frame comes and the if-statement ist not fullfilled anymore my recangle disappers. I know why (of course ;)) but how can i "fix" my rectangle on that position forever. So at the end i want to have a lof of recangles. So enough of rectangles here is my code: Thank you very much <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

Upvotes: 0

Views: 1080

Answers (1)

J.D.
J.D.

Reputation: 4561

This seems to be what you are asking, but it might not behave as you intend.

Create a list above of the while-loop. Also add a boolean variable (optional - see below)

rectangles = []
detected = False

while True:

When the distance is small enough, add the coordinates of the rectangle to the list - use a tuple. Use the detected variable to prevent adding a rectangle on the same location every frame. A rectangle is only added on the first frame something is in range. Remove that code if you intend to track something across the screen.

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

Each frame, draw all rectangles before displaying the image

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

cv2.imshow("RGB Stream", rgb_color_image)

Note that the rectangles are drawn to the screen - not to a surface. If you point the camera to the sky, the rectangles will still be there. It is technically possible to stick the rects to a surface, but that is much more complicated.

Upvotes: 1

Related Questions