資源簡介
簡單的一份代碼程序,調(diào)用筆記本的攝像頭進(jìn)行人臉捕捉,同時(shí)進(jìn)行灰度處理。利用OpenCV自帶的函數(shù)庫實(shí)現(xiàn),可運(yùn)行。

代碼片段和文件信息
//?ComputerVisualHW.cpp?:?定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//
#include?“stdafx.h“?
#include?“opencv2/objdetect/objdetect.hpp“
#include?“opencv2/highgui/highgui.hpp“
#include?“opencv2/imgproc/imgproc.hpp“
#include?
#include?
//using?namespace?std;
using?namespace?cv;
/*******************************************************
??????????????detectAndGray函數(shù)申明
函數(shù)功能:將攝像頭所攝取到的圖像進(jìn)行人臉捕捉以及灰度轉(zhuǎn)換
********************************************************/
void?detectAndGray(Mat?frame);
/*******************************************************
???????????????????函數(shù)變量定義
********************************************************/
//人臉識(shí)別功能所需的擴(kuò)展標(biāo)記語言
string?face_cascade_name?=?“haarcascade_frontalface_alt.xml“;
//聲明對(duì)象
CascadeClassifier?face_cascade;
//顯示窗口1:原圖像及人臉捕捉
string?window_name_RGB?=?“Capture?-?Face?detection?-?RGB“;
//顯示窗口2:灰度化圖像及人臉捕捉
string?window_name_GRAY?=?“Capture?-?Face?detection?-?GRAY“;
/*******************************************************
???????????????????????主函數(shù)
********************************************************/
int?main?(int?argc?const?char**?argv)
{
CvCapture*?capture;//用來存儲(chǔ)攝像頭的狀態(tài)
Mat?frame;//用來存儲(chǔ)攝像頭采集回來的一幀圖像
/************加載級(jí)聯(lián)分類器文件***********/
if(!face_cascade.load(face_cascade_name))
{
//級(jí)聯(lián)分類器文件加載失敗,程序停止運(yùn)行
printf(“--(!)Error?loading?face_cascade\n“);
return?-1;
}
//打開內(nèi)置攝像頭視頻流
capture?=?cvCaptureFromCAM(0);
//攝像頭處于打開狀態(tài)
if?(capture)
{
while(true)
{
//從攝像頭中獲取一幀圖像并存儲(chǔ)在frame中
frame?=?cvQueryframe(capture);
//對(duì)當(dāng)前一幀圖像進(jìn)行處理
if(!frame.empty())//有圖像
{
//人臉檢測(cè)以及灰度轉(zhuǎn)化
detectAndGray(frame);
}
//按ESC鍵退出圖像窗口
if(cvWaitKey(50)?==?27){break;}
}
}
return?0;
}
/*****************函數(shù)?detectAndDisplay?****************/
void?detectAndGray(Mat?frame)
{
std::vector?faces;
Mat?frame_gray;
//多尺寸檢測(cè)人臉
face_cascade.detectMultiScale(framefaces1.120|CV_HAAR_SCALE_IMAGESize(3030));
for(int?i?=?0;?i? {
Point?center(faces[i].x?+?faces[i].width*0.5faces[i].y+faces[i].height*0.5);
ellipse(framecenterSize(faces[i].width*0.5faces[i].height*0.5)00360Scalar(2550255)480);
}
imshow(window_name_RGBframe);//窗口1顯示原圖像
//將原圖像轉(zhuǎn)換成灰度圖像
cvtColor(frameframe_grayCV_BGR2GRAY);
//在灰度圖像上進(jìn)行人臉檢測(cè)
face_cascade.detectMultiScale(frame_grayfaces1.120|CV_HAAR_SCALE_IMAGESize(3030));
for(int?i?=?0;?i? {
Point?center(faces[i].x?+?faces[i].width*0.5faces[i].y+faces[i].height*0.5);
ellipse(frame_graycenterSize(faces[i].width*0.5faces[i].height*0.5)00360Scalar(2550255)480);
}
imshow(window_name_GRAYframe_gray);//窗口2顯示灰度圖像
}
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件???????2959??2014-11-20?18:17??ComputerVisualHW\ComputerVisualHW\ComputerVisualHW.cpp
?????文件???????5007??2014-11-19?20:46??ComputerVisualHW\ComputerVisualHW\ComputerVisualHW.vcxproj
?????文件???????1320??2014-11-19?20:40??ComputerVisualHW\ComputerVisualHW\ComputerVisualHW.vcxproj.filters
?????文件????????143??2014-11-19?20:40??ComputerVisualHW\ComputerVisualHW\ComputerVisualHW.vcxproj.user
?????文件???????1882??2014-11-20?18:04??ComputerVisualHW\ComputerVisualHW\Debug\cl.command.1.tlog
?????文件??????20506??2014-11-20?18:04??ComputerVisualHW\ComputerVisualHW\Debug\CL.read.1.tlog
?????文件???????1466??2014-11-20?18:04??ComputerVisualHW\ComputerVisualHW\Debug\CL.write.1.tlog
?????文件????????406??2014-11-19?20:40??ComputerVisualHW\ComputerVisualHW\Debug\ComputerVisualHW.exe.em
?????文件????????472??2014-11-19?20:41??ComputerVisualHW\ComputerVisualHW\Debug\ComputerVisualHW.exe.em
?????文件????????381??2014-11-20?18:04??ComputerVisualHW\ComputerVisualHW\Debug\ComputerVisualHW.exe.intermediate.manifest
?????文件?????????94??2014-11-20?18:04??ComputerVisualHW\ComputerVisualHW\Debug\ComputerVisualHW.lastbuildstate
?????文件???????4849??2014-11-20?18:04??ComputerVisualHW\ComputerVisualHW\Debug\ComputerVisualHW.log
?????文件?????176229??2014-11-20?18:04??ComputerVisualHW\ComputerVisualHW\Debug\ComputerVisualHW.obj
?????文件????1179648??2014-11-19?20:40??ComputerVisualHW\ComputerVisualHW\Debug\ComputerVisualHW.pch
?????文件????????707??2014-11-19?22:08??ComputerVisualHW\ComputerVisualHW\Debug\ComputerVisualHW.vcxprojResolveAssemblyReference.cache
?????文件??????????0??2014-11-19?20:40??ComputerVisualHW\ComputerVisualHW\Debug\ComputerVisualHW.write.1.tlog
?????文件????????222??2014-11-19?20:40??ComputerVisualHW\ComputerVisualHW\Debug\ComputerVisualHW_manifest.rc
?????文件??????????2??2014-11-20?18:04??ComputerVisualHW\ComputerVisualHW\Debug\li
?????文件??????????2??2014-11-20?18:04??ComputerVisualHW\ComputerVisualHW\Debug\li
?????文件??????????2??2014-11-20?18:04??ComputerVisualHW\ComputerVisualHW\Debug\li
?????文件??????????2??2014-11-20?18:04??ComputerVisualHW\ComputerVisualHW\Debug\li
?????文件??????????2??2014-11-20?18:04??ComputerVisualHW\ComputerVisualHW\Debug\li
?????文件??????????2??2014-11-20?18:04??ComputerVisualHW\ComputerVisualHW\Debug\li
?????文件??????????2??2014-11-20?18:04??ComputerVisualHW\ComputerVisualHW\Debug\li
?????文件??????????2??2014-11-20?18:04??ComputerVisualHW\ComputerVisualHW\Debug\li
?????文件??????????2??2014-11-20?18:04??ComputerVisualHW\ComputerVisualHW\Debug\li
?????文件??????????2??2014-11-20?18:04??ComputerVisualHW\ComputerVisualHW\Debug\li
?????文件??????????2??2014-11-20?18:04??ComputerVisualHW\ComputerVisualHW\Debug\li
?????文件??????????2??2014-11-20?18:04??ComputerVisualHW\ComputerVisualHW\Debug\li
?????文件??????????2??2014-11-20?18:04??ComputerVisualHW\ComputerVisualHW\Debug\li
............此處省略65個(gè)文件信息
評(píng)論
共有 條評(píng)論