haar detection demo code

来源:互联网 发布:北京房山网络职业学院 编辑:程序博客网 时间:2024/05/18 13:06
#include "stdafx.h"
#include <iostream>
#include <opencv2/imgproc/imgproc.hpp>  // Gaussian Blur
#include <opencv2/core/core.hpp>        // Basic OpenCV structures (cv::Mat, Scalar)
#include <opencv2/highgui/highgui.hpp>  // OpenCV window I/O
#include "opencv2/objdetect/objdetect.hpp"//人脸识别的接口


using namespace cv;//必须加入,否则无法检找到OPENCV的各个函数
using namespace std;




string face_cascade_name = "C:\\Users\\admin\\Desktop\\opencv_fdace\\face_detect\\face_detect\\haarcascade_frontalface_alt.xml";
CascadeClassifier face_cascade;
string window_name = "real time 人脸检测";
void detectAndDisplay(Mat frame){
std::vector<Rect> faces;
Mat frame_gray;


cvtColor(frame, frame_gray, CV_BGR2GRAY);
equalizeHist(frame_gray, frame_gray);


face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));


for (int i = 0; i < faces.size(); i++){
Point center(faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5);
ellipse(frame, center, Size(faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);
}


imshow(window_name, frame);
}


int main(int argc, _TCHAR* argv[])
{
VideoCapture cap(0); // open the default camera
if (!cap.isOpened())  // check if we succeeded
return -1;


Mat edges;
//namedWindow("edges", 1);
if (!face_cascade.load(face_cascade_name)){
printf("[error] 无法加载级联分类器文件!\n");
return -1;
}
int nTick = 0;
for (;;)
{
if (!cap.isOpened())
{//等等摄像头打开
continue;
}
Mat frame;
nTick = getTickCount();
cap >> frame; // get a new frame from camera
if (frame.data == NULL)
{//等到捕获到数据
continue;
}
cvtColor(frame, edges, CV_BGR2BGRA);


detectAndDisplay(edges);


if (waitKey(30) >= 0) break;
}
return 0;
}
0 0
原创粉丝点击