使用OpenCV和MFC怎么實(shí)現(xiàn)一個(gè)人臉識(shí)別功能

使用OpenCV 和MFC怎么實(shí)現(xiàn)一個(gè)人臉識(shí)別功能?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。

創(chuàng)新互聯(lián)建站是一家專(zhuān)業(yè)提供復(fù)興企業(yè)網(wǎng)站建設(shè),專(zhuān)注與成都網(wǎng)站建設(shè)、做網(wǎng)站、HTML5建站、小程序制作等業(yè)務(wù)。10年已為復(fù)興眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專(zhuān)業(yè)網(wǎng)站設(shè)計(jì)公司優(yōu)惠進(jìn)行中。

1 設(shè)置控件

首先新建一個(gè)基于Dialog的MFC程序的工程,工程名為FaceDetect ;
然后在IDD_FACEDETECT_DIALOG對(duì)話框中添加一個(gè)Picture 控件,ID命名為:IDC_PICTURE;添加一個(gè)Button控件,Caption命名為 “檢測(cè)”,ID命名為IDC_START,將原來(lái)自動(dòng)生成的的OK按鈕的Caption改為“退出”;
刪除原來(lái)的Text控件和“Cancel”控件。

2 定義變量

在FaceDetectDlg.h開(kāi)頭添加以下幾行代碼

#pragma once
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp" 
#include "opencv2/imgproc/imgproc.hpp”
using namespace std;
using namespace cv;

然后在CFaceDetectDlg類(lèi)定義一下幾個(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ù)的編寫(xiě)

思路是這樣的:

1.首先打開(kāi)攝像頭
2.然后將攝像托獲取的圖像傳遞給人臉識(shí)別的函數(shù)
3.將識(shí)別后處理過(guò)的圖像在Picture控件中顯示出來(lái)

雙擊IDD_FACEDETECT_DIALOG對(duì)話框上的上的“檢測(cè)”按鈕控件,進(jìn)入控件函數(shù)編寫(xiě)的地方,該函數(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)行程序框,并且把它“畫(huà)”到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類(lèi)的頭文件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像頭在程序退出后沒(méi)有關(guān)閉掉,因此還得添加代碼關(guān)閉攝像頭。

雙擊“退出”按鈕,編輯代碼如下

void CFaceDetectDlg::OnBnClickedOk()
{
 // TODO: Add your control notification handler code here
 capture.release(); //關(guān)閉攝像頭
 CDialogEx::OnOK();
}

關(guān)于使用OpenCV 和MFC怎么實(shí)現(xiàn)一個(gè)人臉識(shí)別功能問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

分享名稱(chēng):使用OpenCV和MFC怎么實(shí)現(xiàn)一個(gè)人臉識(shí)別功能
標(biāo)題來(lái)源:http://muchs.cn/article38/gdsdpp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、域名注冊(cè)、定制網(wǎng)站、自適應(yīng)網(wǎng)站、手機(jī)網(wǎng)站建設(shè)搜索引擎優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)