简体   繁体   English

多线程处理时cv2图像显示不起作用

[英]cv2 image show doesn't work when multithreading

I am trying to put images on screen while capturing webcam (I'm using MAC). 我正在尝试在捕获网络摄像头时将图像显示在屏幕上(我正在使用MAC)。 Hence, I started two thread: one for capturing video, the other for presenting images on screen: 因此,我启动了两个线程:一个用于捕获视频,另一个用于在屏幕上呈现图像:

    webcam_thread = self.init_webcam_thread()
    images_thread = self.init_images_thread()

    webcam_thread.start()
    images_thread.start()

The video capture is working correctly; 视频捕获工作正常; The image show is working correctly while I'm not using thread (When this is the only process). 当我不使用线程时(这是唯一的过程),图像显示正常工作。 However, when using mutli-Threading, all presented in a white box and not the image itself. 但是,使用mutli-Threading时,所有内容均显示在白框中,而不显示图像本身。 This is the image code: 这是图像代码:

for pic_idx , pic_name in enumerate(pics):
while True:
    image = cv2.imread(pic_name, 0)
    if image is not None:
       cv2.imshow('image', image)
       k = cv2.waitKey(2000)

Again, when I'm not using Multi-Thread - and all I do is presenting the pic (without the video capture) it is working perfectly. 同样,当我不使用多线程时,我所做的只是呈现图片(不带视频捕获),它运行良好。 What might be the reason? 可能是什么原因?

As a general rule, you should keep any code which interacts with UI on the main thread. 通常,您应保留与主线程上的UI交互的所有代码。 You might want to consider using a Queue, with the main thread pulling images from the Queue to imshow them, and other threads pushing the images into the queue when they want them shown. 您可能要考虑使用队列,主线程从队列中拉出图像以显示图像,而其他线程则在希望显示图像时将图像推入队列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM