与opencv CV混合的IplImage ::垫 [英] openCV mixing IplImage with cv::Mat

查看:109
本文介绍了与opencv CV混合的IplImage ::垫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经经历了OpenCV的一些内存管理ambiguitys。
你可以用新的OpenCV的C ++类做到这一点:

I have experienced some memory managment ambiguitys with opencv. You could do this with the new opencv c++ classes:

cv::Mat theimage = cvLoadImage("rawimage.pgm",CV_LOAD_IMAGE_ANYDEPTH);

现在我不明白,如果我这样做,我会得到一个错误:

now what I don t understand if I do the following I will get an error:

theimage.deallocate();

也许这是错误的。我做了一些实验,当我释放垫目标:

maybe this is wrong. I have made some experiments and when I release the Mat object:

theimage.release();

的IplImage对象仍然在存储器中。是否有某种方式来表达对CV ::垫目标摧毁的IplImage对象或者是我的code错误的第一行(因为我已经失去了的IplImage对象的指针)?
我看到了很多在互联网exemples人们使用code的第一线。许多解释如何转换,但没有人解释与内存会发生什么!

the IplImage object still remains in the memory. Is there some way to say to the cv::Mat object destroy the IplImage object or is my first line of code wrong (since there I have lost the pointer for the IplImage object) ? I have seen a lot of exemples in the internet where people use the first line of code. Many explains how to convert but no one explains what happens with the memory !

里边反问题是,我有一个正在使用的IplImage对象很多类(我使用启动我的项目)。我明白为什么CV ::垫比的IplImage更好。

Tthe problem is that I have many classes that are using the IplImage object (I started my project using that). And I understand why cv::Mat is better than IplImage.

- 编辑 -

在网上我发现下面的混合解决方案:

In the internet I have found following solution for the mixing:

cv::Ptr<IplImage> tmp = cvLoadImage("rawimage.pgm",CV_LOAD_IMAGE_ANYDEPTH);
cv::Mat theimage(tmp);

我觉得这可以解决我的一些问题,但让我的code有点难以阅读,并在我看来还是老样子危险。
如果TMP被CV ::垫释放之前,我会用一些损坏的对象(我没有t检验这一点,但我觉得它是如此)。最简单的方法是使用一个副本:

I think this could solve some of my problems but makes my code a bit unreadable and is in my opinion stil dangerous. If tmp gets deallocated before cv::Mat I will use some corrupt objects (I didn t test that but I think it is so). The easy way would be to use a copy:

cv::Mat theimage(tmp,true);

这是我找到的唯一解决办法,但我感觉错了...

this is the only solution that I found but for me it feels wrong...

推荐答案

模糊性来自一个extremelly可怕的做法:用混合的OpenCV的C ++接口C接口。不这样做,可以使用 CV :: imread()代替。

The ambiguity comes from an extremelly horrible practice: mixing the C interface with the C++ interface of OpenCV. Don't do it, use cv::imread() instead.

CV ::垫将需要的内存管理总是释放内存时除了当它是<一个href=\"http://docs.opencv.org/modules/core/doc/basic_structures.html?highlight=deallocate#mat-operator-iplimage\"相对=nofollow>从的IplImage 初始化,然后点击就看你用 DEALLOCATE释放它的资源() 。我写了一个简单的实验来验证此信息:

The destructor of cv::Mat will always deallocate memory when needed, except when it is initialized from a IplImage, then it's up to you to free it's resources with deallocate(). I wrote a simple experiment to verify this information:

void load()
{
    cv::Mat img = cvLoadImage("out.png", 1);

    std::cout << "* IMG was loaded\n";
    getchar();

    img.deallocate();
}

int main()
{
    load();

    std::cout << "* IMG was deallocated\n";
    getchar();

    return 0;
}

的getchar()通话暂停程序,使您可以检查应用程序的内存占用。如果您注释掉 DEALLOCATE()你会发现,内存占用不加载图像后下降。

The getchar() calls pause the program so you can check the memory footprint of the application. If you comment out deallocate() you'll notice that the memory footprint doesn't decrease after loading the image.

在一个侧面说明,<一个href=\"http://docs.opencv.org/modules/core/doc/basic_structures.html?highlight=deallocate#mat-release\"相对=nofollow> 垫::发布() 仅减少引用计数器,如果需要重新分配该数据(但它也不会在情况下,释放从初始化的IplImage )。

On a side note, Mat::release() only decreases the reference counter and deallocates the data if needed (but it will also not deallocate in case the Mat was initialized from IplImage).

这篇关于与opencv CV混合的IplImage ::垫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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