Visualization error of cv::imshow in multithreading

In multi-threading, if imshow in opencv is used in the sub-thread, there will be a problem of visual error reporting. Possible problems include: getting stuck, displaying blank, or reporting the following errors.

demo: ../../src/xcb_io.c:641:_XReply: 假设 ‘!xcb_xlib_threads_sequence_lost’ 失败。
已放弃 (核心已转储)
(2:49940): GLib-GObject-CRITICAL **: 22:37:28.447: g_object_new_with_properties: assertion 'G_TYPE_IS_OBJECT (object_type)' failed

Problem: If I remove all lines in the cv2.imshow() code it works as expected, both threads run fine. But if cv2.imshow() is added, the above error will be reported.
Reason Explanation (1): Calling imshow in multiple threads will cause threads to preempt ui resources.
Try to solve the problem: move namedWindow creation to the main thread and capture_and_save thread. I also tried moving between waitKey(), since OpenCV supposedly only allows waitKey() in the main thread. However, there is no difference.

Since there is no requirement for the visualization of this sub-thread in the project, the visualization code is directly deleted. However, if the visualization code is not removed, how should it be modified? I hope someone can answer it.—(Or transfer the results that need to be visualized to a new thread for visualization.) Reference link: 1) Python
multithreading
is useless_Python Multithreaded cv2.imshow() doesn't work

Guess you like

Origin blog.csdn.net/qq_42178122/article/details/128432200