简体   繁体   English

为什么 cv2 不在 for 循环中显示图像?

[英]Why doesn't cv2 show an image within a for loop?

The following code will display an image correctly using cv2 in python:以下代码将使用 python 中的 cv2 正确显示图像:

import cv2

img = 'image file.jpg'

frame = cv2.imread(img)

while True:

    cv2.imshow('frame', frame)

    if cv2.waitKey(20) == ord('q'):

        break

However, say I want to run a for loop which incorporates cv2 showing an image:但是,假设我想运行一个包含显示图像的 cv2 的 for 循环:

import cv2

img = 'image file.jpg'

frame = cv2.imread(img)

test = [1,2]

for t in test:

    print(t)

    cv2.imshow('frame', frame)

    if cv2.waitKey(20) == ord('q'):

        break

I expected this code to show the image twice, but the image isn't shown at all.我希望这段代码显示图像两次,但图像根本没有显示。 't' is printed correctly. 't' 打印正确。 What am I missing?我错过了什么?

Nevermind I realized I just need to change this:没关系,我意识到我只需要改变这个:

if cv2.waitKey(20) == ord('q'):

    break

to this:对此:

if cv2.waitKey(0) == ord('q'):

    cv2.destroyAllWindows()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM