Search code examples
pythonopencv

What is the difference between cv2.waitKey() and cv2.waitKey(0)?


I have found a lot of questions around about what is cv2.waitKey() used for, about the difference between cv2.waitKey(0) and cv2.waitKey(1), etc., the answers to which can be found in many OpenCV documentations. Yet, I have not found any documentation or people asking about the difference between cv2.waitKey(0) and cv2.waitKey(). Both seem to have the same effect, namely waiting for a keypress before the image window is closed. Yet, In dozens of codes that I have seen, they all use cv2.waitKey(0). For example:

cv2.imshow("my_image", img)
cv2.waitKey(0) 

So, is there a particular reason for that or is 0 just useless?

(My opencv-python version is 4.9.0.80.)


Solution

  • There is no difference.

    waitKey()'s first argument's default is 0. If you call the function without arguments, the default argument is used.

    Nothing in C++ or Python forbids you from passing values equal to the defaults. It is not an error and not a problem. It is a matter of style.