简体   繁体   中英

I can not get CV2.waitKey in OpenCV to work properly. After running waitKey the code get unresponsive

I ran this simple code to show a pic using OpenCV. The pic shows up in a new windows as expected. When I close the window the console in spyder get stuck, it looks like is still running or waiting. It seams like the waitKey() in not working properly. The only way I can continue is restarting the console. When I use a number for example waitKey(1000) the program works fine, the picture shows up, windows close and the program finish as expected. I tried to run the code in the Anaconda shell from the prompt and I have the same problem I am using python 3.7.5 Thanks a lot!

import cv2
cv2.imshow("title", img)
cv2.waitKey()
cv2.destroyAllWindows()

Before hitting a key get focus on graphic window with the image.

cv2.waitKey() is a function that sleeps for a certain amount of milliseconds to keep the windows displayed with cv2.imshow() opened.

The correct usage is one of the two cases below:

  • cv2.waitKey(0) : sleeps until the user presses a key;
  • cv2.waitKey(5) : sleeps for 5ms. Here, the number 5 can be replaced for any value greater than zero to represent the amount of milliseconds the function must use to take a nap;

Calling cv2.waitKey() without any value might trigger unexpected behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM