OpenCV + MFC實(shí)現(xiàn)簡(jiǎn)單人臉識(shí)別
用VS2010 + OpenCV 2.4.9 實(shí)現(xiàn)簡(jiǎn)單人臉識(shí)別,供大家參考,具體內(nèi)容如下
首先放效果圖(為了防止辣眼睛,后期處理了下):

首先聲明,我是在參考其他文章的基礎(chǔ)上實(shí)現(xiàn)的。
切入正題:
1 設(shè)置控件
首先新建一個(gè)基于Dialog的MFC程序的工程,工程名為FaceDetect ;
然后在IDD_FACEDETECT_DIALOG對(duì)話框中添加一個(gè)Picture 控件,ID命名為:IDC_PICTURE;添加一個(gè)Button控件,Caption命名為 “檢測(cè)”,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 檢測(cè)函數(shù)的編寫
思路是這樣的:
1.首先打開攝像頭
2.然后將攝像托獲取的圖像傳遞給人臉識(shí)別的函數(shù)
3.將識(shí)別后處理過的圖像在Picture控件中顯示出來
雙擊IDD_FACEDETECT_DIALOG對(duì)話框上的上的“檢測(cè)”按鈕控件,進(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)語(yǔ)句表示調(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 );
//-- 多尺寸檢測(cè)人臉
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;
//-- 在每張人臉上檢測(cè)雙眼
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-
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 手把手教你利用opencv實(shí)現(xiàn)人臉識(shí)別功能(附源碼+文檔)
- springboot集成opencv實(shí)現(xiàn)人臉識(shí)別功能的詳細(xì)步驟
- java+opencv實(shí)現(xiàn)人臉識(shí)別功能
- python opencv人臉識(shí)別考勤系統(tǒng)的完整源碼
- python基于opencv實(shí)現(xiàn)人臉識(shí)別
- python實(shí)現(xiàn)圖片,視頻人臉識(shí)別(opencv版)
- PyQt5+Caffe+Opencv搭建人臉識(shí)別登錄界面
- opencv實(shí)現(xiàn)簡(jiǎn)單人臉識(shí)別
- Java OpenCV4.0.0實(shí)現(xiàn)實(shí)時(shí)人臉識(shí)別
- 基于opencv和pillow實(shí)現(xiàn)人臉識(shí)別系統(tǒng)(附demo)
相關(guān)文章
C語(yǔ)言中char*和char[]用法區(qū)別分析
這篇文章主要介紹了C語(yǔ)言中char*和char[]用法區(qū)別,包括使用過程中的誤區(qū)及注意點(diǎn)分析,需要的朋友可以參考下2014-09-09
C++實(shí)現(xiàn)基于時(shí)序公平的讀寫鎖詳解
讀寫鎖與普通的互斥鎖的區(qū)別在于有兩種上鎖方式:讀鎖和寫鎖,不用的用戶對(duì)同一個(gè)讀寫鎖獲取讀鎖是非互斥的,其他情況則是互斥的,本文小編將給大家詳細(xì)介紹C++實(shí)現(xiàn)基于時(shí)序公平的讀寫鎖,需要的朋友可以參考下2023-10-10
C語(yǔ)言實(shí)現(xiàn)多線程定時(shí)器實(shí)例講解
在本篇文章里小編給各位分享的是一篇關(guān)于C語(yǔ)言實(shí)現(xiàn)多線程定時(shí)器實(shí)例講解內(nèi)容,有需要的朋友們可以參考學(xué)習(xí)下。2021-01-01
wxWidgets實(shí)現(xiàn)無(wú)標(biāo)題欄窗口拖動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了wxWidgets實(shí)現(xiàn)無(wú)標(biāo)題欄窗口拖動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
模擬鼠標(biāo)事件的實(shí)現(xiàn)思路及代碼
這篇文章主要介紹了模擬鼠標(biāo)事件的實(shí)現(xiàn)思路及代碼,有需要的朋友可以參考一下2013-12-12

