1. 程式人生 > >opencv學習筆記六十五:人臉識別演算法之EigenFace

opencv學習筆記六十五:人臉識別演算法之EigenFace

簡要:

特徵臉演算法是將影象每一個畫素當作一維特徵,然後用SVM或其它機器學習演算法進行訓練。但這樣維數太多,根本無法計算。我這裡用的是ORL人臉資料庫,英國劍橋實驗室拍攝的,有40位志願者的人臉,在不同表情不同光照下每位志願者拍攝10張,共有400張圖片,大小為112*92,所以如果把每個畫素當做特徵拿來訓練的話,一張人臉就有10304維特徵,這麼高維的資料根本無法處理。所以需要先對資料進行降維,去掉一些冗餘的特徵。PCA降維在我的這篇部落格中有詳細推導https://blog.csdn.net/qq_24946843/article/details/81775368

 第一步:將ORL人臉圖片的地址統一放在一個檔案裡,等會通過對該檔案操作,將圖片全部載入進來。

//ofstream一般對檔案進行讀寫操作,ifstream一般對檔案進行讀操作
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);//將數字轉換成字元
			file << str << endl;//寫入
		}		
	}

得到路勁檔案如下圖所示:

 第二步:讀入模型需要輸入的資料,即用來訓練的影象vector<Mat>images和標籤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)將字串轉換為整數型
	}

第三步:載入、訓練、預測模型

Ptr<BasicFaceRecognizer> model = EigenFaceRecognizer::create();
	model->train(images, labels);
	int predictedLabel = model->predict(testSample);
	printf("actual label:%d,predict label :%d\n", testLabel, predictedLabel);

補充:

1、顯示平均臉

//計算特徵值特徵向量及平均值
	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);//第一個引數為通道數,第二個引數為多少行
	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、對第一張人臉在特徵向量空間進行人臉重建(分別基於前10,20,30,40,50,60個特徵向量進行人臉重建)

//重建人臉
	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);

	//讀入模型需要輸入的資料,用來訓練的影象vector<Mat>images和標籤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)將字串轉換為整數型
	}
	int height = images[0].rows;
	int width = images[0].cols;
	printf("height:%d,width:%d\n", height, width);
	//將最後一個樣本作為測試樣本
	Mat testSample = images[images.size() - 1];
	int testLabel = labels[labels.size() - 1];
	//刪除列表末尾的元素
	images.pop_back();
	labels.pop_back();
	
	//載入,訓練,預測
	Ptr<BasicFaceRecognizer> model = EigenFaceRecognizer::create();
	model->train(images, labels);
	int predictedLabel = model->predict(testSample);
	printf("actual label:%d,predict label :%d\n", testLabel, predictedLabel);

	//計算特徵值特徵向量及平均值
	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);//第一個引數為通道數,第二個引數為多少行
	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;
}