DestroyWindow 不会在 Mac 上使用 Python 和 OpenCV 关闭窗口 [英] DestroyWindow does not close window on Mac using Python and OpenCV

查看:25
本文介绍了DestroyWindow 不会在 Mac 上使用 Python 和 OpenCV 关闭窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序使用以下代码生成一系列窗口:<​​/p>

My program generates a series of windows using the following code:

def display(img, name, fun):
    global clicked

    cv.NamedWindow(name, 1)
    cv.ShowImage(name, img)
    cv.SetMouseCallback(name, fun, img)

    while cv.WaitKey(33) == -1:
        if clicked == 1:
            clicked = 0
            cv.ShowImage(name, img)

    cv.DestroyWindow(name)

我在 gui 窗口中按q"将其关闭.但是,代码会继续下一次调用 display 函数并显示第二个 gui 窗口,而不会关闭第一个.我正在使用带有 OpenCV 2.1 的 Mac,在终端中运行该程序.如何关闭 gui 窗口?谢谢.

I press "q" within the gui window to close it. However, the code continues to the next call of the display function and displays a second gui window while not closing the first. I'm using a Mac with OpenCV 2.1, running the program in Terminal. How can I close the gui windows? Thanks.

推荐答案

OpenCV 中的 GUI 有一些特殊性.destroyImage 调用无法关闭窗口(至少在 Linux 下,在 2.1.0 之前默认后端是 Gtk+),除非调用 waitKey 来抽取事件.在 destroyWindow 之后添加一个 waitKey(1) 调用可能会起作用.

There are a few peculiarities with the GUI in OpenCV. The destroyImage call fails to close a window (atleast under Linux, where the default backend was Gtk+ until 2.1.0) unless waitKey was called to pump the events. Adding a waitKey(1) call right after destroyWindow may work.

即便如此,也不能保证关闭;waitKey 函数只有在窗口有焦点时才会被拦截,所以如果窗口在你调用 destroyWindow 时没有焦点,它很可能会停留可见,直到下一个 destroyWindow 调用.

Even so, closing is not guaranteed; the the waitKey function is only intercepted if a window has focus, and so if the window didn't have focus at the time you invoked destroyWindow, chances are it'll stay visible till the next destroyWindow call.

我假设这是一种源于 Gtk+ 的行为;在Windows下使用该功能没有给我带来任何麻烦.

I'm assuming this is a behaviour that stems from Gtk+; the function didn't give me any trouble when I used it under Windows.

这篇关于DestroyWindow 不会在 Mac 上使用 Python 和 OpenCV 关闭窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆