opencv使用默认的人脸和眼睛检测器


#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>

using namespace std;
using namespace cv;

int _tmain(int argc, _TCHAR* argv[])
{
    VideoCapture capture;
    Mat frame, image;
    string inputName;
    bool tryflip;
    CascadeClassifier cascade, nestedCascade;
    double scale;

    CascadeClassifier face_cascade;  
    CascadeClassifier eyes_cascade;  

    if(0==face_cascade.load("haarcascade_frontalface_alt.xml"))
    {
        cout << "load face xml err!" << endl;
        return -1;
    }

    if(0==eyes_cascade.load("haarcascade_eye_tree_eyeglasses.xml"))
    {
        cout << "load face xml err!" << endl;
        return -1;
    }

    image = imread( "./lena.jpg", 1 );
    if(image.empty()) cout << "Couldn't read ./lena.jpg" << endl;

    std::vector<cv::Rect> faces;  
    std::vector<cv::Rect> eyes;  

    face_cascade.detectMultiScale(image, faces, 1.1, 2, 0 | CASCADE_SCALE_IMAGE,Size(30, 30));  

    eyes_cascade.detectMultiScale(image, eyes, 1.1, 2, 0 | CASCADE_SCALE_IMAGE,Size(30, 30));  

    for (int i = 0; i < faces.size(); i++)  
    {  
        rectangle(image,Point(faces[i].x,faces[i].y),Point(faces[i].x+faces[i].width,faces[i].y+faces[i].height),Scalar(255,255,0));
    }

    for (int i = 0; i < eyes.size(); i++)  
    {  
        rectangle(image,Point(eyes[i].x,eyes[i].y),Point(eyes[i].x+eyes[i].width,eyes[i].y+eyes[i].height),Scalar(255,0,0));
    }

    imshow("Test",image);

    imwrite("Out.bmp",image);

    waitKey();

    return 0;
}

最终效果:
这里写图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值