Mouse callback to namedWindow in its own thread

I am porting from Windows C++ VS2017 OpenCV 2.4 to 3.4 and now to 4.5

Previously in OCV 2.4 and 3.4 I could create a namedWindow, start a new thread, and reference that namedWindow with imshow, waitKey, etc and, outside of that thread, call the mouse callback for that namedWindow.

Now I am finding out that all calls to a namedWindow have to stay in the same thread that it was created in. So, in the new thread, at the top, I create the namedWindow. and I can now reference it within that thread with imshow, waitKey and destroyWindow. So far so good…except that I can no longer find a way to set the mouse callback for that namedWindow

Originally this call was outside the thread

setMouseCallback(“cameraWindow”, my_mouse_callbackCamera, NULL);

When that is called now from the original outside the thread location the app just sits there. In debug mode I can see that nothing in the thread is getting hit. The app seems to be looping somewhere or just stuck because all I can do is Stop Debugging to exit the app.

So I moved the call back function to a location that can use a memory variable to call the callback from inside the thread.

In effect, inside the thread I have

if (bSetCameraMouseCallback == true){
setMouseCallback(“cameraWindow”, my_mouse_callbackCamera, NULL);

The declaration looks like this

void my_mouse_callbackCamera(int event, int x, int y, int flags, void* param);

But, just like when it was called from outside the thread, as soon as the setMouseCallback is called, the program just seems to stop functioning.

So the question is: How would one go about setting a mouseCallback on a namedWindow that is running in a separate thread?

I know that it is “recommended” not to use OpenCV in a multi-threaded app but, in the real world, it would be unreasonable to limit OpenCV to single threaded apps only. There has to be a way. Any help would be appreciated.

do not use threads with highgui.

everything (!), that includes namedWindow, imshow, waitKey, … has to happen in the same thread, to be safe. anything that happens to work when those conditions are violated, works unintentionally and can’t be relied upon to keep working in the future.

if you need to spread GUI stuff across multiple threads, use a specific GUI toolkit and show the data contained in your Mat objects as appropriate for that toolkit. OpenCV is meant to not support this flexibility.

So apparently they tightened up already existing rules with 4.x. In other words, this was the case back in 3.5 but I got away with it. Now, however, OCV won’t let me get away with it any more.

So I did put everything pertaining to this namedWindow cameraWindow into one thread but I can’t reference it with setMouseCallback. If others have been following the rules all this time and keeping everything in one thread, have any of them ever tried to setMouseCallback on the namedWindow? Is there a way to do that that you are aware of?

What are these GUI toolkits that you mention? Are you able to give me one or two names?

Thanks again.