(VS2017+OPENCV3.3.1)人臉檢測,detectMultiScale內存泄漏處理方法

一、人臉識別代碼

#include "opencv2/objdetect.hpp"

#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
#include "stdafx.h"

using namespace std;
using namespace cv;

//人臉識別的步驟
//1、打開攝像頭
//2、加載攝像頭參數
//3、加載分類器
//4、識別
//5、畫圖
//6、顯示

class face_detect
{
public:
face_detect();
~face_detect();
int open_carmera();
int load_carmera_data();
int load_classifier_data(CascadeClassifier& type_classifier, string path);
int show_carmera();
int face_recognition(CascadeClassifier& type_classifier, vector<Rect>& type);
int drawing(vector<Rect>& type);
private:
VideoCapture cap;
Mat frame;
CascadeClassifier face_classifier;
vector<Rect> face, face2;
};

face_detect::face_detect()
{
}

face_detect::~face_detect()
{
}

//1、打開攝像頭
int face_detect::open_carmera()
{
cap.open(0);
if (!cap.isOpened())
{
printf("cant open the camera\n");
return 1;
}
return 0;
}
//2、獲取攝像頭捕抓的幀
int face_detect::load_carmera_data()
{
int ret = 0;
cap >> frame;
if (frame.empty())
{
printf("no capture the video");
ret = 1;
return ret;
}
return ret;
}
//3、加載分類器
int face_detect::load_classifier_data(CascadeClassifier& type_classifier, string path)
{
int ret = 0;
ret |= type_classifier.load(path);
if (!ret)
{
printf("load face classifier fail");
ret = 1;
return ret;
}
return ret;
}
//4、檢測
int face_detect::face_recognition(CascadeClassifier& type_classifier, vector<Rect>& type)
{
int ret = 0;
Mat gray;
//轉換成灰度圖
cvtColor(frame, gray, COLOR_BGR2GRAY);
//imshow("live", frame);
//直方均橫
equalizeHist(gray, gray);
//人臉識別
type_classifier.detectMultiScale(gray, type,1.1, 2, 0| CASCADE_SCALE_IMAGE,Size(30, 30));
printf("size : %d ", type.size());
return ret;
}
//5、畫圖
int face_detect::drawing(vector<Rect>& type)
{
int ret = 0;
printf("size : %d ", type.size());
for (size_t i = 0; i < type.size(); i++)
{
printf("width : %d  ,height : %d\n", type[i].width, type[i].height);
if (type[i].width > 0 && type[i].height > 0)
{
Point p1 = Point(type[i].x, type[i].y);
Point p2 = Point(type[i].x + type[i].width, type[i].y + type[i].height);
rectangle(frame, p1, p2, Scalar(255, 0, 0), 1, 8, 0);
}
}
return ret;
}

//6、顯示圖像
int face_detect::show_carmera()
{
int ret = 0;
namedWindow("live", WND_PROP_FULLSCREEN);
imshow("live", frame);
return ret;
}

int main()
{
printf("hello world\n");
bool stop = false;
int ret = 0;
face_detect detect;
//人臉相關
CascadeClassifier face_classifier;
vector<Rect> face;
string face_classifier_path = "D:/AI/OPENCV/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml";
//人眼相關
CascadeClassifier eye_classifier;
vector<Rect> eye;
string eye_classifier_path = "D:/AI/OPENCV/opencv/sources/data/haarcascades/haarcascade_eye_tree_eyeglasses.xml";

ret |= detect.open_carmera();
while (!stop)
{
ret |= detect.load_carmera_data();
//人臉識別
ret |= detect.load_classifier_data(face_classifier, face_classifier_path);
ret |= detect.face_recognition(face_classifier,face);
ret |= detect.drawing(face);
//人眼識別
ret |= detect.load_classifier_data(eye_classifier, eye_classifier_path);
ret |= detect.face_recognition(eye_classifier, eye);
ret |= detect.drawing(eye);

ret |= detect.show_carmera();
//按p時退出
if(waitKey(20)>= 0)  stop = true;
}

}

二、處理detectMultiScale內存泄露方法

2.1 選擇debug x64


2.2 選擇輸入的lib文件時,只選擇debug的庫文件的opencv_world331d.lib,即opencv_world331d.lib,不選擇release 的庫文件opencv_world331.lib,即opencv_world331.lib



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章