用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-
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答
圖片精選