示例#1
0
 // MOOSE suffixes [0] to all elements to path. Remove [0] with null
 // character whenever possible. For n > 0, [n] should not be touched. Its
 // the user job to take the pain and write the correct path.
 string createMOOSEPath( const string& path )
 {
     string s = path;                        /* Local copy */
     // Remove [0] from paths. They will be annoying for normal users.
     std::string::size_type n = 0;
     string zeroIndex("[0]");
     while( (n = s.find( zeroIndex, n )) != std::string::npos )
         s.erase( n, zeroIndex.size() );
     return s;
 }
示例#2
0
    // Fix the given path.
    string createMOOSEPath( const string& path )
    {
        string s = path;                        /* Local copy */
        // Remove [0] from paths. They will be annoying for normal users.
        std::string::size_type n = 0;
        string zeroIndex("[0]");
        while( (n = s.find( zeroIndex, n )) != std::string::npos )
            s.erase( n, zeroIndex.size() );

        string undesired = ":?\"<>|";
        for (size_t i = 0; i < s.size() ; ++i)
        {
            bool found = undesired.find(s[i]) != string::npos;
            if(found) s[i] = '_';
        }
        return s;
    }
/*! @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;
}