在openCV中的图像显示之间暂停 [英] Make a pause between images display in openCV
问题描述
我想显示几张图像,中间要稍作停顿. 我已经尝试过使用waitKey,等待用户按ESC,但是它似乎不起作用.
I would like to show several images, making a pause in between. I have tried with waitKey, waiting the user to press ESC, but it doesn't seem to be working.
import numpy as np
import cv2
img = cv2.imread('image1.jpg',0)
cv2.imshow('image',img)
# here it should be the pause
k = cv2.waitKey(0)
if k == 27: # wait for ESC key to exit
cv2.destroyAllWindows()
img = cv2.imread('image2.jpg',0)
cv2.imshow('image',img)
有什么建议吗?
预先感谢
推荐答案
在您发布的代码中,显示image1.jpg
并等待用户按下任何键,因为您已经使用过
In the code you've posted, image1.jpg
is displayed and waits for the user to press any key since you've used waitKey(0)
. The 0
indicates the program will wait until a user hits a key. You can add the number of milliseconds you want to wait before the second image is displayed in place of the 0
. Once you've pressed a key, image2.jpg
is read but not displayed since you do not have a waitKey
after the second imshow
and the program will exit.
您可以尝试以下代码.此代码假定您的几张图片"位于一个文件夹中,并显示一张图片,暂停3秒钟,然后显示下一张.
You can try the following code. This code assumes that your "several images" are located in a folder and it displays an image, pauses for 3 seconds and displays the next.
import cv2
import os
folder_path = ''#folder path to your images
for path in os.listdir(folder_path):#loop to read one image at a time
imgpath = os.path.join(folder_path, path)
frame = cv2.imread(imgpath, 1)
cv2.imshow('Window', frame)
key = cv2.waitKey(3000)#pauses for 3 seconds before fetching next image
if key == 27:#if ESC is pressed, exit loop
cv2.destroyAllWindows()
break
这篇关于在openCV中的图像显示之间暂停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!