cv2 image show doesn't work when multithreading

久未见 提交于 2020-01-06 03:13:05

问题


I am trying to put images on screen while capturing webcam (I'm using 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. 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?


回答1:


As a general rule, you should keep any code which interacts with UI on the main thread. 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.



来源:https://stackoverflow.com/questions/49096804/cv2-image-show-doesnt-work-when-multithreading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!