Opencv之简单人脸检测

话不多说,直接上代码:

#include "stdafx.h"

#include <opencv2\opencv.hpp>

 

using namespace cv;

 

int main()

{

    CascadeClassifier face_cascade;

    //use the haarcascade_frontalface_alt2.xml library

    face_cascade.load("E:\\Opencv\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt2.xml");

    const char* imagename = "F:\\Picture\\test.jpg";

    //从文件中读入图像

    Mat img = imread(imagename);

    //如果读入图像失败

    if (img.empty())

    {

        fprintf(stderr,"Can not load image %s\n", imagename);

        return -1;

    }

 

    //setup image files used in the process

    Mat grayscaleFrame;

    //convert captured image to gray scale and equalize

    cvtColor(img, grayscaleFrame, CV_BGR2GRAY);

    equalizeHist(grayscaleFrame,grayscaleFrame);

    //create a vector array to store the face found

    std::vector<Rect> faces;

    //find faces and store them in the vector array

    face_cascade.detectMultiScale(grayscaleFrame,faces, 1.1, 3, 0,Size(20, 20));

    //draw a rectangle for all found faces in the vectorarray on the original image

    for (int i = 0; i < faces.size(); i++)

    {

        Point pt1(faces[i].x + faces[i].width, faces[i].y + faces[i].height);

        Point pt2(faces[i].x, faces[i].y);

        rectangle(img, pt1, pt2, cvScalar(0,255, 255, 0), 1, 8, 0);

    }

 

    //print the output

    imshow("FaceRecognition", img);

    waitKey(0);

    return 0;

}

效果图:


关于函数:CV_WRAP void detectMultiScale( InputArray image,

                          CV_OUT std::vector<Rect>& objects,

                          double scaleFactor = 1.1,

                          int minNeighbors = 3,int flags = 0,

                          Size minSize =Size(),

                          Size maxSize =Size() );

说明如下:

 @brief Detects objects of different sizes inthe input image. The detected objects are returned as a listof rectangles.

    @param image Matrix of the type CV_8U containingan image where objects are detected.

    @param objects Vector of rectangles whereeach rectangle contains the detected object, therectangles may be partiallyoutside the original image.

    @param scaleFactor Parameter specifying howmuch the image size is reduced at each image scale.

    @param minNeighbors Parameter specifyinghow many neighbors each candidate rectangle should haveto retain it.

    @param flags Parameter with the samemeaning for an old cascade as in the functioncvHaarDetectObjects. It is not used for a new cascade.

    @param minSize Minimum possible objectsize. Objects smaller than that are ignored.

@param maxSize Maximum possible object size.Objects larger than that are ignored.


源码下载:http://download.csdn.net/detail/cracent/9492682

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值