Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying image without waitKey

Tags:

python

opencv

This Python code displayed an image in full screen:

blank_image = cv2.imread('blank.jpg')
cv2.namedWindow("bw", cv2.WND_PROP_FULLSCREEN)          
cv2.setWindowProperty("bw", cv2.WND_PROP_FULLSCREEN, cv2.cv.CV_WINDOW_FULLSCREEN)
cv2.imshow("bw", blank_image)
cv2.waitKey(0)

The problem is the code is going to run on a Linux machine without keyboard. Calling waitKey means the UI processing will not be done until a key event occurs, and thus the contradiction.

Is there any way beside the waitKey, then?

like image 971
anta40 Avatar asked Sep 14 '15 08:09

anta40


People also ask

Is cv2 waitKey necessary?

waitKey() , the NOTE section mentions that '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. ' You can notice that without the cv2.

What happens if we pass 0 to waitKey ()?

waitKey(0) will pause your screen because it will wait infinitely for keyPress on your keyboard and will not refresh the frame( cap. read() ) using your WebCam.

What is the use of waitKey in OpenCV?

waitkey() function of Python OpenCV allows users to display a window for given milliseconds or until any key is pressed. It takes time in milliseconds as a parameter and waits for the given time to destroy the window, if 0 is passed in the argument it waits till any key is pressed.


2 Answers

Just to clarify: from the docs you can see that

Python: cv2.waitKey([delay]) → retval

The function waitKey waits for a key event infinitely (when delay <= 0 ) or for delay milliseconds, when it is positive.

If you use the delay = 0, then your program waits for a key event infinitely, blocking the execution. As @Miki said, you can use delay = 1, so that waitKey won't block.

like image 183
Berriel Avatar answered Oct 07 '22 21:10

Berriel


You can use matplotlib library for displaying images in python.

In [9]: blank_image = cv2.imread('blank.jpg')
In [10]: import matplotlib.pyplot as plt
In [11]: plt.ion()
In [12]: plt.imshow(blank_image)
Out[12]: <matplotlib.image.AxesImage at 0x7fb3cf31bf10>
like image 22
Rahul K P Avatar answered Oct 07 '22 19:10

Rahul K P