Python OpenCV - waitKey(0) 没有响应?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20539497/
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-18 20:41:53  来源:igfitidea点击:

Python OpenCV - waitKey(0) does not respond?

pythonopencvubuntu-12.04

提问by Dhorka

I'm using opencv 2.4.7 on ubuntu 12.04. I'm programming with python and I have a problem when i run this script:

我在 ubuntu 12.04 上使用 opencv 2.4.7。我正在使用 python 编程,但在运行此脚本时遇到问题:

import cv2

img = cv2.imread('347620923614738322_233985812.jpg')
cv2.namedWindow("window")
cv2.imshow("window", img)
cv2.waitKey(0)

The problem is that the script doesn't stop when I close the image. I searched information about waitKeyand I found that using cv2.waitKey(0)is correct.

问题是当我关闭图像时脚本不会停止。我搜索了有关的信息waitKey,发现使用cv2.waitKey(0)是正确的。

I don't understand, where is the problem?

不明白,问题出在哪里?

采纳答案by Raoul HATTERER

This code works for me from IDLE:

此代码适用于 IDLE:

# -*- coding: utf-8 -*-

# Objectif : découvrir le fonctionnement d'opencv-python
# http://opencv-python-tutroals.readthedocs.org/en/latest/index.html


import numpy as np
import cv2

# Load an color image in grayscale
img = cv2.imread('Lena.tiff',0)
WINDOW_NAME = 'Image de Lena'
cv2.namedWindow(WINDOW_NAME, cv2.CV_WINDOW_AUTOSIZE)
cv2.startWindowThread()

# Display an image
cv2.imshow(WINDOW_NAME,img)
cv2.waitKey(0) 


cv2.destroyAllWindows()

Hope this helps for future readers.

希望这对未来的读者有所帮助。

回答by Tomasz Edwin

cv2.waitKey(0) means that script is in infinity loop with 0 miliseconds wait after loop .only specified key can stop it.

cv2.waitKey(0) 表示脚本处于无限循环中,循环后等待 0 毫秒。只有指定的键才能停止它。

you did not specified end of app condition.

您没有指定应用程序结束条件。

Try this code: Using other keys for the waitKey() function of opencv

试试这个代码: Using other keys for the waitKey() function of opencv

回答by Cherif KAOUA

Try to execute the script directly from the Terminal works 100%for me but not from an IDE for example, i explain : I'm using fedora 20 and got the same problem, copying the first example from official opencv python tutorial, i'm using :

尝试直接从终端执行脚本对我来说100%对我来说是 100%但不是从 IDE 例如,我解释说:我使用的是 Fedora 20 并遇到了同样的问题,从官方 opencv python 教程中复制了第一个示例,我是使用 :

  • Fedora 20 64bit
  • Spyder IDE for python
  • Python Version 2.7.5
  • Fedora 64 bit
  • OpenCV 2.4.7
  • Fedora 20 64 位
  • 适用于 Python 的 Spyder IDE
  • Python 版本 2.7.5
  • Fedora 64 位
  • OpenCV 2.4.7

Here is the code for test

这是测试的代码

import cv2
img = cv2.imread('/path/image1.jpeg',0)
cv2.imshow('Display',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

When running this script using F5 from Spyder, it runs it using the embedded python terminal with this line :

当使用来自 Spyder 的 F5 运行此脚本时,它使用嵌入式 python 终端运行它:

runfile('/home/user/Workspace/test.py', wdir=r'/home/user/Workspace')

In this instance, cv2.waitKey(0) or cv2.waitKey(-1) are not working and windows remains opened after pressing keys with the code of the example Trying to close the windows will result in a "Not Responding , Force to Quit" Alert But when executing the script from Terminal , it works 100%

在这种情况下, cv2.waitKey(0) 或 cv2.waitKey(-1) 不起作用,并且在使用示例代码按下键后窗口保持打开状态尝试关闭窗口将导致“无响应,强制退出” " Alert 但是当从终端执行脚本时,它 100% 工作

didn't found the problem origin , will update if i find it.

没有找到问题的根源,如果我找到了会更新。

回答by user2022581

I found that it works if i press the key whilst the window is in focus. If the command line is in focus then nothing happens

我发现如果我在窗口处于焦点时按下键,它会起作用。如果命令行处于焦点,则什么也不会发生

回答by Teng Long

Adding a cv2.waitKey(1) after you destroy the window should work in this case.

在这种情况下,在销毁窗口后添加 cv2.waitKey(1) 应该可以工作。

cv2.imshow('imgae',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(1)

回答by R K Bhalodia

There is a problem with unix based system running opencv programs from python notebooks.

基于 Unix 的系统从 python 笔记本运行 opencv 程序存在问题。

Check this alternate method My suggestion is to run code in python in terminal. You will not face any kind of problem

检查这个替代方法 我的建议是在终端中的 python 中运行代码。你不会遇到任何问题

Copy the same code and save with filename.py

复制相同的代码并使用 filename.py 保存

import cv2
input = cv2.imread('path_to_image.png')
cv2.imshow('Hello World', input)
cv2.waitKey(0)
cv2.destroyAllWindows()

then open specific directory and then open terminal

然后打开特定目录,然后打开终端

Steps:

脚步:

Open Terminal
cd path/to/filename.py
source activate YOURPROFILE 
python filename.py

This will solve problem

这将解决问题

https://youtu.be/8O-FW4Wm10s

https://youtu.be/8O-FW4Wm10s

回答by Apogentus

Solved in Spyder under Ubuntu by following [Run]->[Configuration per file]->[Execute in an external system terminal].

在Ubuntu下的Spyder中按照【运行】->【按文件配置】->【在外部系统终端中执行】解决。

回答by Employee

Here a minimalistic code for best performance on all platforms:

这是在所有平台上获得最佳性能的简约代码:

import cv2

img = cv2.imread("image.jpg")
cv2.imshow("Window", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

And now some observations:

现在有一些观察

When the user wants to close the window through the press of the0key, he/she has to make sure that the 0key is pressed whilst the window is in focus. Because as stated above, if the terminal is in focus nothing happens and code execution stucks at the cv2.waitKey(0)until the 0key is pressed properly whilst the window is in focus.

当用户想要通过的按关闭该窗口0的关键,他/她必须确保该0键被按下,同时窗口处于焦点。因为如上所述,如果终端处于焦点状态,则不会发生任何事情并且代码执行会停留在 ,cv2.waitKey(0)直到0在窗口处于焦点状态时正确按下键。

Pressing the 0key whilst the window is in focus is the right way to close the window and make sure that, once the window is destroyed in line cv2.destroyAllWindows()and the program ends, the user can get back the control of the terminal.

0在窗口处于焦点时按下键是关闭窗口的正确方法,并确保一旦窗口被销毁cv2.destroyAllWindows()并且程序结束,用户可以重新获得对终端的控制

If the window is exited by mouse click, the window will be destroyed yes, but the user will very much end up in the situation of not being able to get back the control of the terminal. In this kind of situation the user may want to shut down the unresponsive terminal and open a new one.

如果通过鼠标点击退出窗口,窗口会被销毁是的,但用户最终会处于无法取回终端控制权的情况。在这种情况下,用户可能想要关闭没有响应的终端并打开一个新终端。

回答by rahul singh

click on the image window(active) and then press and key, don't write in the terminal window.

单击图像窗口(活动)然后按 和 键,不要在终端窗口中写入。

回答by juliukvi

This will work even if you close the window using the cross button on the window.

即使您使用窗口上的十字按钮关闭窗口,这也将起作用。

import numpy as np
import cv2

img = cv2.imread('arvid.jpg', 0)
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow('image', img)

while True:
    k = cv2.waitKey(30) & 0xFF
    if k == 27:         # wait for ESC key to exit
        cv2.destroyAllWindows()
    elif k == ord('s'):  # wait for 's' key to save and exit
        cv2.imwrite('arvid2.jpg', img)
        cv2.destroyAllWindows()
    if cv2.getWindowProperty("image", 0) == -1:
        break