Skip to content

Instantly share code, notes, and snippets.

@kevinkindom
Created November 29, 2015 10:39
Show Gist options
  • Select an option

  • Save kevinkindom/a66ab7365b8d1e456b22 to your computer and use it in GitHub Desktop.

Select an option

Save kevinkindom/a66ab7365b8d1e456b22 to your computer and use it in GitHub Desktop.

Revisions

  1. kevinkindom created this gist Nov 29, 2015.
    53 changes: 53 additions & 0 deletions OpenCV Face Detect.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    #include <opencv2/core/core.hpp>
    #include <opencv2/objdetect/objdetect.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/imgproc/imgproc.hpp>

    #include <iostream>
    #include <stdio.h>

    using namespace std;
    using namespace cv;

    string face_cascade_name = "haarcascade_frontalface_alt.xml";
    CascadeClassifier face_cascade;

    void detectAndDisplay(Mat frame);

    int main(int argc, char** argv)
    {
    Mat image = imread("face.png");

    if (!image.data)
    {
    printf("[ERR] 加载图片失败");
    }

    if (!face_cascade.load(face_cascade_name))
    {
    printf("[ERR] 无法加载级联分类器文件!\n");
    }

    detectAndDisplay(image);

    waitKey(0);
    }

    void detectAndDisplay(Mat frame)
    {
    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 * .5, faces[i].y + faces[i].height * .5);
    ellipse(frame, center, Size(faces[i].width * .5, faces[i].height * .5), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);
    }

    imshow("人脸识别", frame);
    }