資源簡介
c++版本的opencv開發案例14個中的第11個案例:人臉檢測與識別系統的實現
代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
#include?“facedetect-dll.h“
#pragma?comment(lib“libfacedetect.lib“)
#pragma?comment(?linker?“/subsystem:\“windows\“?/entry:\“mainCRTStartup\““?)//不顯示控制臺
using?namespace?cv;
using?namespace?std;
//函數聲明
int?detectAndDisplay(Mat?frame);
extern?bool?stop?=?false;//設置全局變量
extern?string?mode?=?“manual“;
extern?unsigned?_int64?time?=?1000;
static?void?help()
{
????cout?<<
“\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n“
????“\n@This?program?shows?image?from?camera.\n“
“\n@And?Detect?Face!!!\n“
“\n@Version:1.15.11.0\n“
????“\n@Call:./detectface.exe?/auto?1000\n“
“\n@Call:./detectface.exe?/manual?5000\n“
“\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n“<}
int?main(int?argc?char**?argv?)
{
help();
//打開攝像頭設備
VideoCapture?cap(0);
if(!cap.isOpened())
{
cout<<“---Device?Can?Not?Open---Break“;
waitKey(1000);
return?1;
}
Mat?frame;
if(argc?>?1?)
{
mode?=?argv[1];
}
if(argc?>?2?)
{
mode?=?argv[1];
time?=?_strtoui64(argv[2]NULL10);
}
while(!stop)
{
//查看圖像是否被抓取到,抓取到就調用檢測face并顯示,未檢測到報錯,任意鍵退出。
cap>>frame;
if(!frame.empty())
{
detectAndDisplay(frame);
}
else
{
cout<<“---No?Capture?frame---Break“;
waitKey(1000);
return?2;
}
if(waitKey(20)?>?0)?//設置幀率50Hz
{
cout<<“This?is?FR/HZ“;
}
}
return?0;
}
int?detectAndDisplay(Mat?frame)
{
Mat?edges;
Mat?gray;
//GaussianBlur(edgesedgesSize(77)1.51.5);//高斯濾波
//Canny(edgesedges0303);//邊緣檢測
flip(frameedges1);
//每一幀轉化為灰度
cvtColor(edgesgrayCV_BGR2GRAY);??//將frame圖像由BGR轉化為GRAY的目標圖像edges
if(gray.empty())
{
fprintf(stderr?“Can?not?load?the?image?file.\n“);
????cin.get();
return?2;
}
int?*pResults?=?NULL;?
///////////////////////////////////////////
//?多角度人臉檢測?facedetect_multiview()
//?可以檢測部分臉部,但比正面臉部檢測要慢,正面函數為facedetect_frontal()
//////////////////////////////////////////
//!!!?輸入信號必須為轉灰度的圖像(單通道信號)
//!!!?不要釋放變量pResults?!!!
pResults?=?facedetect_frontal((unsigned?char*)(gray.ptr(0))?gray.cols?gray.rows?gray.step
???1.2f?4?24);
printf(“%d?faces?detected.\n“?(pResults???*pResults?:?0));
//print?the?detection?results
for(int?i?=?0;?i?(pResults???*pResults?:?0);?i++)
{?
????????short?*?p?=?((short*)(pResults+1))+6*i;
int?x?=?p[0];
int?y?=?p[1];
int?w?=?p[2];
int?h?=?p[3];
int?neighbors?=?p[4];
int?angle?=?p[5];
//printf(“face_rect=[%d?%d?%d?%d]?neighbors=%d?angle=%d\n“?xywhneighbors?angle);
rectangle(edges?cvPoint(xy)?cvPoint(x+w?y+h)?CV_RGB(25500)?2);//畫出矩形框
if(neighbors?>?5)
{
imshow(“Camera-說明:抓取到人臉后會定格圖像數秒,請確認后任意鍵退出!“edges);
if(mode?==?“/auto“)
{
stop?=?true;
}
if(waitKey(time)?>?0)
{
cout<<“Detect?Face?Pass!!!“< stop?=?true;
}
}
}
?????imshow(“Camera-說明:抓取到人臉后會定格圖像數秒,請確認后任意鍵退出!“edges);
?return?-1;
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????843824??2006-10-18?21:49??faceidentifysys\cv100.dll
-----------?---------??----------?-----??----
???????????????843824????????????????????1
評論
共有 條評論