OpenCV关闭窗口,出现鼠标问题 [英] OpenCV close window with mouse issue

查看:288
本文介绍了OpenCV关闭窗口,出现鼠标问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OpenCV 2.2版,C ++接口.

OpenCV version 2.2, C++ interface.

在带有以下代码段的窗口中显示加载的图像时

When showing a loaded image in a window with following code snippet

cvStartWindowThread();

Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR);   // Read the file

if(! image.data )                              // Check for invalid input
{
    cout <<  "Could not open or find the image" << std::endl ;
    return -1;
}

namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image );   

while( 1 ) {
    if( cvWaitKey(100) == 27 ) break;
}

当我用鼠标按下close button而不是使用逃逸键来关闭图像时,我遇到了一个问题.

I face a problem when closing image by pressing the close button with the mouse, instead of using escape key.

在这种情况下,我的程序将被阻塞在while中,退出它的唯一方法是停止执行,这显然是不希望的.

In this case my program will be blocked in the while and the only way to exit it is to stop execution, which obviously is not desired.

是否有任何功能可以控制是否已按下close button? 这样,我可以将其添加到while循环中,如下所示:

Is there any function that controls if the close button has been pressed? In that way I could add it in the while loop as this:

例如

while( 1 ) {
    if( cvWaitKey(100) == 27 ) break;
    if( cvCloseButtonPressed == 1) break; <--- purely invented method I'm looking for...
}

推荐答案

您可以使用cvGetWindowHandle()函数获取命名窗口的句柄.窗口句柄是特定于操作系统的功能. win32的示例如下所示:

You can use cvGetWindowHandle() function to get handle of your named window. Window handle is an OS specific feature. An example for win32 looks like this:

HWND hwnd = (HWND)cvGetWindowHandle("Display window");
while(IsWindowVisible(hwnd)) {
    if( cvWaitKey(100) == 27 ) break;
}

IsWindowVisible()是winapi函数,因此您可能要添加#include <windows.h>

IsWindowVisible() is a winapi function, so you may want to add #include <windows.h>

这篇关于OpenCV关闭窗口,出现鼠标问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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