Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to use waitKey()

I am writing a program to read images from a folder and transition between them using addWeighted() method. But I am not where to keep the waitKey() method. Should is go inside the same loop where I am transitioning between the images or should it go outside the loop. I tried the following but it does not work as required. I am actually not sure about waitKey() behavior I have seen code where it is used inside the loop and sometimes outside. What I have understood by now is that it when your frame is static it can go outside without looping but when your frame needs a refresh it needs to go inside the loop. Is my understanding correct?

Exercise: Create a slide show of images in a folder with smooth transition

import glob
images = glob.glob('*.png')
while(True):
    for i in range(len(images)-1):
        current = cv2.imread(images[i])
        nxt = cv2.imread(images[i+1])
        for alpha in [x / 100.0 for x in range(101)]:
            dst = cv2.addWeighted(current, 1.0-alpha, nxt, alpha, 0)
            cv2.imshow('res', dst)
            if cv2.waitKey(0) == 27:
                break
cv2.destroyAllWindows()