Python 在 openCV 中的图像显示之间暂停

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46671348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:45:10  来源:igfitidea点击:

Make a pause between images display in openCV

pythonopencv

提问by Antonio Serrano

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.

我想展示几张图片,中间暂停一下。我尝试过使用 waitKey,等待用户按 ESC,但它似乎不起作用。

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)

Any suggestion?

有什么建议吗?

Thanks in advance

提前致谢

回答by Abhijith

In the code you've posted, image1.jpgis displayed and waits for the user to press any key since you've used waitKey(0). The 0indicates 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.jpgis read but not displayed since you do not have a waitKeyafter the second imshowand the program will exit.

在您发布的代码中,image1.jpg显示并等待用户按任意键,因为您已经使用了waitKey(0). 该0指示程序将等待,直到用户点击一个键。您可以添加在显示第二个图像之前要等待的毫秒数来代替0. 一旦你按下一个键,image2.jpg就会被读取但不显示,因为你waitKey在第二个之后没有imshow,程序将退出。

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.

你可以试试下面的代码。此代码假定您的“多张图像”位于一个文件夹中,它显示一个图像,暂停 3 秒并显示下一个。

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

回答by Martin Beckett

In the waitkey()call the number is the number of milliseconds to wait for.

waitkey()呼叫中,数字是等待的毫秒数。

回答by Martin Evans

Adding a value to waitkey()will make it pause for the given number of milliseconds. You could use itertools.cycle()to cycle through a list of images.

添加一个值waitkey()将使其暂停给定的毫秒数。您可以使用itertools.cycle()循环浏览图像列表。

This script will pause 5 seconds and then display the next image. If escape is pressed it exits.

此脚本将暂停 5 秒,然后显示下一张图像。如果按下退出,它会退出。

from itertools import cycle
import cv2


for image_filename in cycle(['image1.jpg', 'image2.jpg']):
    image = cv2.imread(image_filename, 0)
    cv2.imshow('image', image)

    # Pause here 5 seconds.
    k = cv2.waitKey(5000)

    if k == 27:         # If escape was pressed exit
        cv2.destroyAllWindows()
        break

回答by amran hossen

You can use glob module in python to select all files

您可以在 python 中使用 glob 模块来选择所有文件

import cv2
import glob # get all files in the directory


images = glob.glob("path_to_files/*.jpg") #get all files with given extension

for img in range(len(images)):
    current_image = cv2.imread(images[img], 1) #cv2.imread(images[i], 0) # for image in grayscale
    cv2.imshow('Display Images', current_image)
    key = cv2.waitKey(300) #change to your own waiting time 1000 = 1 second 
    if key == 27: #if ESC is pressed, exit
        cv2.destroyAllWindows()
        break