【图像处理】(特征检测与匹配)SIFT算法OpenCV实现

SIFT算法要比SURF算法慢一点

#include<opencv2/core/core.hpp>
#include<opencv2/features2d/features2d.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/nonfree/features2d.hpp>
#include<opencv2/legacy/legacy.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main()
{
	Mat trainImage = imread("1.jpg", 1);
	Mat captureImage = imread("2.jpg", 1);
	Mat trainImage_gray, captureImage_gray;
	if (!trainImage.data || !captureImage.data)
	{
		cout << "读取图片错误,请确定图片是否存在" << endl;
		return false;
	}
	imshow("原始图1", trainImage);
	imshow("原始图2", captureImage);
	//灰度化
	cvtColor(trainImage,trainImage_gray,CV_BGR2GRAY);
	cvtColor(captureImage, captureImage_gray, CV_BGR2GRAY);
	//检测SIFT关键点、提取训练对象描述符
	vector<KeyPoint> train_KP, test_KP;
	SiftFeatureDetector feaDetector;
	feaDetector.detect(trainImage_gray,train_KP);//检测关键点
	feaDetector.detect(captureImage_gray, test_KP);

	SiftDescriptorExtractor feaExtractor;
	Mat trainDescription, testDescription;
	feaExtractor.compute(trainImage_gray,train_KP,trainDescription);//描述子
	feaExtractor.compute(captureImage_gray, test_KP, testDescription);

	//进行基于描述符的暴力匹配
	BFMatcher matcher;
	vector<Mat> train_desc_collection(1, trainDescription);
	matcher.add(train_desc_collection);
	matcher.train();

	//匹配训练和测试描述符
	vector<vector<DMatch>> matches;
	matcher.knnMatch(testDescription,matches,2);

	//根据劳氏算法,得到优秀的匹配点
	vector<DMatch> goodMatches;
	for (unsigned int i = 0; i < matches.size(); i++)
	{
		if(matches[i][0].distance < 0.6 * matches[i][1].distance)
			goodMatches.push_back(matches[i][0]);
	}

	Mat dst;
	drawMatches(captureImage,test_KP,trainImage,train_KP,goodMatches,dst);
	imshow("匹配窗口",dst);
	waitKey(0);
	return 0;
}

效果图:

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值