使用selectROI后cv2 imshow不返回

时间:2019-10-26 18:36:50

标签: python opencv imshow

使用cv2.selectROI方法后,在另一个线程中使用imshow遇到麻烦。 这是重现问题的代码:

import cv2
from threading import Lock, Thread, Event
from timeloop import Timeloop
from datetime import timedelta


frames_list_max_size = 100
desired_fps = 8
frames_list = []
lock = Lock()
new_frame = Event()
new_frame_consumed = Event()
frames_processed = Event()
read_frame = Event()
tl = Timeloop()
cam = cv2.VideoCapture(0 + cv2.CAP_DSHOW)


def video_display():
    global frames_list
    br_temp = 0
    b, g, r = 0, 255, 0
    while True:
        print(str(len(frames_list)))
        if len(frames_list) > 0:
            frame = frames_list[(len(frames_list)-1)]
            print("imshow in video_display thread")
            cv2.imshow("preview1", frame)
            print("imshow in video_display thread done")
            cv2.waitKey(1)
            print("BR get: " + str(br_temp))
        # print (str(len(frames_list)))


@tl.job(interval=timedelta(seconds=(1/desired_fps)))
def video_capture():
    print("start video_capture2")
    global frames_list
    global frames_list_max_size
    global video_end
    global cam
    ret, frame = cam.read()
    # print(time.time())
    if ret:
        read_frame.set()
        lock.acquire()
        frames_list.insert(len(frames_list), frame)
        lock.release()
    if len(frames_list) >= frames_list_max_size:
        lock.acquire()
        del frames_list[-1]
        lock.release()
    new_frame.set()


_, _ = cam.read()
_, frame_roi = cam.read()
roi = cv2.selectROI('roi', frame_roi)
# frame_roi2 = frame_roi[int(roi[1]):int(roi[1]+roi[3]), int(roi[0]):int(roi[0]+roi[2])]
cv2.destroyWindow('roi')
# _, frame1 = cam.read()
# cv2.imshow('1', frame1)
# cv2.waitKey(0)
# cv2.destroyWindow('1')
# print("ROI selection done")

thr1 = Thread(target=video_display)


thr1.start()

tl.start(block=True)

thr1.join()

我也尝试从磁盘使用.jpg图像,而不是从设备捕获图像,但这没有帮助。 在注释行“ roi = cv2.selectROI('roi',frame_roi)”时,cv2.imshow可以正常工作。

有什么想法吗? 谢谢

0 个答案:

没有答案