OpenCV學習筆記__特征檢測與匹配之 SURF算法(轉)


步驟:
特征檢測 —— 特征描述 —— 特征匹配
 
實現流程:
(1)特征檢測:SurfFeatureDetector類 . detect( )函數                 
(2)特征描述:SurfDescriptorExtractor類 . compute( )函數
(3)特征匹配:BruteForceMatcher類 . match( )函數
(這三步的實現都類似:類實例化一個對象,(定義vector或者Mat存放結果),調用函數,將計算結果存儲供給下一步使用)
(4)最后顯示“匹配圖”:drawMatches( )函數
 
實現代碼:
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/nonfree/nonfree.hpp>
#include<opencv2/legacy/legacy.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main()
{
	//【1】載入素材圖
	Mat srcImage1 = imread("1.jpg", 1);
	Mat srcImage2 = imread("2.jpg", 1);
	if (!srcImage1.data || !srcImage2.data)
	{
		printf("讀取圖片錯誤,請確定目錄下是否有imread函數指定的圖片存在~! \n"); return false;
	}

	//【2】使用SURF算子檢測關鍵點
	int minHessian = 700;//SURF算法中的hessian閾值
	SurfFeatureDetector detector(minHessian);//定義一個SurfFeatureDetector(SURF) 特征檢測類對象  
	std::vector<KeyPoint> keyPoint1, keyPoints2;//vector模板類,存放任意類型的動態數組

	//【3】調用detect函數檢測出SURF特征關鍵點,保存在vector容器中
	detector.detect(srcImage1, keyPoint1);
	detector.detect(srcImage2, keyPoints2);

	//【4】計算描述符(特征向量)
	SurfDescriptorExtractor extractor;
	Mat descriptors1, descriptors2;
	extractor.compute(srcImage1, keyPoint1, descriptors1);
	extractor.compute(srcImage2, keyPoints2, descriptors2);

	//【5】使用BruteForce進行匹配
	// 實例化一個匹配器
	BruteForceMatcher< L2<float> > matcher;
	std::vector< DMatch > matches;
	//匹配兩幅圖中的描述子(descriptors)
	matcher.match(descriptors1, descriptors2, matches);

	//【6】繪制從兩個圖像中匹配出的關鍵點
	Mat imgMatches;
	drawMatches(srcImage1, keyPoint1, srcImage2, keyPoints2, matches, imgMatches);//進行繪制

	//【7】顯示效果圖
	imshow("匹配圖", imgMatches);

	waitKey(0);
	return 0;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM