C++ OpenCV-关闭图像显示窗口

C++ OpenCV-关闭图像显示窗口,c++,c,opencv,C++,C,Opencv,我正在做一个项目,搜索一个图像数据库,当我找到一些query-5数据库图像的结果时,我想直观地显示结果。我没有将所有图像都保存在内存中,因此我必须先加载图像以显示它 我有一个简单的想法,用伪代码: for image 1..5 load images display image in a window wait for any keypress close the window 以下是我在C++中使用OpenCV的代码片段: IplImage *img; fo

我正在做一个项目,搜索一个图像数据库,当我找到一些query-5数据库图像的结果时,我想直观地显示结果。我没有将所有图像都保存在内存中,因此我必须先加载图像以显示它

我有一个简单的想法,用伪代码:

for image 1..5
    load images
    display image in a window
    wait for any keypress
    close the window
以下是我在
C++
中使用
OpenCV
的代码片段:

IplImage *img;

for (int i=0; i < 5; ++i){
    img = cvLoadImage(images[i].name.c_str(),1);
    cvShowImage(("Match" + images[i].name).c_str(), img);
    cvWaitKey(0);
    cvDestroyWindow(("Match" + images[i].name).c_str());
    // sleep(1);
    cvReleaseImage(&img);
}
IplImage*img;
对于(int i=0;i<5;++i){
img=cvLoadImage(images[i].name.c_str(),1);
cvShowImage((“匹配”+images[i].name).c_str(),img);
cvWaitKey(0);
CVD窗口((“匹配”+图像[i].name).c_str());
//睡眠(1);
cvReleaseImage(&img);
}
此处使用的
images
数组本身不存在于我的代码中,但出于问题的考虑,如果其
name
成员,它包含相对于当前程序运行点的图像文件名。在我的项目中,我存储图像名称的方式略有不同

上面的代码几乎可以正常工作:我可以遍历4/5个图像,但当显示最后一个图像并按下一个键时,图像变为灰色,我无法关闭图像窗口而不使其余的应用程序崩溃

我的第一个想法是,由于编译时优化,
cvReleaseImage
cvdestronWindow
完成之前释放图像,这不知何故使图像冻结。但是,我试着增加一些等待时间(因此我的代码中注释掉了
sleep(1)
行),但没有效果

我正在从控制台应用程序调用此显示功能,当图像冻结时,控件返回到我的应用程序,我可以继续使用它(但图像窗口仍在后台冻结)

你能给我一些关于如何解决这个问题的建议吗

编辑

自从提出这个问题以来,我经常与一些处理计算机视觉和OpenCV的人交谈,但仍然没有任何想法

我也发现了类似的问题,但仍然没有公认的答案。谷歌搜索结果只是给出了类似的问题,但没有答案

非常感谢任何关于尝试什么的想法(即使它们不是完整的解决方案)

尝试使用

cvDestroyWindow("Match");
// sleep(1);
cvReleaseImage(&img); // outside the for loop

无需破坏每个帧上的窗口,只需使用相同的窗口名调用cvShowImage(),它将替换当前图像

您只需要在关机时调用销毁窗口。您可以在启动时使用cvCreateWindow()创建窗口,但它将在第一次调用showWindow()时自动创建。

cvDestroyWindow()
通常只启动相当复杂的窗口销毁过程。此过程需要在窗口系统和应用程序之间进行一些交互(事件交换)。在该过程完成之前,无法完全销毁窗口。这就是为什么当应用程序执行与GUI无关的操作时,会看到部分被破坏的窗口

事件交换可以以依赖于系统的方式执行。在Windows中,这意味着(直接或间接)调用
GetMessage
MsgWaitFor*
函数并处理结果。对于Unix,这意味着(直接或间接)调用
XNextEvent
并处理结果

OpenCV允许以独立于系统的方式进行此事件交换。有两种记录在案的方法可以做到这一点。第一个是
cvWaitKey()
(关闭最后一个图像后,只需调用
cvWaitKey(1)
)。第二种方法是在程序开始时调用
cvStartWindowThread()
,以允许OpenCV自动更新其窗口

这些方法中只有一种在我的Linux机器上使用libcv2.1时工作正常:
cvStartWindowThread()


更新(使用cvStartWindowThread()的代码片段)

//gcc-std=c99 main.c-lcv-lcxcore-lhighgui
#包括
#包括
#包括
#包括
#定义NUM_IMGS 2
int main(int argc,char*argv[])
{
如果(argc<2)
{
printf(“用法:%s\n”,argv[0]);
返回-1;
}
cvStartWindowThread();
//用于存储图像指针的数组
IplImage*图像[NUM_IMGS]={0};
对于(int i=0;i
出于测试目的,下面的应用程序完全按照您在问题中所说的那样执行:它通过命令行逐个加载7个图像,并为每个要显示的图像创建一个新窗口

它与Linux上的OpenCV 2.3.1完美配合

#include <cv.h>
#include <highgui.h>

#define NUM_IMGS 7

int main(int argc, char* argv[])
{
    if (argc < 8)
    {
        printf("Usage: %s <img1> <img2> <img3> <img4> <img5> <img6> <img7>\n", argv[0]);
        return -1;
    }

    // Array to store pointers for the images
    IplImage* images[NUM_IMGS] = { 0 };

    for (int i = 0; i < NUM_IMGS; i++)
    {
        // load image
        images[i] = cvLoadImage(argv[i+1], CV_LOAD_IMAGE_UNCHANGED);
        if (!images[i])
        {
            printf("!!! failed to load: %s\n", argv[i+1]);
            continue;
        }

        // display image in a window
        cvNamedWindow(argv[i+1], CV_WINDOW_AUTOSIZE); // creating a new window each time
        cvShowImage(argv[i+1], images[i]);

        // wait for keypress
        cvWaitKey(0);

        // close the window
        cvDestroyWindow(argv[i+1]);
        cvReleaseImage(&images[i]);
    }

    return 0;
}
#包括
#包括
#定义NUM_IMGS 7
int main(int argc,char*argv[])
{
如果(argc<8)
{
printf(“用法:%s\n”,argv[0]);
返回-1;
}
//用于存储图像指针的数组
IplImage*图像[NUM_IMGS]={0};
对于(int i=0;i#include <cv.h>
#include <highgui.h>

#define NUM_IMGS 7

int main(int argc, char* argv[])
{
    if (argc < 8)
    {
        printf("Usage: %s <img1> <img2> <img3> <img4> <img5> <img6> <img7>\n", argv[0]);
        return -1;
    }

    // Array to store pointers for the images
    IplImage* images[NUM_IMGS] = { 0 };

    for (int i = 0; i < NUM_IMGS; i++)
    {
        // load image
        images[i] = cvLoadImage(argv[i+1], CV_LOAD_IMAGE_UNCHANGED);
        if (!images[i])
        {
            printf("!!! failed to load: %s\n", argv[i+1]);
            continue;
        }

        // display image in a window
        cvNamedWindow(argv[i+1], CV_WINDOW_AUTOSIZE); // creating a new window each time
        cvShowImage(argv[i+1], images[i]);

        // wait for keypress
        cvWaitKey(0);

        // close the window
        cvDestroyWindow(argv[i+1]);
        cvReleaseImage(&images[i]);
    }

    return 0;
}
for(...)
{
   if(!img)
       break;

   // display it
}
<!DOCTYPE html>
<html>
  <head>
    <style>img {display:block}</style>
    <meta http-equiv="refresh" content="5">
  </head>
  <body>
    <img src="subfolderA/img1.png" />
    <img src="subfolderB/img2.png" />
    <img src="subfolderC/img3.png" />
    <img src="subfolderD/img4.png" />
    <img src="subfolderE/img5.png" />
  </body>
</html>
#include "cv.h" // include standard OpenCV headers, same as before
#include "highgui.h"
#include <stdio.h>
#include <iostream>

using namespace cv; // all the new API is put into "cv" namespace. Export its content
using namespace std;

void help(){
  cout <<
  "\nThis program shows how to use cv::Mat and IplImages converting back and forth.\n"
  "Call:\n"
  "./image img1.png img2.png img3.png img4.png img5.png\n" << endl;
}

int main( int argc, char** argv ){
  help();
  namedWindow("Peephole", CV_WINDOW_AUTOSIZE);
  int i=0;
  while ((argc-1) > i){
    i++;
    const char* imagename = argv[i];
    Ptr<IplImage> iplimg = cvLoadImage(imagename); // Ptr<T> is safe ref-conting pointer              class
    if(iplimg.empty()){
        fprintf(stderr, "Can not load image %s\n", imagename);
        return -1;
    }
    Mat img(iplimg); // cv::Mat replaces the CvMat and IplImage, but it's easy to convert
    // between the old and the new data structures (by default, only the header

    // is converted, while the data is shared)

    if( !img.data ) // check if the image has been loaded properly
        return -1;
    // it's easy to pass the new matrices to the functions that only work with IplImage or CvMat:
    // step 1) - convert the headers, data will not be copied
    // this is counterpart for cvNamedWindow
    imshow("Peephole", img);
    waitKey();
  }
  destroyAllWindows();
  while (1) {
    waitKey(10);
  }
    // all the memory will automatically be released by Vector<>, Mat and Ptr<> destructors.
  return 0;
}
"""Check if window closed or key pressed"""
import cv2 as cv

img = cv.imread('image.jpg')
cv.imshow("MyWindow", img)

while cv.getWindowProperty("MyWindow", cv.WND_PROP_VISIBLE) > 0:
    if cv.waitKey(1000) > 0:
        break