C++ 基于稀疏表示的opencv分类,kl1p
我正在写基于稀疏表示的人脸识别分类代码 我使用了opencv、boost和kl1p(执行Basis追踪的库)C++ 基于稀疏表示的opencv分类,kl1p,c++,opencv,boost,machine-learning,C++,Opencv,Boost,Machine Learning,我正在写基于稀疏表示的人脸识别分类代码 我使用了opencv、boost和kl1p(执行Basis追踪的库) 但是,我没有得到好的结果。我使用PCA进行降维 请看一下我的代码,告诉我哪里出错了 #include "opencvheader.h" #include "matconver.h" #include "basispursuit.h" #include "math.h" using namespace kl1p; int main(int argc, char** argv) {
但是,我没有得到好的结果。我使用PCA进行降维 请看一下我的代码,告诉我哪里出错了
#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;
}
#包括“opencvheader.h”
#包括“matconver.h”
#包括“basispursuit.h”
#包括“math.h”
使用名称空间kl1p;
int main(int argc,字符**argv)
{
尝试
{
如果(argc<2)
{
你说得不到好结果是什么意思?
?在类1中检测到任何测试图像。我曾尝试在PCA中更改特征向量的数量,但出现了相同的问题。我正在使用耶鲁人脸数据库。只是一个建议,为什么不尝试opencv中提供的PCA,cv::CreateGeneignFaceRecoginazer(10)
现在这里10表示您希望保留10个主成分,就这么简单。如果您感兴趣,那么这里可以提供完整的代码:如果您已经知道这一点,并且出于某种原因不想使用它,那么很抱歉:)。我已经使用PCA进行降维,您的链接提供了人脸识别功能。对于降维,也有随机投影算法,但我还没有找到任何关于这方面的文档来编写代码。如果你对此有任何了解,请告诉我。