正如标题所说。我正在尝试在图像上运行一个简单的人脸检测器:
#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)
这是在 64 位 Windows 7 上运行 64 位 Kubuntu 11.10 的 VMware VM 上运行的。 OpenCV 版本是最新的 - 2.3.1。
最佳答案
你必须释放可变面
关于c++ - 在opencv中使用CascadeClassifier的detectMultiScale是否会泄漏内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8370154/