Mat confusion

103 views
Skip to first unread message

Vlad

unread,
Aug 1, 2012, 3:10:02 PM8/1/12
to android...@googlegroups.com
Hi guys,
I'm a bit confused. i hope you can help me out.

I have a matrix called rgba.
i want to transpose it, and then, flip it on both axis.

        Mat rgba = org.opencv.android.Utils.bitmapToMat(frame);
       
Mat flipped = new Mat();
       
       
if (!Utils.isInLandscape()){
           
/* portrait - transpose than flip twice */
           
Mat transposed = rgba.t();
           
Mat flipped2 = new Mat();
           
           
Core.flip(transposed, flipped2, 0);
           
Core.flip(flipped2, flipped, 1);
   
            flipped2
.release();
            transposed
.release();
...
}
...
do something with flipped...

flipped
,release();
rgba
.release()


this is my code, but i have a feeling that i'm doing it wrong.. there are too many matrices and too many releases going on..
so, is this right? and in general when should i release a matrix, and does the CV wrapper, releases native objects by itself, or is it my responsibility?

any help would be appreciated.
thanks,
Vlad

Daniil Osokin

unread,
Aug 2, 2012, 8:28:11 AM8/2/12
to android...@googlegroups.com
Hi, Vlad.

Flip in both axes you can do, like Core.flip(transposed, flipped, -1).
Negative parameter means flipping in both axes (zero - around x-axis other positive - in y-axis).

About manually invoke release() method docs says:
This method can be called manually to force the matrix data deallocation.
But since this method is automatically called in the destructor,
or by any other method that changes the data pointer, it is usually not needed.

Vlad

unread,
Aug 2, 2012, 10:32:34 AM8/2/12
to android...@googlegroups.com
Thanks, i've read the manual as well. but what do they mean "usually not needed".
when is it needed? in the face recognition example for instance, they do use it manually.. so i'm still a bit confused :)

בתאריך יום חמישי, 2 באוגוסט 2012 15:28:11 UTC+3, מאת Daniil Osokin:

Daniil Osokin

unread,
Aug 3, 2012, 2:09:01 AM8/3/12
to android...@googlegroups.com
Mat has own reference counter, so basic rule here is: if you don't manually increase this counter, you needn't to manually invoke release method. By the way, release method decrements ref counter and release memory if needed. So, in general, invoking release method doesn't mean direct deallocating memory.

Andrey Pavlenko

unread,
Aug 3, 2012, 8:46:15 AM8/3/12
to android...@googlegroups.com
Mat::release() is called automatically when Java Garbage Collector deletes the Java Mat object.
But Mat object usually keeps much memory on native level and is very small on Java side. 
So it can happen that Java sees no significant (Java) memory utilization, while it's high on native side.

So a general rule is: if you know that after some particular code line the particular Mat object is unneeded - you'd better call release() method, or leave it as is if not sure.

Vlad

unread,
Aug 3, 2012, 9:09:47 AM8/3/12
to android...@googlegroups.com
Great, understood :)
thanks!

בתאריך יום שישי, 3 באוגוסט 2012 15:46:15 UTC+3, מאת Andrey Pavlenko:
Reply all
Reply to author
Forward
0 new messages