OpenCV cv :: matte release

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

for example, think about 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 ?

      

+3


source to share


1 answer


The function cv::release()

frees memory which the destructor will take care of at the end of the scope of the Mat instance anyway. Therefore, you do not need to explicitly name it in the code snippet you posted. An example of when this will be necessary is that 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 cv :: release () saves the compiler from the overhead when creating the pointer in each loop

+2


source







All Articles