基于稀疏表示的分类使用opencv,kl1p

时间:2015-02-18 05:38:13

标签: 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)
{
    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;
}

0 个答案:

没有答案