基於opencv的圖像拼接(一): sift特徵點提取

網上有很多關於sift理論知識的介紹,在這裏就不贅述了;

附上有關sift特徵點提取的相關程序,需要注意的如下:

1. 使用opencv封裝好的sift模塊時, 需要加上

#include <opencv2/nonfree/nonfree.hpp>

2. SiftFeatureDetector 和 SiftDescriptorExtractor 意義不同,

SiftFeatureDetector 是爲提取特徵點位置和角度,一般保存到keypoint中,多用於在圖中顯現特徵點;

SiftDescriptorExtractor是真正在後續實驗中要用到的特徵向量;

在程序中也會有專門講解。


#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/nonfree/nonfree.hpp>  //sift算子需要用到的
#include <opencv2/legacy/legacy.hpp>

#include <vector>
using namespace std;
using namespace cv;

Mat cacSIFTFeatureAndCompare(Mat srcImage1, Mat srcImage2 )
{

   //灰度及歸一化
	Mat grayMat1, grayMat2;
	cvtColor(srcImage1, grayMat1, CV_BGR2GRAY);
	normalize(grayMat1, grayMat1, 0, 255, NORM_MINMAX);
	cvtColor(srcImage2, grayMat2, CV_BGR2GRAY);
	normalize(grayMat2, grayMat2, 0, 255, NORM_MINMAX);

	//定義sift描述子
	SiftFeatureDetector detector;
	SiftDescriptorExtractor extractor;//特徵提取器

	//特徵點檢測
	vector<KeyPoint>keypoint1;
	vector<KeyPoint>keypoint2;
	detector.detect(grayMat1, keypoint1);//提取到特徵點的位置和角度,保存在keypoint
	detector.detect(grayMat2, keypoint2);

	//計算特徵點描述子
	Mat descriptions1, descriptions2;
	extractor.compute(grayMat1, keypoint1, descriptions1);
	extractor.compute(grayMat2, keypoint2, descriptions2);//做實驗中要用到的
	Mat src_description1, src_description2;

	drawKeypoints(srcImage1,keypoint1,src_description1);
	drawKeypoints(srcImage2,keypoint2,src_description2);

	imshow("pic1_descriptions", src_description1);
	imshow("pic2_descriptions", src_description2);



	//特徵點匹配  匹配用到的是比較簡單的匹配
	vector<DMatch>matches;
	BruteForceMatcher<L2<float>>matcher;
	matcher.match(descriptions1,descriptions2,matches);

	//二分排序
	int N = 80;
	nth_element(matches.begin(), matches.begin()+N-1,matches.end());
	matches.erase(matches.begin()+N, matches.end());

	//繪製檢測結果
	Mat matchMat;
	drawMatches(srcImage1,keypoint1,srcImage2,keypoint2,matches,matchMat);
	return matchMat;
	
}

	int main()
	{
		Mat srcImage1 = imread("E:\\my\\pic_save\\angle_y1.jpg", 1);
		Mat srcImage2 = imread("E:\\my\\pic_save\\angle_x1.jpg", 1);
		Mat resSiftMatchMat = cacSIFTFeatureAndCompare(srcImage1,srcImage2);
		imshow("resSiftMatchMat", resSiftMatchMat);

		waitKey(0);
		return 0;

	}






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