OpenCV基础人脸检测(Harr级联实现)



#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <vector>  
#include <string> 
#include <opencv\cvaux.hpp>  
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;
double diffent_pixel_sum = 0;
/** Function Headers */
void detectAndDisplay(Mat frame);

/** Global variables */
//-- Note, either copy these two files from opencv/data/haarscascades to your current folder, or change these locations
String face_cascade_name = "E:\\opencv-for-window\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml";
//String eyes_cascade_name = "E:\\opencv-for-window\\opencv\\sources\\data\\haarcascades\\haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
void detectAndDisplay(Mat face) {



vector<Rect> faces;
Mat face_gray;

cvtColor(face, face_gray, CV_BGR2GRAY);  //rgb类型转换为灰度类型    
equalizeHist(face_gray, face_gray);   //直方图均衡化    
face_cascade.detectMultiScale(face_gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(50, 100));

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(face, center, Size(faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar(255, 0, 0), 2, 7, 0);
imshow("MyVideo", face);
}
}
int main() {
VideoCapture capture;
capture.open(0);
Mat frame;
if (!face_cascade.load(face_cascade_name)) {
printf("级联分类器错误,可能未找到文件,拷贝该文件到工程目录下!\n");
return -1;
}
for (;;) {
capture.read(frame);
detectAndDisplay(frame);
// imshow("MyVideo",frame);

if (waitKey(5) >= 0)
break;

}return 0;}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值