Opencv EigenFace人臉識(shí)別算法詳解
簡(jiǎn)要:
EigenFace是基于PCA降維的人臉識(shí)別算法,PCA是使整體數(shù)據(jù)降維后的方差最大,沒(méi)有考慮降維后類(lèi)間的變化。 它是將圖像每一個(gè)像素當(dāng)作一維特征,然后用SVM或其它機(jī)器學(xué)習(xí)算法進(jìn)行訓(xùn)練。但這樣維數(shù)太多,根本無(wú)法計(jì)算。我這里用的是ORL人臉數(shù)據(jù)庫(kù),英國(guó)劍橋?qū)嶒?yàn)室拍攝的,有40位志愿者的人臉,在不同表情不同光照下每位志愿者拍攝10張,共有400張圖片,大小為112*92,所以如果把每個(gè)像素當(dāng)做特征拿來(lái)訓(xùn)練的話,一張人臉就有10304維特征,這么高維的數(shù)據(jù)根本無(wú)法處理。所以需要先對(duì)數(shù)據(jù)進(jìn)行降維,去掉一些冗余的特征。
第一步:將ORL人臉圖片的地址統(tǒng)一放在一個(gè)文件里,等會(huì)通過(guò)對(duì)該文件操作,將圖片全部加載進(jìn)來(lái)。
//ofstream一般對(duì)文件進(jìn)行讀寫(xiě)操作,ifstream一般對(duì)文件進(jìn)行讀操作 ofstream file; file.open("path.txt");//新建并打開(kāi)文件 char str[50] = {}; for (int i = 1; i <= 40; i++) { for (int j = 1; j <= 10; j++) { sprintf_s(str, "orl_faces/s%d/%d.pgm;%d", i, j, i);//將數(shù)字轉(zhuǎn)換成字符 file << str << endl;//寫(xiě)入 } }
得到路勁文件如下圖所示:
第二步:讀入模型需要輸入的數(shù)據(jù),即用來(lái)訓(xùn)練的圖像vector<Mat>images和標(biāo)簽vector<int>labels
string filename = string("path.txt"); ifstream file(filename); if (!file) { printf("could not load file"); } vector<Mat>images; vector<int>labels; char separator = ';'; string line,path, classlabel; while (getline(file,line)) { stringstream lines(line); getline(lines, path, separator); getline(lines, classlabel); images.push_back(imread(path, 0)); labels.push_back(atoi(classlabel.c_str()));//atoi(ASCLL to int)將字符串轉(zhuǎn)換為整數(shù)型 }
第三步:加載、訓(xùn)練、預(yù)測(cè)模型
Ptr<BasicFaceRecognizer> model = EigenFaceRecognizer::create(); model->train(images, labels); int predictedLabel = model->predict(testSample); printf("actual label:%d,predict label :%d\n", testLabel, predictedLabel);
補(bǔ)充:
1、顯示平均臉
//計(jì)算特征值特征向量及平均值 Mat vals = model->getEigenValues();//89*1 printf("%d,%d\n", vals.rows, vals.cols); Mat vecs = model->getEigenVectors();//10324*89 printf("%d,%d\n", vecs.rows, vecs.cols); Mat mean = model->getMean();//1*10304 printf("%d,%d\n", mean.rows, mean.cols); //顯示平均臉 Mat meanFace = mean.reshape(1, height);//第一個(gè)參數(shù)為通道數(shù),第二個(gè)參數(shù)為多少行 normalize(meanFace, meanFace, 0, 255, NORM_MINMAX, CV_8UC1); imshow("Mean Face", meanFace);
2、顯示前部分特征臉
//顯示特征臉 for (int i = 0; i<min(10, vals.rows); i++) { Mat feature_vec = vecs.col(i).clone(); Mat feature_face= feature_vec.reshape(1, height); normalize(feature_face, feature_face, 0, 255, NORM_MINMAX, CV_8UC1); Mat colorface; applyColorMap(feature_face, colorface, COLORMAP_BONE); sprintf_s(win_title, "eigenface%d", i); imshow(win_title, colorface); }
3、對(duì)第一張人臉在特征向量空間進(jìn)行人臉重建(分別基于前10,20,30,40,50,60個(gè)特征向量進(jìn)行人臉重建)
//重建人臉 for (int i = min(10, vals.rows); i <min(61, vals.rows); i+=10) { Mat vecs_space = Mat(vecs, Range::all(), Range(0, i)); Mat projection = LDA::subspaceProject(vecs_space, mean, images[0].reshape(1, 1));//投影到子空間 Mat reconstruction = LDA::subspaceReconstruct(vecs_space, mean, projection);//重建 Mat result = reconstruction.reshape(1, height); normalize(result, result, 0, 255, NORM_MINMAX, CV_8UC1); //char wintitle[40] = {}; sprintf_s(win_title, "recon face %d", i); imshow(win_title, result); }
完整代碼如下:
#include<opencv2\opencv.hpp> #include<opencv2\face.hpp> using namespace cv; using namespace face; using namespace std; char win_title[40] = {}; int main(int arc, char** argv) { namedWindow("input",CV_WINDOW_AUTOSIZE); //讀入模型需要輸入的數(shù)據(jù),用來(lái)訓(xùn)練的圖像vector<Mat>images和標(biāo)簽vector<int>labels string filename = string("path.txt"); ifstream file(filename); if (!file) { printf("could not load file"); } vector<Mat>images; vector<int>labels; char separator = ';'; string line,path, classlabel; while (getline(file,line)) { stringstream lines(line); getline(lines, path, separator); getline(lines, classlabel); //printf("%d\n", atoi(classlabel.c_str())); images.push_back(imread(path, 0)); labels.push_back(atoi(classlabel.c_str()));//atoi(ASCLL to int)將字符串轉(zhuǎn)換為整數(shù)型 } int height = images[0].rows; int width = images[0].cols; printf("height:%d,width:%d\n", height, width); //將最后一個(gè)樣本作為測(cè)試樣本 Mat testSample = images[images.size() - 1]; int testLabel = labels[labels.size() - 1]; //刪除列表末尾的元素 images.pop_back(); labels.pop_back(); //加載,訓(xùn)練,預(yù)測(cè) Ptr<BasicFaceRecognizer> model = EigenFaceRecognizer::create(); model->train(images, labels); int predictedLabel = model->predict(testSample); printf("actual label:%d,predict label :%d\n", testLabel, predictedLabel); //計(jì)算特征值特征向量及平均值 Mat vals = model->getEigenValues();//89*1 printf("%d,%d\n", vals.rows, vals.cols); Mat vecs = model->getEigenVectors();//10324*89 printf("%d,%d\n", vecs.rows, vecs.cols); Mat mean = model->getMean();//1*10304 printf("%d,%d\n", mean.rows, mean.cols); //顯示平均臉 Mat meanFace = mean.reshape(1, height);//第一個(gè)參數(shù)為通道數(shù),第二個(gè)參數(shù)為多少行 normalize(meanFace, meanFace, 0, 255, NORM_MINMAX, CV_8UC1); imshow("Mean Face", meanFace); //顯示特征臉 for (int i = 0; i<min(10, vals.rows); i++) { Mat feature_vec = vecs.col(i).clone(); Mat feature_face= feature_vec.reshape(1, height); normalize(feature_face, feature_face, 0, 255, NORM_MINMAX, CV_8UC1); Mat colorface; applyColorMap(feature_face, colorface, COLORMAP_BONE); sprintf_s(win_title, "eigenface%d", i); imshow(win_title, colorface); } //重建人臉 for (int i = min(10, vals.rows); i <min(61, vals.rows); i+=10) { Mat vecs_space = Mat(vecs, Range::all(), Range(0, i)); Mat projection = LDA::subspaceProject(vecs_space, mean, images[0].reshape(1, 1)); Mat reconstruction = LDA::subspaceReconstruct(vecs_space, mean, projection); Mat result = reconstruction.reshape(1, height); normalize(result, result, 0, 255, NORM_MINMAX, CV_8UC1); //char wintitle[40] = {}; sprintf_s(win_title, "recon face %d", i); imshow(win_title, result); } waitKey(0); return 0; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
欄 目:C語(yǔ)言
本文標(biāo)題:Opencv EigenFace人臉識(shí)別算法詳解
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/283.html
您可能感興趣的文章
- 01-10實(shí)現(xiàn)opencv圖像裁剪分屏顯示示例
- 01-10使用opencv拉伸圖像擴(kuò)大分辨率示例
- 01-10基于C++實(shí)現(xiàn)kinect+opencv 獲取深度及彩色數(shù)據(jù)
- 01-10淺談CMake配置OpenCV 時(shí)靜態(tài)鏈接與動(dòng)態(tài)鏈接的選擇
- 01-10OpenCV中C++函數(shù)imread讀取圖片的問(wèn)題及解決方法
- 01-10visual studio 2013中配置opencv圖文教程 Opencv2.4.9安裝配置教程
- 01-10OPENCV批量讀取圖片實(shí)現(xiàn)方法
- 01-10Opencv學(xué)習(xí)教程之漫水填充算法實(shí)例詳解
- 01-10Opencv基于CamShift算法實(shí)現(xiàn)目標(biāo)跟蹤
- 01-10OpenCV實(shí)現(xiàn)人臉檢測(cè)


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)
- 04-02c語(yǔ)言用函數(shù)寫(xiě)分段 用c語(yǔ)言表示分段
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)
- 04-02c語(yǔ)言編寫(xiě)函數(shù)冒泡排序 c語(yǔ)言冒泡排
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎
- 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求
隨機(jī)閱讀
- 04-02jquery與jsp,用jquery
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載