zoukankan      html  css  js  c++  java
  • OpenCV视频检测人脸

    用opencv自带xml。打开摄像头,实时视频检测人脸

    可以直接运行在linux下或者Windows下的VS中

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <ctime>
     4 #include <opencv2/opencv.hpp>
     5 #include <opencv2/highgui/highgui.hpp>
     6 #include <opencv2/imgproc/imgproc.hpp>
     7 #include "opencv2/core/core.hpp"
     8 using namespace cv;
     9 using namespace std;
    10 
    11 #define W 1920
    12 #define H 1080
    13 
    14 string face_cascade_name = "E:/opencv_VS/opencv/sources/data/haarcascades/haarcascade_frontalface_alt2.xml";
    15 CascadeClassifier face_cascade;
    16 int facesnum = 0;
    17 void DectectorAndDis(Mat frame)
    18 {
    19     Mat face_gray = Mat::zeros(H, W, CV_8UC3);
    20     vector<Rect> faces;
    21     cvtColor(frame, face_gray, CV_BGR2GRAY);//RGB转化为灰度
    22     equalizeHist(face_gray, face_gray);//直方图均衡化
    23     double t = (double)getTickCount();
    24     face_cascade.detectMultiScale(face_gray, faces, 1.75, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
    25     t = ((double)getTickCount() - t)/getTickFrequency();
    26     
    27     for (int i = 0; i < faces.size(); i++){
    28         Point CvBox2D(int(faces[i].x + faces[i].width*0.5), int(faces[i].y + faces[i].height*0.5));
    29         ellipse(frame, CvBox2D, Size(int(faces[i].width*0.5), int(faces[i].height*0.5)), 0, 0, 360, Scalar(255, 0, 0), 4, 8, 0);    
    30      
    31     }
    32     if( facesnum != faces.size())
    33     {
    34         cout << "人脸数:" << faces.size() << endl;
    35         facesnum = faces.size();
    36         printf("本次检测使用: %0.2f ms
    ",t*1000);
    37     }
    38     
    39     imshow("读取视频", frame);
    40 }
    41 int main(int argc, char* argv[])
    42 {    
    43     VideoCapture capture;    
    44     capture.open(0);
    45     Mat frame = Mat::zeros(H, W, CV_8UC3);
    46     Mat frameaftcanny = Mat::zeros(H, W, CV_8UC3);
    47     face_cascade.load(face_cascade_name);
    48     while (1)
    49     {
    50         capture >> frame;
    51         //cv::Canny(frame, frameaftcanny, 100, 300, 3);
    52         //imshow("边缘检测", frameaftcanny);
    53         DectectorAndDis(frame);
    54         cv::waitKey(20);
    55     }
    56 
    57     return 0;
    58 }
    View Code
  • 相关阅读:
    C# 语言基础(++和--运算)
    Android RSA加密解密
    laravel redis
    larave5.1l队列
    shell更改目录编码
    Linux的权限说明
    MySQL主从架构之Master-Master互为主备
    php curl流方式远程下载大文件
    php session 跨页失效问题
    mysql中文字段转拼音首字母,以及中文拼音模糊查询
  • 原文地址:https://www.cnblogs.com/duanjinjie/p/9157204.html
Copyright © 2011-2022 走看看