Face Detection

In this chapter we are going to learn how to detect faces in an image. Now in might sound complicated but it is actually easier than detecting colors or contours. So let’s get started first thing we will do is we will create a new c++ files. And then we will copy the code to import a image. So we will use the meimei.jpg, and we will detect the face here so we can close this now to detect the face we are going to use viola jones method of hard cascades now this method was introduced quite a long time age but it is still quite good and it is still used quite a bit so what we need is we need a new header files. So we will write here include and we will write opencv2 and then we will write objdetect.hpp. so this is the header file that will allow us to work with our cascade files. So basically what we have is a model already trains and we have the xml file for it. So here we have the frontal face default to load our cascade and then we will use it to find the faces. So here after we have imported our image we are going to load our cascade. So we will write here cascade classifier and we will call it let’s say face cascade and then we will say that facecascade.load and we will give in our path. So it will be resources slash. actually I’m not going to type here. So we can create a vector of rectangles. So let’s create that vector of rectangles and we will call it faces. And then we can use our facecascade.detectMultiscale. so we will write here image we will give in our faces we want to store them in this vector, and then we have some parameters that we can define. So the first is the scale factor so we will write 1.1. and then we have the minimum neighbors. So for example we can put 10 now these. We are not printing out anything or we are not displaying anything. So what we can do is we can iterate through all the faces that we have detected and then we can draw them one by one. Last run it, so now the face has been detected and we are able to draw our rectangle around it.

代码如下:

#include<opencv2/imgcodecs.hpp>
#include<opencv2/highgui.hpp>
#include<opencv2/imgproc.hpp>
#include<opencv2/objdetect.hpp>
#include<iostream>
using namespace cv;//声明使用opencv命名空间
using namespace std;//声明使用标准库命名空间

// importing images
int main()
{
    //设置一个路径接受图像
    string path = "D:/opencv_study/opencv_tutorial_data-master/images/persons.png";//等号右侧为图像路径
    Mat img = imread(path);// 创建矩阵接收图像
    CascadeClassifier faceCascade;//加载级联分类器,命名为脸部级联
    faceCascade.load("D:/opencv_study/Resources/haarcascade_frontalface_default.xml");//指出加载路径
    if (faceCascade.empty()) //如果facecascade为空,则打印输出XML文件未被加载
    {
        cout << "XML file not loaded" << endl;
    }
    vector<Rect>faces;//创建矩形,并命名为faces
    faceCascade.detectMultiScale(img, faces, 1.1, 10);//使用人脸级联多尺度检测方法
    for (int i = 0; i < faces.size(); i++)//遍历所有面孔
    {
        rectangle(img, faces[i].tl(), faces[i].br(), Scalar(255, 0, 255), 3);//给定矩形框的位置和颜色
    }
    imshow("Image", img);//imshow显示图像
    //waitkey(0):参数0表示阻塞,程序运行到这即停止
    //Waitkey(1):参数1停顿1毫秒,然后继续向下执行
    waitKey(0);
}

脸部检测结果,显然并不是很完美。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值