OpenCV學習(38) 人臉識別(3)

     

      前面咱們學習了基於特徵臉的人臉識別,如今咱們學習一下基於Fisher臉的人臉識別,Fisher人臉識別基於LDA(線性判別算法)算法,算法的詳細介紹能夠參考下面兩篇教程內容:html

http://docs.opencv.org/modules/contrib/doc/facerec/facerec_tutorial.html

LDA算法細節參考:ios

http://www.cnblogs.com/mikewolf2002/p/3435750.html算法

 

程序代碼:數據庫

#include "opencv2/core/core.hpp"
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/highgui/highgui.hpp"

#include <iostream>
#include <fstream>
#include <sstream>

using namespace cv;
using namespace std;

static Mat norm_0_255(InputArray _src)
{
Mat src = _src.getMat();
Mat dst;
switch(src.channels())
{
case 1:
cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC1);
break;
case 3:
cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC3);
break;
default:
src.copyTo(dst);
break;
}
return dst;
}

static void read_csv(const string& filename, vector<Mat>& images, vector<int>& labels, char separator = ';')
{
std::ifstream file(filename.c_str(), ifstream::in);
if (!file)
{
string error_message = "No valid input file was given, please check the given filename.";
CV_Error(CV_StsBadArg, error_message);
}
string line, path, classlabel;
while (getline(file, line))
{
stringstream liness(line);
getline(liness, path, separator);
getline(liness, classlabel);
if(!path.empty() && !classlabel.empty())
{
images.push_back(imread(path, 0));
labels.push_back(atoi(classlabel.c_str()));
}
}
}

int main(int argc, const char *argv[])
{

string fn_csv = string("facerec_at_t.txt");
vector<Mat> images;
vector<int> labels;

try
{
read_csv(fn_csv, images, labels);
} catch (cv::Exception& e)
{
cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl;
exit(1);
}

if(images.size() <= 1)
{
string error_message = "This demo needs at least 2 images to work. Please add more images to your data set!";
CV_Error(CV_StsError, error_message);
}

int height = images[0].rows;

Mat testSample = images[images.size() - 1];
int testLabel = labels[labels.size() - 1];

images.pop_back();
labels.pop_back();

Ptr<FaceRecognizer> model = createFisherFaceRecognizer();
model->train(images, labels);
int predictedLabel = model->predict(testSample);

string result_message = format("Predicted class = %d / Actual class = %d.", predictedLabel, testLabel);
cout << result_message << endl;

Mat eigenvalues = model->getMat("eigenvalues");
Mat W = model->getMat("eigenvectors");
Mat mean = model->getMat("mean");
imshow("mean", norm_0_255(mean.reshape(1, images[0].rows)));

for (int i = 0; i < min(16, W.cols); i++)
{
string msg = format("Eigenvalue #%d = %.5f", i, eigenvalues.at<double>(i));
cout << msg << endl;
Mat ev = W.col(i).clone();
Mat grayscale = norm_0_255(ev.reshape(1, height));
Mat cgrayscale;
applyColorMap(grayscale, cgrayscale, COLORMAP_BONE);
imshow(format("fisherface_%d", i), cgrayscale);

}

for(int num_component = 0; num_component < min(16, W.cols); num_component++)
{

Mat ev = W.col(num_component);
Mat projection = subspaceProject(ev, mean, images[0].reshape(1,1));
Mat reconstruction = subspaceReconstruct(ev, mean, projection);
reconstruction = norm_0_255(reconstruction.reshape(1, images[0].rows));
imshow(format("fisherface_reconstruction_%d", num_component), reconstruction);

}

while(1)
waitKey(0);
return 0;
}

      從代碼中咱們能夠看到,最大的區別就是建立人臉識別模式類時候,調用的函數不同,其它代碼和特徵臉識別的代碼同樣,對於train和predict函數來講,調用方式徹底同樣,只是底層的具體算法細節不同。app

    Ptr<FaceRecognizer> model = createFisherFaceRecognizer();函數

  

     下面是Fisher人臉識別類的train函數,從中能夠看到,函數會先調用PCA算法進行降維,以後再執行LDA算法,求得Fisher特徵值和特徵向量。注意投影矩陣是PCA算法的特徵向量和LDA算法特徵向量的乘積學習

    // 先用PCA算法降維perform a PCA and keep (N-C) componentsui

    PCA pca(data, Mat(), CV_PCA_DATA_AS_ROW, (N-C));spa

    // 把數據投影到 PCA空間,再對該數據執行LDA算法code

    LDA lda(pca.project(data),labels, _num_components);

   // 保存總的均值向量

    _mean = pca.mean.reshape(1,1);

    _labels = labels.clone();

    lda.eigenvalues().convertTo(_eigenvalues, CV_64FC1);

   //計算投影矩陣=pca.eigenvectors * lda.eigenvectors.

    // Note: OpenCV stores the eigenvectors by row, so we need to transpose it!

    gemm(pca.eigenvectors, lda.eigenvectors(), 1.0, Mat(), 0.0, _eigenvectors, GEMM_1_T);

    //把原始矩陣投影到新的投影空間

    for(int sampleIdx = 0; sampleIdx < data.rows; sampleIdx++) {

        Mat p = subspaceProject(_eigenvectors, _mean, data.row(sampleIdx));

        _projections.push_back(p);

    }

 

    在程序中,咱們仍然使用AT&T Facedatabase數據庫的圖片,原教程中推薦用Yale Facedatabase A,可是它的圖像格式是gif,OpenCV不支持,只好放棄。

程序代碼:FirstOpenCV34

相關文章
相關標籤/搜索