Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

openCV cv::mat release

Tags:

c++

opencv

When using openCV cv::Mat. http://docs.opencv.org/modules/core/doc/basic_structures.html I understand that some sort of smart pointers are being used. my question is, in order to do some memory optimization.
should I call cv::Mat release() in order to free unused matrices?
or should I trust the compiler to do it?

for example think of this code:

cv::Mat filterContours = cv::Mat::zeros(bwImg.size(),CV_8UC3);  
bwImg.release();
for (int i = 0; i < goodContours.size(); i++)
{
    cv::RNG rng(12345);
    cv::Scalar color = cv::Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
    cv::drawContours(filterContours,goodContours,i,color,CV_FILLED);        
}

/*% Fill any holes in the objects
bwImgLabeled = imfill(bwImgLabeled,'holes');*/


imageData = filterContours;
filterContours.release(); //should I call this line or not ?
like image 605
Gilad Avatar asked Oct 19 '22 10:10

Gilad


1 Answers

The cv::release() function releases the memory , which the destructor will take care of at the end of the scope of the Mat instance anyways. So you need not explicitly call it in the code snippet that you have posted. An example of when it would be needed is if the size of the Matrix can vary in different iterations within the same loop, i.e.,

using namespace cv;
int i = 0;
Mat myMat;
while(i++ < relevantCounter )
{
    myMat.create(sizeForThisIteration, CV_8UC1);

    //Do some stuff where the size of Mat can vary in different iterations\\

    mymat.release();
}

Here, using cv::release() saves the compiler from the overhead of creating a pointer in every loop

like image 81
R. S. Nikhil Krishna Avatar answered Oct 27 '22 20:10

R. S. Nikhil Krishna