C++ OpenCV圖片特徵匹配

void OpenCVHandle::pashSurf(std::string fixjpg, std::string armjpg)
{
	Mat image01 = imread(fixjpg, 1);
	Mat image02 = imread(armjpg, 1);
	//提取特徵點    
	 
	cv::Ptr<cv::xfeatures2d::SURF> surf = cv::xfeatures2d::SURF::create(); // 海塞矩陣閾值,在這裏調整精度,值越大點越少,越精準 
	vector<KeyPoint> keyPoint1, keyPoint2;
	surf->detect(image01, keyPoint1);
	surf->detect(image02, keyPoint2);

	//特徵點描述,爲下邊的特徵點匹配做準備    
 
	cv::Ptr<cv::xfeatures2d::SURF> surf2 = cv::xfeatures2d::SURF::create();
	Mat imageDesc1, imageDesc2;
	surf2->compute(image01, keyPoint1, imageDesc1);
	surf2->compute(image02, keyPoint2, imageDesc2);

	FlannBasedMatcher matcher;
	vector<vector<DMatch> > matchePoints;
	vector<DMatch> GoodMatchePoints;

	vector<Mat> train_desc(1, imageDesc1);
	matcher.add(train_desc);
	matcher.train();

	matcher.knnMatch(imageDesc2, matchePoints, 2);
	cout << "total match points: " << matchePoints.size() << endl;

	// Lowe's algorithm,獲取優秀匹配點
	for (int i = 0; i < matchePoints.size(); i++)
	{
		if (matchePoints[i][0].distance < 0.39 * matchePoints[i][1].distance)
		{
			GoodMatchePoints.push_back(matchePoints[i][0]);
		}
	}

	Mat first_match;
	drawMatches(image02, keyPoint2, image01, keyPoint1, GoodMatchePoints, first_match);
	imshow("first_match ", first_match);
	waitKey();
}

通過SURF進行特徵點提取並進行特徵匹配

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章