コード例 #1
0
void costfunc::compute_correspondences(mat &ptns, mat &sphM, uvec &matchId) {

	/* ptns: reference to observed points
	 * sphM: reference to spheres model
	 * matchId:returned vector containing the matchId
	 *
	 * Match every point in the observed model to the closest sphere of
	 * the hand model
	 *
	 * This uses the BFMatcher provided by OpenCV
	 *
	 * ==> need to firstly convert arma::mat to cv::mat
	 */

	// start conversion from arma::mat --> cv::mat
	fmat spheresM = conv_to<fmat>::from(sphM);
	fmat ptncloud = conv_to<fmat>::from(ptns);

	cv::Mat cvspheresM(spheresM.n_cols, spheresM.n_rows, CV_32F, spheresM.memptr());
	cv::Mat cvptncloud(ptncloud.n_cols, ptncloud.n_rows, CV_32F, ptncloud.memptr());

	// cv::mat is row-major while arma::mat is col-major
	cvspheresM = cvspheresM.t();
	cvptncloud = cvptncloud.t();

	cv::BFMatcher matcher(cv::NORM_L2); // initialise a brute-force matcher
	std::vector<cv::DMatch> matches; // initialise a container for the matches

	matcher.match(cvptncloud, cvspheresM, matches); // perform matching

	int cnt = ptncloud.n_rows;
	matchId = zeros<uvec>(cnt);

	for (int i = 0; i < cnt; i++) {
		matchId.at(i) = matches[i].trainIdx;
	}

}