OpenCV + MFC實(shí)現(xiàn)簡單人臉識(shí)別
用VS2010 + OpenCV 2.4.9 實(shí)現(xiàn)簡單人臉識(shí)別
首先放效果圖(為了防止辣眼睛,后期處理了下):
首先聲明,我是在參考其他文章的基礎(chǔ)上實(shí)現(xiàn)的。
切入正題:
1 設(shè)置控件
首先新建一個(gè)基于Dialog的MFC程序的工程,工程名為FaceDetect ;
然后在IDD_FACEDETECT_DIALOG對(duì)話框中添加一個(gè)Picture 控件,ID命名為:IDC_PICTURE;添加一個(gè)Button控件,Caption命名為 “檢測”,ID命名為IDC_START,將原來自動(dòng)生成的的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類定義一下幾個(gè)變量
public: String face_cascade_name; String eyes_cascade_name; CascadeClassifier face_cascade; CascadeClassifier eyes_cascade; VideoCapture capture;
3 對(duì)定義的變量初始化
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 檢測函數(shù)的編寫
思路是這樣的:
1.首先打開攝像頭
2.然后將攝像托獲取的圖像傳遞給人臉識(shí)別的函數(shù)
3.將識(shí)別后處理過的圖像在Picture控件中顯示出來
雙擊IDD_FACEDETECT_DIALOG對(duì)話框上的上的“檢測”按鈕控件,進(jìn)入控件函數(shù)編寫的地方,該函數(shù)如下所示:
void CFaceDetectDlg::OnBnClickedStart() { // TODO: Add your control notification handler code here capture.open(0);//捕獲外部攝像頭,如果只有一個(gè)攝像頭,就填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);//隱藏運(yùn)行程序框,并且把它“畫”到MFC上 if (capture.isOpened()) { for (;;)//循環(huán)以達(dá)到視頻的效果 { capture >> frame; if (!frame.empty()) { detectAndDisplay(frame);//識(shí)別的函數(shù) imshow("view", frame); UpdateData(FALSE); } else { //::AfxMessageBox(" --(!) No captured frame -- Break!"); continue; //break; } waitKey(10); } } }
以上代碼中 detectAndDisplay(frame)語句表示調(diào)用了 detectAndDisplay(Mat frame)函數(shù),因此我們得聲明和定義該函數(shù)。
在CFaceDetectDlg類的頭文件FaceDetectDlg.h中聲明該函數(shù):
void detectAndDisplay(Mat frame);//聲明函數(shù)
在FaceDetectDlg.cpp中定義該函數(shù):
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 ); } } }
編譯運(yùn)行
編譯工程,然后將
haarcascade_frontalface_alt.xml 和 haarcascade_eye_tree_eyeglasses.xml拷貝到工程目錄文件下Debug文件夾里,也就是可執(zhí)行文件所在的那個(gè)文件夾。
以上基本上可以實(shí)現(xiàn)預(yù)期的人臉識(shí)別功能,可是我們可以發(fā)現(xiàn)此時(shí)點(diǎn)擊“退出”按鈕時(shí),攝像頭的燈還亮著,那是因?yàn)閿z像頭在程序退出后沒有關(guān)閉掉,因此還得添加代碼關(guān)閉攝像頭。
雙擊“退出”按鈕,編輯代碼如下
void CFaceDetectDlg::OnBnClickedOk() { // TODO: Add your control notification handler code here capture.release(); //關(guān)閉攝像頭 CDialogEx::OnOK(); }
后記
以后我將把這個(gè)工程的代碼公布在我的Github上,希望能對(duì)其他人有所幫助。
代碼已上傳至 :MFC-OpenCV-
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:C++中進(jìn)行txt文件讀入和寫入的方法示例
欄 目:C語言
本文標(biāo)題:OpenCV + MFC實(shí)現(xiàn)簡單人臉識(shí)別
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/211.html
您可能感興趣的文章
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)-用棧實(shí)現(xiàn)表達(dá)式求值的方法詳解
- 01-10使用OpenGL實(shí)現(xiàn)3D立體顯示的程序代碼
- 01-10求斐波那契(Fibonacci)數(shù)列通項(xiàng)的七種實(shí)現(xiàn)方法
- 01-10C語言 解決不用+、-、&#215;、&#247;數(shù)字運(yùn)算符做加法
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10用C++實(shí)現(xiàn)DBSCAN聚類算法
- 01-10深入全排列算法及其實(shí)現(xiàn)方法
- 01-10全排列算法的非遞歸實(shí)現(xiàn)與遞歸實(shí)現(xiàn)的方法(C++)
- 01-10用C語言實(shí)現(xiàn)單鏈表的各種操作(一)
- 01-10用C語言實(shí)現(xiàn)單鏈表的各種操作(二)


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 04-02jquery與jsp,用jquery
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?