如何更新 Python OpenCV CV2 的 imshow() 窗口

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

How to update imshow() window for Python OpenCV CV2

pythonpython-2.7opencv

提问by Alwin Hui

My current program will output an image to the user and based on user input, readjust the image as necessary.

我当前的程序将向用户输出图像,并根据用户输入,根据需要重新调整图像。

Long story short, I am trying to find circular objects in an image file. I will be using the Hough Circle Transform. However, because many of my circles in the image are not "perfect circles", I am doing an algorithm that "guesses" the radii of the circles. However, I want to allow the user to readjust the radii as necessary.

长话短说,我试图在图像文件中找到圆形对象。我将使用霍夫圆变换。然而,因为我在图像中的许多圆圈都不是“完美的圆圈”,我正在做一个算法来“猜测”圆圈的半径。但是,我希望允许用户根据需要重新调整半径。

Is there some way to ask the user for input and then based on the user input, readjust the window in imshow()? Right now, imshow() refuses to show the actual window until I use cv2.waitKey(0), at which point I can't ask for user input until the window is destroyed.

有没有办法让用户输入,然后根据用户输入,在 imshow() 中重新调整窗口?现在,imshow() 拒绝显示实际窗口,直到我使用 cv2.waitKey(0),此时我不能要求用户输入,直到窗口被销毁。

采纳答案by 101

You can call imshowrepeatedly without destroying it. And yes, you will likely need waitKey, just don't call it with 0 or it will wait indefinitely. Call it with a 1 to wait just 1ms and ensure the image redraws. Try something like:

您可以imshow重复调用而不会破坏它。是的,您可能需要waitKey,只是不要用 0 调用它,否则它会无限期地等待。用 1 调用它以等待 1ms 并确保图像重绘。尝试类似:

while True:
    cv2.imshow('image', img)
    cv2.waitKey(1)
    radius = input('Input radius')
    # recalculate image with new radius here...