Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Is OpenCV frames passed by referenced?

I want to display my camera output in 2 different windows. I want to do some drawing in one of them and display the original image in the other. So what I did is I create a thread that fetches the camera stream and assigned it to a global variable. Then, 2 separate thread retrieved these frames and assigned it to a local variable. However, upon displaying, both are showing the drawings.

import cv2
import time
from threading import Thread

camip = 0
camera_frame = None

def read():
    cam = cv2.VideoCapture(camip)
    while True:
        ret,frame = cam.read()
        global camera_frame
        camera_frame = frame

def display(x):
    print "Starting camera "+str(x)
    while True:
        temp_frame = camera_frame
        cv2.rectangle(temp_frame,(130,130),(430,430),(0,255,0),2)
        cv2.imshow("Camera "+str(x),temp_frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

def display2(x):
    print "Starting camera "+str(x)
    while True:
        temp_frame = camera_frame
        cv2.imshow("Camera "+str(x),temp_frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

t0 = Thread(target=read)
t0.start()
time.sleep(1)

t1 = Thread(target=display, args=(1,))
t1.start()
time.sleep(1)

t2 = Thread(target=display2, args=(2,))
t2.start()