在opencv泄漏内存中使用CascadeClassifier的detectMultiScale吗?

时间:2011-12-03 19:00:54

标签: c++ memory-leaks opencv

正如标题所说。我正试图在图像上运行一个简单的面部检测器:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect/objdetect.hpp>

using namespace cv;

int main(int argc, char** argv)
{
  Mat image = imread("V2.jpg", 1);
  CascadeClassifier face_cascade;
  face_cascade.load("haarcascade_frontalface_alt.xml");
  vector<Rect> faces;
  face_cascade.detectMultiScale(image, faces);
  return 0;
}

根据valgrind,以下代码从detectMultiScale函数泄漏。我在这里忽略了一个好习惯吗?什么东西要发布?从逻辑上讲,一切都在我的结尾,所以它应该在程序结束时释放。

valgrind的输出是:

==4852==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
==4852==    by 0x4EB1D90: cv::fastMalloc(unsigned long) (in /usr/lib/libopencv_core.so.2.3.1)
==4852==    by 0x58F175D: ??? (in /usr/lib/libopencv_objdetect.so.2.3.1)
==4852==    by 0x58F8699: cvHaarDetectObjectsForROC(void const*, CvHaarClassifierCascade*, CvMemStorage*, std::vector<int, std::allocator<int> >&, std::vector<double, std::allocator<double> >&, double, int, int, CvSize, CvSize, bool) (in /usr/lib/libopencv_objdetect.so.2.3.1)
==4852==    by 0x58EA38B: cv::CascadeClassifier::detectMultiScale(cv::Mat const&, std::vector<cv::Rect_<int>, std::allocator<cv::Rect_<int> > >&, std::vector<int, std::allocator<int> >&, std::vector<double, std::allocator<double> >&, double, int, int, cv::Size_<int>, cv::Size_<int>, bool) (in /usr/lib/libopencv_objdetect.so.2.3.1)
==4852==    by 0x58DA6B5: cv::CascadeClassifier::detectMultiScale(cv::Mat const&, std::vector<cv::Rect_<int>, std::allocator<cv::Rect_<int> > >&, double, int, int, cv::Size_<int>, cv::Size_<int>) (in /usr/lib/libopencv_objdetect.so.2.3.1)

这是在Windows 7 64位上运行64位Kubuntu 11.10的VMware VM上运行的。 OpenCV版本是最新版本 - 2.3.1。

2 个答案:

答案 0 :(得分:0)

您必须释放变脸

答案 1 :(得分:0)

我不确定Valgrind的错误信息,但如果有泄漏,那肯定不在您的代码中。尝试更新到2.4.2,它也快得多。