I am writing code on Sparse representation based classification for face recognition.
I have used opencv, boost and kl1p (library to perform Basis pursuit).
But, I am not getting good results. I have used PCA for dimensionality reduction.
Please have a look at my code and tell me where I have gone wrong.
#include "opencvheader.h"
#include "matconver.h"
#include "basispursuit.h"
#include "math.h"
using namespace kl1p;
int main(int argc, char** argv)
{
try
{
if (argc < 2)
{
cout << "sparsel.exe [path to folder containing training images] [Test image] ";
return -1;
}
cout << "\n Concatenation of training samples" << endl;
matconver g; // Object
path p = argv[1];
vector<Mat> stored;
int Nc = 0, nc = 75;
g.foldertomat(p, stored, Nc); //user defined Boost functions to iterate through folders
Mat Y(stored.size(), stored[0].rows * stored[0].cols, CV_32FC1);
for (int i = 0; i < stored.size(); i++)
{
Y.row(i) = stored[i].reshape(0, 1);
}
int nEigens = 2;
PCA pca(Y, cv::Mat(), CV_PCA_DATA_AS_ROW, nEigens);
Mat mean = pca.mean;
Mat projectedMat(Y.rows, nEigens, CV_32FC1);
for (int i = 0; i < nEigens; i++)
{
pca.project(Y.row(i), projectedMat.row(i));
}
Mat data(Y.rows, Y.cols, Y.type());
cout << "\n Size of Y \t" << Y.size() << endl;
cout << "\n Size of Reconstruction \t" << projectedMat.size() << endl;
//*************************Consideration of observation vector or test sample***************************************************
Mat yt = imread(argv[2], 0);
// resize(yt,yt, Size(70,70), 0, 0);
norm(yt, NORM_L2);
yt = yt.reshape(0, 1);
Mat projectedMat1; // (yt.rows, nEigens, CV_32FC1);
for (int i = 0; i < nEigens; i++)
{
pca.project(yt, projectedMat1);
}
Mat data1(yt.rows, yt.cols, CV_32FC1);
cout << "\n Size of yt \t" << yt.size() << endl;
cout << "\n Size of Reconstruction of observec matrix\t" << projectedMat1.size() << endl;
//***********************************Basis pursuit for solving l1 minimization problem******************************************
// mat to armadillo of Training samples
cv::Mat imgY(projectedMat.t());
arma::Mat<klab::DoubleReal> Yarma(reinterpret_cast<double*>(imgY.data), imgY.rows, imgY.cols);
std::cout << "size of Yarma" << std::endl << Yarma.n_rows << "x" << Yarma.n_cols << std::endl;
// mat to armadillo of Testing sample
cv::Mat imgyt(projectedMat1.t());
arma::Col<klab::DoubleReal> ytarma(reinterpret_cast<double*>(imgyt.data), imgyt.rows, imgyt.cols);
std::cout << "size of ytarma" << std::endl << ytarma.n_rows<<"x"<<ytarma.n_cols << std::endl;
// Column matrix for storing sparse vectors
arma::Col<klab::DoubleReal> xtarma;
RunExample(ytarma, Yarma, xtarma);
std::cout << "size of xtarma" << std::endl << xtarma.n_rows << "x" << xtarma.n_cols << std::endl;
cout << "xt \t " << xtarma;
// Finding the class of Image
vector<long double> ec;
cv::Mat cvxt(xtarma.n_rows, xtarma.n_cols, CV_32FC1, xtarma.memptr());
cv::Mat cvMatxt(cvxt.t());
Mat xt(xtarma.n_rows, xtarma.n_cols, CV_32FC1);
cv::Mat cvyt(ytarma.n_rows, ytarma.n_cols, CV_32FC1, ytarma.memptr());
cv::Mat cvMatyt(cvyt.t());
Mat Yc, xtc, tempYcxt;
Mat Ycxt(nEigens, 1, CV_64FC1);
for (int u = 0; u < data.rows; u += 5)
{
for (int v = u; v < 5; v++)
{
Yc.push_back(projectedMat.row(v));
}
Yc.convertTo(Yc,CV_32FC1);
for (int q = u; q < 5; q++)
{
xtc.push_back(cvMatxt.col(q));
}
xtc.convertTo(xtc, CV_32FC1);
Mat Ycxt = xtc.t() * Yc;
long double a = norm(cvMatyt, Ycxt, NORM_L2);
//cout << a << endl;
ec.push_back(a);
Ycxt.release();
xtc.release();
Yc.release();
}
for (int z = 0; z < ec.size(); z++)
cout << ec[z] << "\t";
long double res = *max_element(ec.begin(), ec.end());
cout << endl << res;
cout << "\t max value at " << max_element(ec.begin(), ec.end()) - ec.begin();
}
catch (const char* msg)
{
cout<<"exception caught"<<msg;
}
return 0;
}