Jords
Jords

Reputation: 1875

Explicitly releasing Mat with opencv 2.0

I'm working on a program where we do some image processing of full quality camera photos using the Android NDK. So, obviously memory usage is a big concern.

There are times where I don't need the contents of a Mat anymore - I know that it'll be released automatically when it goes out of scope, but is there a good way of releasing it earlier, so I can reduce the memory usage?

It's running fine on my Galaxy S II right now, but obviously that is not representative of the capabilities of a lot of the older phones around!

Upvotes: 3

Views: 3716

Answers (2)

Sam
Sam

Reputation: 20056

If you have only one matrix pointing to your data, you can do this trick:

Mat img = imread("myImage.jpg");
// do some operations
img = Mat(); // release it

If more than one Mat is pointing to your data, what you should do is to release all of them

Mat img = imread("myImage.jpg");

Mat img2 = img;
Mat roi = img(Rect(0,0,10,10));
// do some operations

img = Mat(); // release all of them
img2 = Mat();
roi = Mat();

Or use the bulldozer approach: (Are you sure? this sounds like inserting bugs in your code )

Mat img = imread("myImage.jpg");

Mat img2 = img;
Mat roi = img(Rect(0,0,10,10));
// do some operations
char* imgData = (char*)img.data;

free[] imgData;

imshow("Look, this is called access violation exception", roi);

Upvotes: 3

cape1232
cape1232

Reputation: 1009

Mat::release() should do the trick.

cf.: OpenCV Memory Management Documentation

Upvotes: 3

Related Questions