cv :: Mat的免费内存使用FileStorage API加载 [英] Free memory of cv::Mat loaded using FileStorage API

查看:652
本文介绍了cv :: Mat的免费内存使用FileStorage API加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用OpenCV FileStorage API连续加载和释放OpenCV矩阵的系统。这些矩阵保存图像特征描述符。具体来说,这段代码是这样的:

I'm working on a system that continuously loads and releases OpenCV Matrices using OpenCV FileStorage API. These matrices hold images feature descriptors. In concrete the piece of code that does the job is this one:

descriptors.create(rows, cols, type);

...

for (cv::FileNodeIterator it = keypointsSequence.begin(); it != keypointsSequence.end(); it++, idx++) {

    ...

    cv::Mat featureVector;
    (*it)["descriptor"] >> featureVector;

    featureVector.copyTo(descriptors.row(idx));
    featureVector.release();
}

正如我从 this post a cv :: Mat 仅当使用 Mat :: create 创建时才会释放。虽然我使用的是create方法,但其内容是从加载的矩阵中复制的。

As I understand from this post a cv::Mat is released only when it was created using Mat::create. Though I'm using the create method, its content is being copied from the loaded matrix.

我有几个问题:


  1. 是否正确释放 featureVector ,因此其内存被释放?

  2. 在循环中如何正确定义 featureVector

  1. Is featureVector being correctly released and hence its memory deallocated? Or do I have to delete it manually?
  2. How correct is it defining featureVector inside the loop? And how does it impact the overall performance (specially in memory)?

我非常感谢您对这个问题的深入了解。

I would strongly appreciate any insight about this issues.

推荐答案


  1. 您提及的内容显示如何手动释放Mat,意味着你需要自己释放它。 Mat是一个智能指针,它本身处理各种释放。

  1. Post you mention show how Mat 'can' be released manually, but it doesn't mean that you 'need' to release it yourself. Mat is a smart pointer that deals with all kinds of deallocation by itself. Personally I never had a need to use release function myself (and I am using OpenCV for years).

只有当它的大小改变时,它才会重新分配它的内存。而事实并非如此。因此,在循环之外定义它将防止大量不必要的分配和释放。

It reallocates its memory only if its size is changed. And that is not the case. So defining it outside of the loop will prevent a lot of unneeded allocation and deallocations.

临时Mat(featureVector)而不是直接写入descriptors.row(idx)?

Besides, why are you using temporary Mat (featureVector) instead of writing directly to descriptors.row(idx)?

编辑(对注释3的响应):

Edit (response to comment 3):

您的错误看起来很奇怪。只是为了确保代码工作正常我写了非常短的测试代码,它的工作原理。这里是:

Your error looks weird. Just to be sure that the code is working fine I wrote very short test code and it worked as it should. Here it is:

Mat src = imread(argv[1],-1);

FileStorage fs("test.yml", FileStorage::WRITE);
fs << "imgs" << "[" << "{" << "img" << src.row(500) << "}" << "]";
fs.release();

FileStorage fs2("test.yml", FileStorage::READ);
Mat src2(src.size(), src.type(), Scalar(0));
FileNode imgsNode= fs2["imgs"];
for(FileNodeIterator it = imgsNode.begin(); it != imgsNode.end(); ++it )
    (*it)["img"] >> src2.row(500);
fs2.release();

imwrite("res.pgm",src2);

您可以看到不需要临时图片。

As you can see no temporary image is needed.

这篇关于cv :: Mat的免费内存使用FileStorage API加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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