• 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
  • 相关阅读:
    HDU 1698 Just a Hook (线段树模板题-区间求和)
    spring定时任务详解(@Scheduled注解)
    spring定时任务(@Scheduled注解)
    java反射实现接口重试
    微信开发者工具在线调试
    消息队列应用场景
    Redis、Memcache和MongoDB的区别
    下拉框多选实现回显及sql
    MySQL 中 You can't specify target table '表名' for update in FROM clause错误解决办法
    catch异常
  • 原文地址:https://www.cnblogs.com/duanjinjie/p/9157204.html
Copyright © 2020-2023  润新知