Event Processing Environment for OpenCV's HighGUI

Hello,

I have an application using OpenCV’s HighGUI. The gui is updated periodically based on user input so it may sit for a long time. Verified on two setups, the gui would sort of crash. I don’t know if crash is the right word, but the window would gray out, which I typically see when Ubuntu is crashing. But only the OpenCV gui window is crashing. This was always after about 10 seconds. I was also able to reproduce the problem by simply display the image and waiting in a while loop.

I looked into the functions for HighGUI, and I found an interesting note under cv::waitKey:

This function is the only method in HighGUI that can fetch and handle events, so it needs to be called periodically for normal event processing unless HighGUI is used within an environment that takes care of event processing.

Now I’ve used OpenCV on Ubuntu 16.04 and Raspbian 9. I never had this graying out thing. I figure this probably didn’t change with Ubuntu 18.04. But I have to ask if something with the Nano doesn’t “take care of event processing” or if there’s something weird causing the window to crash.

I have a band-aid solution for this that I don’t really like. I’d like to see if there is some other option I am missing.

My band-aid solution is as below:

std::mutex keep_alive_gui_mtx;
void keep_alive_gui() {
    while(keep_alive_gui_mtx.try_lock()) {
        cv::waitKey(1);
        keep_alive_gui_mtx.unlock();

        std::this_thread::sleep_for(std::chrono::milliseconds(1));
    }
}

...

//always active while the application is active
std::thread keep_alive_gui_thread(keep_alive_gui);

...

//on closing application
keep_alive_gui_mtx.lock();
keep_alive_gui_thread.join();
keep_alive_gui_mtx.unlock();

Hi,
This looks to be an issue in OpenCV. You may upgrade to 4.3.0 and try again:

If the issue is still presnet, you may add debug prints to source code and rebuild the libs.

hi warpstar22:
if you want gui sit for a long time ,why not simply set cv::waitKey(0)? then it will exist there until you press any key which will cause the key event

Hi @Jeffli,

I need the application to run without a key press. But, it seems like this is probably a problem with OpenCV. I’m not sure what causes the problem exactly but I found one other who had a similar problem. It appears to be that the Ubuntu window will stall if it doesn’t have an occasional event handling call. That person also had the greying happen after 10 seconds so it seems like a normal thing. It just seems odd that I didn’t have to do this for a similar application on Raspberry Pi, but maybe the windows on that doesn’t need the event handling calls. I’m using my work around for now, but in the future I’ll probably move to something else for displaying images. Thanks for the help anyways.