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

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

问题描述

我的程序使用以下代码生成一系列窗口:<​​/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"将其关闭.但是,代码将继续显示函数的下一个调用,并显示第二个gui窗口,而不会关闭第一个.我正在使用带有OpenCV 2.1的Mac,并在Terminal中运行该程序.如何关闭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有一些特殊之处.除非调用waitKey来泵送事件,否则destroyImage调用将无法关闭窗口(至少在Linux下,默认后端是Gtk +,直到2.1.0).在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.

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

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