haar detection demo code

#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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值