2015-05-29 613 views
3

当使用openCV cv :: Mat。 http://docs.opencv.org/modules/core/doc/basic_structures.html 我知道某些智能指针正在被使用。我的问题是 ,为了做一些内存优化。
我应该调用cv :: Mat release()来释放未使用的矩阵吗?
还是我应该相信编译器来做到这一点?openCV 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 ? 
+2

当矩阵超出范围时,它将被释放(除非该矩阵的数据有不同的引用) – Micka

+0

@Micka如果这是一个计算时间很长的非常长的函数,会发生什么情况。它会帮助释放吗? – Gilad

+1

它会给你一些回忆,这可能会帮助你或者可能不会帮你 – Micka

回答

2

cv::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(); 
} 

在这里,用CV ::释放()从的开销节省编译器在每个循环中创建一个指针