17806

Free memory of cv::Mat loaded using FileStorage API

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

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.

I have a couple of questions:

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

I would strongly appreciate any insight about this issues.

Answer1:

<ol> <li>

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).

</li> <li>

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.

</li> </ol>

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

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.

Recommend

  • Deep cloning objects
  • OpenCV Java, Getting region of interest from image
  • Converting a BitArray in to UInt32 C# [duplicate]
  • HDFS resiliency to machine restarts in DC/OS
  • Is it OK to write code after [super dealloc]?
  • How can i display a PDF file in an Android ImageView
  • iAd works in iPhone but not iPad
  • Example for subscribing for BLE indicate in android
  • Erlang raise number of opened sockets, MacOS and CentOs
  • rselenium | get youtube page source
  • How to share a database between ASP.NET MVC 5 application and .NET console application?
  • C# Networkstream reads nothing
  • Why is django manage.py syncdb failing to create new columns on my development server?
  • How to use animated gif in Firemonkey?
  • ImportError: cannot import name Pubnub
  • Debugging VB6 Code From Visual Studio 2010
  • how does System.Web.HttpRequest::PathInfo work?
  • Not able to aggregate on nested fields in elasticsearch
  • Ajax Loaded meta Tags
  • Spring security and special characters
  • Align navbar back button on right side
  • DotNetZip - Calculate final zip size before calling Save(stream)
  • Incrementing object id automatically JS constructor (static method and variable)
  • Running a C# exe file
  • Trying to switch camera back to front but getting exception
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • File upload with ng-file-upload throwing error
  • AngularJs get employee from factory
  • Load html files in TinyMce
  • Angular 2 constructor injection vs direct access
  • Memory offsets in inline assembly
  • Java static initializers and reflection
  • Turn off referential integrity in Derby? is it possible?
  • unknown Exception android
  • Programmatically clearing map cache
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize