TEST(greedy_algorithms_test, GPGA) { // load the MATLAB MAT file MatlabIO matio; string file_path = "/home/xiejianhe/Pycharm/SPDA/csgpda/data/temp_20160322.mat"; bool ok = matio.open(file_path, "r"); if (!ok) { EXPECT_TRUE(false); } else { // read all of the variables in the file std::vector<MatlabIOContainer> variables; variables = matio.read(); matio.close(); // load the matrix by name in OpenCV style Mat D = matio.find<Mat>(variables, "D"); Mat DTY = matio.find<Mat>(variables, "DTY"); Mat Y = matio.find<Mat>(variables, "Y"); // convert cvMat to Eigen // and call pga_newton function MatrixXd dict, y_eigen, dty_eigen; cv2eigen(D, dict); cv2eigen(Y, y_eigen); cv2eigen(DTY, dty_eigen); int dict_row = dict.rows(), dict_col = dict.cols(), patch_num = y_eigen.cols(); const int k = 2; MatrixXd alphahat = MatrixXd::Zero(dict_col, patch_num); MatrixXd xhat = MatrixXd::Zero(dict_row, patch_num); double diver_error = GPGA(dict, dty_eigen, y_eigen, k, xhat, alphahat); } }
/*! @brief deserialize a Matlab .Mat file into memory * * deserialize a valid version 5 .Mat file using the underlying * MatlabIO parser, and populate the model fields. If any of the fields * do not exist, or a bad type cast is attempted, an exception will be thrown * * @param filename the path to the model file * @return true if the file was found, opened and verified to be a valid Matlab * version 5 file * @throws boost::bad_any_cast, exception */ bool MatlabIOModel::deserialize(const std::string& filename) { // open the Mat File for reading MatlabIO cvmatio; bool ok = cvmatio.open(filename, "r"); if (!ok) return false; // read all of the variables from the file vectorMatlabIOContainer variables; variables = cvmatio.read(); // populate the model, one variable at a time try { name_ = cvmatio.find<std::string>(variables, "name"); } catch (...) { name_ = boost::filesystem::path(filename).stem().c_str(); } /* cv::Mat pa = cvmatio.find<cv::Mat>(variables, "pa"); for (unsigned int n = 0; n < pa.cols*pa.rows; ++n) conn_.push_back(pa.at<double>(n)); zeroIndex(conn_); */ //model vectorMatlabIOContainer model = cvmatio.find<vector2DMatlabIOContainer>(variables, "model")[0]; nscales_ = cvmatio.find<double>(model, "interval"); thresh_ = cvmatio.find<double>(model, "thresh"); binsize_ = cvmatio.find<double>(model, "sbin"); norient_ = 18; // ------------------------------ // get the filters vector2DMatlabIOContainer filters = cvmatio.find<vector2DMatlabIOContainer>(model, "filters"); for (unsigned int f = 0; f < filters.size(); ++f) { // flatten the filters to 2D cv::Mat filter = cvmatio.find<cv::Mat>(filters[f], "w"); const unsigned int M = filter.rows; const unsigned int N = filter.cols; vectorMat filter_vec; cv::split(filter, filter_vec); const unsigned int C = filter_vec.size(); flen_ = C; cv::Mat filter_flat(cv::Size(N * C, M), cv::DataType<double>::type); for (unsigned int m = 0; m < M; ++m) { for (unsigned int c = 0; c < C; ++c) { for (unsigned int n = 0; n < N; ++n) { filter_flat.at<double>(m,n*C+c) = filter_vec[c].at<double>(m,n); } } } filtersw_.push_back(filter_flat); //filtersi_.push_back(cvmatio.find<double>(filters[f], "i")); } // ------------------------------ // get the components vectorMatlabIOContainer components = cvmatio.find<vectorMatlabIOContainer>(model, "components"); const unsigned int ncomponents = components.size(); biasid_.resize(ncomponents); filterid_.resize(ncomponents); defid_.resize(ncomponents); parentid_.resize(ncomponents); for (unsigned int c = 0; c < ncomponents; ++c) { // a single component is a struct array vector2DMatlabIOContainer component = components[c].data<vector2DMatlabIOContainer>(); const unsigned int nparts = component.size(); biasid_[c].resize(nparts); filterid_[c].resize(nparts); defid_[c].resize(nparts); parentid_[c].resize(nparts); // for each element, add to the component indices for (unsigned int p = 0; p < nparts; ++p) { cv::Mat defid = cvmatio.find<cv::Mat>(component[p], "defid"); cv::Mat filterid = cvmatio.find<cv::Mat>(component[p], "filterid"); int parentid = cvmatio.find<double>(component[p], "parent"); // the biasid type can change, depending on the number of elements saved cv::Mat biasid = cvmatio.find<cv::Mat>(component[p], "biasid"); biasid_[c][p] = vectori(biasid.begin<double>(), biasid.end<double>()); zeroIndex(biasid_[c][p]); parentid_[c][p] = parentid; filterid_[c][p] = vectori(filterid.begin<double>(), filterid.end<double>()); defid_[c][p] = vectori(defid.begin<double>(), defid.end<double>()); // re-index from zero (Matlab uses 1-based indexing) zeroIndex(parentid_[c][p]); zeroIndex(filterid_[c][p]); zeroIndex(defid_[c][p]); } } // ------------------------------ // get the defs vector2DMatlabIOContainer defs = cvmatio.find<vector2DMatlabIOContainer>(model, "defs"); const unsigned int ndefs = defs.size(); for (unsigned int n = 0; n < ndefs; ++n) { defw_.push_back(cvmatio.find<cv::Mat>(defs[n], "w")); //defi_.push_back(cvmatio.find<double>(defs[n], "i")); cv::Mat anchor = cvmatio.find<cv::Mat>(defs[n], "anchor"); anchors_.push_back(cv::Point(anchor.at<double>(0), anchor.at<double>(1))); } zeroIndex(anchors_); // ------------------------------ // get the bias vector2DMatlabIOContainer bias = cvmatio.find<vector2DMatlabIOContainer>(model, "bias"); const unsigned int nbias = bias.size(); for (unsigned int n = 0; n < nbias; ++n) { biasw_.push_back(cvmatio.find<double>(bias[n], "w")); //biasi_.push_back(cvmatio.find<double>(bias[n], "i")); } return true; }