Opencv 实现人脸检测

Opencv 实现人脸检测,第1张

        haarcascade_frontalface_alt2.xml 文件在安装 Opencv 环境时应该已经下载,可以在安装目录查找,以 Windows 为例,在 opencv\sources\data\haarcascades\ 文件夹下,相关是的使用方法在 opencv\sources\samples\ 文件下,下面的代码是根据 opencv\sources\samples\cpp\facedetect.cpp 修改而来,仅供参考。

#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include 

using namespace std;
using namespace cv;


void detectAndDraw(Mat& img, CascadeClassifier& cascade, double scale, bool tryflip);

int main(int argc, const char** argv)
{
    bool tryflip = false;
    double scale = 1;
    string cascadeName = "Opencv\\opencv-4.5.2\\sources\\data\\haarcascades\\haarcascade_frontalface_alt2.xml";
    CascadeClassifier cascade;

    if (!cascade.load(cascadeName))
    {
        cerr << "ERROR: Could not load classifier cascade" << endl;
        return -1;
    }

    Mat image = imread("images201020074223525_C420111005003.jpg", IMREAD_COLOR);
    if (image.empty())
    {
        cout << "Couldn't read image" << endl;
        return 1;
    }

    if (!image.empty())
    {
        detectAndDraw(image, cascade, scale, tryflip);
        waitKey(0);
    }

    return 0;
}

void detectAndDraw(Mat& img, CascadeClassifier& cascade, double scale, bool tryflip)
{
    double t = 0;
    vector faces, faces2;
    const static Scalar colors[] =
    {
        Scalar(255,0,0),
        Scalar(255,128,0),
        Scalar(255,255,0),
        Scalar(0,255,0),
        Scalar(0,128,255),
        Scalar(0,255,255),
        Scalar(0,0,255),
        Scalar(255,0,255)
    };
    Mat gray, smallImg;

    cvtColor(img, gray, COLOR_BGR2GRAY);
    double fx = 1 / scale;
    resize(gray, smallImg, Size(), fx, fx, INTER_LINEAR_EXACT);
    equalizeHist(smallImg, smallImg);

    t = (double)getTickCount();
    cascade.detectMultiScale(smallImg, faces,
        1.1, 2, 0
        //|CASCADE_FIND_BIGGEST_OBJECT
        //|CASCADE_DO_ROUGH_SEARCH
        | CASCADE_SCALE_IMAGE,
        Size(30, 30));
    if (tryflip)
    {
        flip(smallImg, smallImg, 1);
        cascade.detectMultiScale(smallImg, faces2,
            1.1, 2, 0
            //|CASCADE_FIND_BIGGEST_OBJECT
            //|CASCADE_DO_ROUGH_SEARCH
            | CASCADE_SCALE_IMAGE,
            Size(30, 30));
        for (vector::const_iterator r = faces2.begin(); r != faces2.end(); ++r)
        {
            faces.push_back(Rect(smallImg.cols - r->x - r->width, r->y, r->width, r->height));
        }
    }
    t = (double)getTickCount() - t;
    printf("detection time = %g ms\n", t * 1000 / getTickFrequency());
    for (size_t i = 0; i < faces.size(); i++)
    {
        Rect r = faces[i];
        Mat smallImgROI;
        vector nestedObjects;
        Point center;
        Scalar color = colors[i % 8];
        int radius;

        double aspect_ratio = (double)r.width / r.height;
        if (0.75 < aspect_ratio && aspect_ratio < 1.3)
        {
            center.x = cvRound((r.x + r.width * 0.5) * scale);
            center.y = cvRound((r.y + r.height * 0.5) * scale);
            radius = cvRound((r.width + r.height) * 0.25 * scale);
            circle(img, center, radius, color, 3, 8, 0);
        }
        else
            rectangle(img, Point(cvRound(r.x * scale), cvRound(r.y * scale)),
                Point(cvRound((r.x + r.width - 1) * scale), cvRound((r.y + r.height - 1) * scale)),
                color, 3, 8, 0);
    }
    imshow("result", img);
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/662351.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-18
下一篇 2022-04-18

发表评论

登录后才能评论

评论列表(0条)

保存