用VS2010 + OpenCV 2.4.9 实现简单人脸识别,供大家参考,具体内容如下
首先放效果图(为了防止辣眼睛,后期处理了下):

首先声明,我是在参考其他文章的基础上实现的。
切入正题:
1 设置控件
首先新建一个基于Dialog的MFC程序的工程,工程名为FaceDetect ;
然后在IDD_FACEDETECT_DIALOG对话框中添加一个Picture 控件,ID命名为:IDC_PICTURE;添加一个Button控件,Caption命名为 “检测”,ID命名为IDC_START,将原来自动生成的的OK按钮的Caption改为“退出”;
删除原来的Text控件和“Cancel”控件。
2 定义变量
在FaceDetectDlg.h开头添加以下几行代码
#pragma once #include "opencv2/objdetect/objdetect.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp” using namespace std; using namespace cv;
然后在CFaceDetectDlg类定义一下几个变量
public: String face_cascade_name; String eyes_cascade_name; CascadeClassifier face_cascade; CascadeClassifier eyes_cascade; VideoCapture capture;
3 对定义的变量初始化
CFaceDetectDlg::CFaceDetectDlg(CWnd* pParent /*=NULL*/)
 : CDialogEx(CFaceDetectDlg::IDD, pParent)
{
 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
 string face_cascade_name = "";
 string eyes_cascade_name = "";
}
BOOL CFaceDetectDlg::OnInitDialog()
{
 CDialogEx::OnInitDialog();
 // Add "About..." menu item to system menu.
 // IDM_ABOUTBOX must be in the system command range.
 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
 ASSERT(IDM_ABOUTBOX < 0xF000);
 CMenu* pSysMenu = GetSystemMenu(FALSE);
 if (pSysMenu != NULL)
 {
 BOOL bNameValid;
 CString strAboutMenu;
 bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
 ASSERT(bNameValid);
 if (!strAboutMenu.IsEmpty())
 {
 pSysMenu->AppendMenu(MF_SEPARATOR);
 pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
 }
 }
 // Set the icon for this dialog. The framework does this automatically
 // when the application's main window is not a dialog
 SetIcon(m_hIcon, TRUE); // Set big icon
 SetIcon(m_hIcon, FALSE); // Set small icon
 // TODO: Add extra initialization here
 string face_cascade_name = "..\\debug\\haarcascade_frontalface_alt.xml";
 string eyes_cascade_name = "..\\debug\\haarcascade_eye_tree_eyeglasses.xml";
 if( !face_cascade.load( face_cascade_name ) )
 {
 MessageBox(_T("haarcascade_frontalface_alt.xml Error loading")); 
 return -1;
 };
 if( !eyes_cascade.load( eyes_cascade_name ) )
 {
 MessageBox(_T(" haarcascade_eye_tree_eyeglasses.xmlError loading"));
 return -1;
 };
 return TRUE; // return TRUE unless you set the focus to a control
}
4 检测函数的编写
思路是这样的:
1.首先打开摄像头
2.然后将摄像托获取的图像传递给人脸识别的函数
3.将识别后处理过的图像在Picture控件中显示出来
双击IDD_FACEDETECT_DIALOG对话框上的上的“检测”按钮控件,进入控件函数编写的地方,该函数如下所示:
void CFaceDetectDlg::OnBnClickedStart()
{
 // TODO: Add your control notification handler code here
 capture.open(0);//捕获外部摄像头,如果只有一个摄像头,就填0
 Mat frame;
 namedWindow("view", WINDOW_AUTOSIZE);
 HWND hWnd = (HWND)cvGetWindowHandle("view");
 HWND hParent = ::GetParent(hWnd);
 ::SetParent(hWnd, GetDlgItem(IDC_PICTURE)->m_hWnd);
 ::ShowWindow(hParent, SW_HIDE);//隐藏运行程序框,并且把它“画”到MFC上
 if (capture.isOpened())
 {
 for (;;)//循环以达到视频的效果
 {
 capture >> frame;
 if (!frame.empty())
 {
 detectAndDisplay(frame);//识别的函数
 imshow("view", frame);
 UpdateData(FALSE);
 }
 else
 {
 //::AfxMessageBox(" --(!) No captured frame -- Break!");
 continue;
 //break;
 }
 waitKey(10);
 }
 }
}
以上代码中 detectAndDisplay(frame)语句表示调用了 detectAndDisplay(Mat frame)函数,因此我们得声明和定义该函数。
在CFaceDetectDlg类的头文件FaceDetectDlg.h中声明该函数:
void detectAndDisplay(Mat frame);//声明函数
在FaceDetectDlg.cpp中定义该函数:
void CFaceDetectDlg::detectAndDisplay( Mat frame )
{
 std::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*0.5, faces[i].y + faces[i].height*0.5 );
 ellipse( frame, center, Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );
 Mat faceROI = frame_gray( faces[i] );
 std::vector<Rect> eyes;
 //-- 在每张人脸上检测双眼
 eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30) );
 for( int j = 0; j < eyes.size(); j++ )
 {
 Point center( faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5 );
 int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
 circle( frame, center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 );
 }
 }
}
编译运行
编译工程,然后将 
haarcascade_frontalface_alt.xml 和 haarcascade_eye_tree_eyeglasses.xml拷贝到工程目录文件下Debug文件夹里,也就是可执行文件所在的那个文件夹。
以上基本上可以实现预期的人脸识别功能,可是我们可以发现此时点击“退出”按钮时,摄像头的灯还亮着,那是因为摄像头在程序退出后没有关闭掉,因此还得添加代码关闭摄像头。
双击“退出”按钮,编辑代码如下
void CFaceDetectDlg::OnBnClickedOk()
{
 // TODO: Add your control notification handler code here
 capture.release(); //关闭摄像头
 CDialogEx::OnOK();
}
后记
以后我将把这个工程的代码公布在我的Github上,希望能对其他人有所帮助。 
代码已上传至 :MFC-OpenCV-
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。