Ask Your Question
2

Block until window closed

asked 2020-07-24 10:15:40 -0600

kbarni gravatar image

I would like to display a window, and block the program execution until the user decides to continue. This is done normally the following way:

Mat img1,img2;
//...process...
namedWindow("Image1",WINDOW_NORMAL);
imshow("Image1",img1);
waitKey();
destroyAllWindows();
namedWindow("Image2",WINDOW_NORMAL);
imshow("Image2",img2);

The only problem with this approach is if the user instinctively closes the window using the X button. Then, as no HighGUI windows are active, waitKey() will never return and the program blocks.

Is there any solution, so the program waits a keypress OR the window to be closed?

edit retag flag offensive close merge delete

Comments

hi @kbarni. what is your OpenCV version and OS?. in Windows with latest OpenCV here your code works flawlessly.

sturkmen gravatar imagesturkmen ( 2020-07-24 12:57:18 -0600 )edit
sturkmen gravatar imagesturkmen ( 2020-07-24 13:17:33 -0600 )edit
1

I'm using Ubuntu and OpenCV 4.3. I have to check on windows...

kbarni gravatar imagekbarni ( 2020-07-27 10:08:49 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2020-07-24 11:04:58 -0600

mvuori gravatar image

updated 2020-07-27 10:04:43 -0600

kbarni gravatar image

A minute of googling could have made you find out that you can use getWindowProperty() to check whether the window is ok: https://medium.com/@mh_yip/opencv-det...

[EDIT] in case of link becomes unavailable, here's the relevant part of the article: you can check if the window is still open using the WND_PROP_VISIBLE propoerty of the window. So the waitKey() line becomes:

while( (chr<0) && (getWindowProperty("Image1",WND_PROP_VISIBLE)) ){ chr=waitKey(100); }
edit flag offensive delete link more

Comments

yeah googling is good sometimes. but doesn't help spreading knowledge. at least me. i learned something new. and the most important thing is sometimes a question seems unnecessary will help some improvements on the library.

sturkmen gravatar imagesturkmen ( 2020-07-24 13:04:34 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-07-24 10:15:40 -0600

Seen: 2,117 times

Last updated: Jul 27 '20