openCV cv::mat 发布

Gil*_*lad 5 c++ opencv

使用 openCV cv::Mat 时。 http://docs.opencv.org/modules/core/doc/basic_structures.html 我知道正在使用某种智能指针。我的问题是,为了做一些内存优化。
我应该调用 cv::Mat release() 以释放未使用的矩阵吗?
还是我应该相信编译器来做?

例如,想想这段代码:

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 ?
Run Code Online (Sandbox Code Playgroud)

R. *_*hna 3

该函数释放内存,无论如何,析构函数将在Matcv::release()实例范围的末尾处理该内存。因此,您无需在发布的代码片段中显式调用它。何时需要它的一个例子是矩阵的大小是否可以在同一循环内的不同迭代中变化,即

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();
}
Run Code Online (Sandbox Code Playgroud)

在这里,使用 cv::release() 可以节省编译器在每个循环中创建指针的开销