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();