OpenCV的 - 关闭形象展示的窗口 [英] OpenCV - closing the image display window

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

问题描述

我做的一个项目,通过图像数据库检索,当我发现的结果,某些查询 - 5数据库的图像,我想直观地显示结果。我不保存在内存中的所有图像,所以我必须做的,以显示它首先加载图像。

I am doing on a project for searching through an image database, and when I find the results to some query - 5 database images, I would like to display the results visually. I do not keep all the images in memory, so I have do load the image first in order to display it.

我脑海中还有简单,伪code:

I had something simple in mind, in pseudocode:

for image 1..5
    load images
    display image in a window
    wait for any keypress
    close the window

下面是我的code在 C ++代码段使用 OpenCV的为此:

Here's a snippet of my code in C++ using OpenCV for this purpose:

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);
}

图片这里使用数组本身并不在我的code存在的,但问题的缘故,它包含相对于图像的文件名在当前程序的运行点如果名称成员。我不同的存储图像的名字有点在我的项目。

The images array used here does not as such exist in my code, but for the sake of the question, it contains the File Names of the images relative to the current program running point if its name member. I store the image names a bit differently in my project.

在code以上的几乎的工作原理:我可以通过4/5的图像重复行,但在显示最后的图像和关键是pressed,图像变为灰色和我不能关闭图像窗口withouth的崩溃我的应用程序的其余部分。

The code above almost works: I can iterate through 4/5 images OK, but when last image is displayed and a key is pressed, the image goes gray and I can not close the image window withouth crashing the rest of my application.

我的第一个想法是,becouse编译时优化, cvReleaseImage cvDestroyWindow 完成之前释放的形象,而且不知何故使得它冻结。但是,我已经尝试加入一些等待时间(因此注释掉我的code的睡眠(1)线),并没有帮助。

My first idea was that becouse of compile-time optimizations, cvReleaseImage releases the image before cvDestroyWindow is finished, and that somehow makes it freeze. But, I've tried adding some waiting time (hence the commented out sleep(1) line of my code) and it didn't help.

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

I am calling this display functionality from my console application, and when the image freezes, the control returns back to my application and I can keep using it (but the image window is still frozen in the background).

您可以给我如何解决这一问题有什么建议?

Can you give me any suggestions on how to fix this?

修改

因为问的问题我已经跟一些人打交道定期计算机视觉和OpenCV,仍然没有想法。

I have talked to some people dealing with computer vision and OpenCV on a regular basis since asking the question, and still no ideas.

我也发现了类似的计算器问题,但仍然没有公认的答案。 Googleing只是给了类似的问题作为一个结果,但没有答案。

I have also found a similar stackoverflow question, but there is still no accepted answer. Googleing just gives similar questions as a result, but no answers.

这是什么尝试(即使它们不是完整的解决方案),任何想法都非常AP preciated。

Any ideas on what to try (even if they are not the complete solution) are very much appreciated.

推荐答案

有关测试的目的,在下面的应用程序不正是你的问题说:它通过命令行装载7图片一接一个,并为每个图像是显示一个新的窗口。

For testing purposes, the application below does exactly what you stated in the question: it loads 7 images through the command line, one by one, and creates a new window for each image to be display.

这与OpenCV的2.3.1完美的作品在Linux上。

It works flawlessly with OpenCV 2.3.1 on Linux.

#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;
}

这篇关于OpenCV的 - 关闭形象展示的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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