zoukankan      html  css  js  c++  java
  • opencv 实现人脸检测(harr特征)


    我这里用的是已经训练好的haar级联分类器。

    眼睛检测 haarcascade_eye_tree_eyeglasses.xml
    人脸检测 haarcascade_frontalface_alt2.xml
    检测思路:
    先把图片转为灰度,接着将图片直方均匀化,在上面处理后的图片矩阵中检测脸的区域,然后把脸这一块圈出来去检测眼睛。
    检测函数代码如下:
    #include <opencv/cv.h> 
    #include <opencv/highgui.h>
    #include <opencv2/opencv.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/core/core.hpp>
    #include <opencv2/imgproc/imgproc.hpp>
    using namespace std;
    using namespace cv;
    void DetectFace(Mat, Mat);
    CascadeClassifier faceCascade;
    CascadeClassifier eyes_Cascade;
    int main(int argc, char** argv) {
    	VideoCapture cap;
    	if (!cap.open(0)) {
    		cout << "摄像头打开失败!!" << endl;
    		return -1;
    	}
    	if (!faceCascade.load("E:\VS2015Opencv\opencv3.2\opencv\build\etc\haarcascades\haarcascade_frontalface_alt2.xml")) {
    		cout << "人脸检测级联分类器没找到!!" << endl;
    		return -1;
    	}
    	if (!eyes_Cascade.load("E:\VS2015Opencv\opencv3.2\opencv\build\etc\haarcascades\haarcascade_eye_tree_eyeglasses.xml")) {
    		cout << "眼睛检测级联分类器没找到!!" << endl;
    		return -1;
    	}
    	Mat img, imgGray;
    	int fps = 60;
    	while (true) {
    		cap >> img;
    		cvtColor(img, imgGray, CV_BGR2GRAY);
    		equalizeHist(imgGray, imgGray);//直方图均匀化
    		DetectFace(img, imgGray);
    		waitKey(1000 / fps);
    	}
    	return 0;
    }
    
    void DetectFace(Mat img, Mat imgGray) {
    	namedWindow("src", WINDOW_AUTOSIZE);
    	vector<Rect> faces, eyes;
    	faceCascade.detectMultiScale(imgGray, faces, 1.2, 5, 0, Size(30, 30));
    	for (auto b : faces) {
    		cout << "输出一张人脸位置:(x,y):" << "(" << b.x << "," << b.y << ") , (width,height):(" << b.width << "," << b.height << ")" << endl;
    	}
    	if (faces.size()>0) {
    		for (size_t i = 0; i<faces.size(); i++) {
    			putText(img, "ugly girl!", cvPoint(faces[i].x, faces[i].y - 10), FONT_HERSHEY_PLAIN, 2.0, Scalar(0, 0, 255));
    
    			rectangle(img, Point(faces[i].x, faces[i].y), Point(faces[i].x + faces[i].width, faces[i].y + faces[i].height), Scalar(0, 0, 255), 1, 8);
    			cout << faces[i] << endl;
    			//将人脸从灰度图中抠出来
    			Mat face_ = imgGray(faces[i]);
    			eyes_Cascade.detectMultiScale(face_, eyes, 1.2, 2, 0, Size(30, 30));
    			for (size_t j = 0; j < eyes.size(); j++) {
    				Point eye_center(faces[i].x + eyes[j].x + eyes[j].width / 2, faces[i].y + eyes[j].y + eyes[j].height / 2);
    				int radius = cvRound((eyes[j].width + eyes[j].height)*0.25);
    				circle(img, eye_center, radius, Scalar(65, 105, 255), 4, 8, 0);
    			}
    		}
    	}
    	imshow("src", img);
    }
    

      

     
  • 相关阅读:
    Maven相关
    Git学习:如何登陆以及创建本地代码仓库、并提交本地代码至Github(最简单方法)
    Git的安装与配置
    下载网址
    .net core 2.2 部署CentOS7(5)部署.net core mvc
    .net core 2.2 部署CentOS7(4)CentOS7下载并安装.NET SDK(软件开发工具包)
    .net core 2.2 部署CentOS7(3)安装Xshell操控CentOS7
    .net core 2.2 部署CentOS7(2)给虚拟机安装CentOS7
    .net core 2.2 部署CentOS7(1)安装虚拟机
    Winfrom 基于TCP的Socket服务端 多线程(进阶版)
  • 原文地址:https://www.cnblogs.com/fcfc940503/p/11342488.html
Copyright © 2011-2022 走看看