亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Opencv EigenFace人臉識(shí)別算法詳解

 更新時(shí)間:2019年05月21日 08:33:10   作者:東城青年  
這篇文章主要為大家詳細(xì)介紹了Opencv EigenFace人臉識(shí)別算法的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

簡要:

EigenFace是基于PCA降維的人臉識(shí)別算法,PCA是使整體數(shù)據(jù)降維后的方差最大,沒有考慮降維后類間的變化。 它是將圖像每一個(gè)像素當(dāng)作一維特征,然后用SVM或其它機(jī)器學(xué)習(xí)算法進(jìn)行訓(xùn)練。但這樣維數(shù)太多,根本無法計(jì)算。我這里用的是ORL人臉數(shù)據(jù)庫,英國劍橋?qū)嶒?yàn)室拍攝的,有40位志愿者的人臉,在不同表情不同光照下每位志愿者拍攝10張,共有400張圖片,大小為112*92,所以如果把每個(gè)像素當(dāng)做特征拿來訓(xùn)練的話,一張人臉就有10304維特征,這么高維的數(shù)據(jù)根本無法處理。所以需要先對數(shù)據(jù)進(jìn)行降維,去掉一些冗余的特征。

第一步:將ORL人臉圖片的地址統(tǒng)一放在一個(gè)文件里,等會(huì)通過對該文件操作,將圖片全部加載進(jìn)來。

//ofstream一般對文件進(jìn)行讀寫操作,ifstream一般對文件進(jìn)行讀操作
ofstream file;
 file.open("path.txt");//新建并打開文件
 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;//寫入
 } 
 }

得到路勁文件如下圖所示:

 第二步:讀入模型需要輸入的數(shù)據(jù),即用來訓(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ù)測模型

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、對第一張人臉在特征向量空間進(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ù),用來訓(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è)樣本作為測試樣本
 Mat testSample = images[images.size() - 1];
 int testLabel = labels[labels.size() - 1];
 //刪除列表末尾的元素
 images.pop_back();
 labels.pop_back();
 
 //加載,訓(xùn)練,預(yù)測
 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;
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C++實(shí)現(xiàn)的多重繼承功能簡單示例

    C++實(shí)現(xiàn)的多重繼承功能簡單示例

    這篇文章主要介紹了C++實(shí)現(xiàn)的多重繼承功能,結(jié)合簡單實(shí)例形式分析了C++面向?qū)ο蟪绦蛟O(shè)計(jì)中類的定義與繼承相關(guān)操作實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2018-05-05
  • VS報(bào)錯(cuò)C1189及MSB3721解決方法

    VS報(bào)錯(cuò)C1189及MSB3721解決方法

    在使用VS進(jìn)行CUDA編譯時(shí)出現(xiàn)錯(cuò)誤,本文主要介紹了VS報(bào)錯(cuò)C1189及MSB3721解決方法,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-06-06
  • Qt生成隨機(jī)數(shù)的方法

    Qt生成隨機(jī)數(shù)的方法

    本文主要介紹了Qt生成隨機(jī)數(shù),生成隨機(jī)數(shù)主要用到了函數(shù)qsrand和qrand,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • 詳解PID控制器原理

    詳解PID控制器原理

    什么是 PID?它是一種在編程中使用的基本方法,如果正確調(diào)整,可以令人難以置信的有效和準(zhǔn)確,PID代表比例積分微分,3個(gè)單獨(dú)的部分連接在一起,雖然有時(shí)你不需要三個(gè)都使用。例如,您可以改為有P控制,PI控制或PD控制
    2021-06-06
  • C++中引用處理的基本方法

    C++中引用處理的基本方法

    引用不是新定義了一個(gè)變量,而是給已經(jīng)存在的變量取了一個(gè)別名,編譯器不會(huì)為引用變量開辟內(nèi)存空間,他和他引用的變量共用一塊內(nèi)存空間,下面這篇文章主要給大家介紹了關(guān)于C++中引用處理的基本方法,需要的朋友可以參考下
    2022-12-12
  • C語言轉(zhuǎn)義字符詳解

    C語言轉(zhuǎn)義字符詳解

    這篇文章主要介紹了C語言轉(zhuǎn)義字符詳解,本篇文章通過簡要的案例,講解了C語言轉(zhuǎn)義字符該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • 關(guān)于C語言函數(shù)strstr()的分析以及實(shí)現(xiàn)

    關(guān)于C語言函數(shù)strstr()的分析以及實(shí)現(xiàn)

    以下是對C語言中strstr()函數(shù)的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以參考下
    2013-07-07
  • C語言 詳細(xì)講解數(shù)組參數(shù)與指針參數(shù)

    C語言 詳細(xì)講解數(shù)組參數(shù)與指針參數(shù)

    這篇文章主要介紹了C語言中數(shù)組參數(shù)與指針參數(shù)的分析,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • C++示例分析內(nèi)聯(lián)函數(shù)與引用變量及函數(shù)重載的使用

    C++示例分析內(nèi)聯(lián)函數(shù)與引用變量及函數(shù)重載的使用

    為了消除函數(shù)調(diào)用的時(shí)空開銷,C++ 提供一種提高效率的方法,即在編譯時(shí)將函數(shù)調(diào)用處用函數(shù)體替換,類似于C語言中的宏展開。這種在函數(shù)調(diào)用處直接嵌入函數(shù)體的函數(shù)稱為內(nèi)聯(lián)函數(shù)(Inline Function),又稱內(nèi)嵌函數(shù)或者內(nèi)置函數(shù)
    2022-08-08
  • VC程序設(shè)計(jì)中CreateProcess用法注意事項(xiàng)

    VC程序設(shè)計(jì)中CreateProcess用法注意事項(xiàng)

    這篇文章主要介紹了VC程序設(shè)計(jì)中CreateProcess用法注意事項(xiàng),需要的朋友可以參考下
    2014-07-07

最新評(píng)論