c++ - openCV cv::mat 发布

标签 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 ?

最佳答案

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

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::release() 可以节省编译器在每个循环中创建指针的开销

关于c++ - openCV cv::mat 发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30533287/

相关文章:

c++ - `std::move` 构造函数中的 Eigen 对象?

c++ - 虚函数和模板冲突

c++ - 以不同用户身份运行的两个 exe 如何使用 COM(组件对象模型)进行通信

python - 使用opencv使用fourcc编解码器h264和h265从帧中保存视频

c++ - 未定义模板的隐式实例化 : Boost Bug or Clang Bug?

C++ 当您从函数返回结构时,汇编中实际发生了什么?

windows - 是什么让 OpenCV 在 Windows 上如此庞大?我能做点什么吗?

c++ - opencv_helloworld.exe : 0xC0000005: Access violation reading location 0x00000004 中 0x778715de 的未处理异常

c++ - opencv中3x3矩阵的对数

c++ - 检测手中的手指