OpenCv:SURF特征匹配

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

#include <opencv2/opencv.hpp>
#include <opencv2/xfeatures2d.hpp>
#include <iostream>

using namespace cv;
using namespace std;
using namespace cv::xfeatures2d;

int main(int argc, char** argv)
{
	//1、载入两张图片
	Mat img1 = imread("1.jpg", IMREAD_GRAYSCALE);
	Mat img2 = imread("2.jpg", IMREAD_GRAYSCALE);
	if (!img1.data || !img2.data) {
		return -1;
	}
	imshow("image1", img1);
	imshow("image2", img2);
	
	//2、使用SURF算子检测关键点,
	//   计算描述符(也就是特征向量)
	int minHessian = 800;
	Ptr<SURF> detector = SURF::create(minHessian);
	vector<KeyPoint> keypoints_1;
	vector<KeyPoint> keypoints_2;

	Mat descriptor_1, descriptor_2;
	detector->detectAndCompute(img1, Mat(), keypoints_1, descriptor_1);
	detector->detectAndCompute(img2, Mat(), keypoints_2, descriptor_2);

	//3、使用BruteForce进行匹配,实例化一个匹配器

	BFMatcher matcher(NORM_L2);
	vector<DMatch> matches;
	matcher.match(descriptor_1, descriptor_2, matches);

	//4、绘制从两个图像中匹配出的关键点
	Mat matchesImg;
	drawMatches(img1, keypoints_1, img2, keypoints_2, matches, matchesImg);
	imshow("Descriptor Demo", matchesImg);

	waitKey(0);
	return 0;
}

在这里插入图片描述
或者

#include <stdio.h>
#include <iostream>
#include "opencv2/core.hpp"
#include "opencv2/features2d.hpp"
#include "opencv2/xfeatures2d.hpp"
#include "opencv2/highgui.hpp"

using namespace cv;
using namespace cv::xfeatures2d;

void readme();
/* @function main */
int main(int argc, char** argv)
{
	//if (argc != 3)
	//{
	//	readme(); return -1;
	//}
	Mat img_1 = imread("1.jpg", IMREAD_GRAYSCALE);
	Mat img_2 = imread("2.jpg", IMREAD_GRAYSCALE);
	if (!img_1.data || !img_2.data)
	{
		std::cout << " --(!) Error reading images " << std::endl; return -1;
	}
	//-- Step 1: Detect the keypoints using SURF Detector
	int minHessian = 400;
	Ptr<SURF> detector = SURF::create(minHessian);
	std::vector<KeyPoint> keypoints_1, keypoints_2;
	detector->detect(img_1, keypoints_1);
	detector->detect(img_2, keypoints_2);
	//-- Draw keypoints
	Mat img_keypoints_1; Mat img_keypoints_2;
	drawKeypoints(img_1, keypoints_1, img_keypoints_1, Scalar::all(-1), DrawMatchesFlags::DEFAULT);
	drawKeypoints(img_2, keypoints_2, img_keypoints_2, Scalar::all(-1), DrawMatchesFlags::DEFAULT);
	//-- Show detected (drawn) keypoints
	imshow("Keypoints_1", img_keypoints_1);
	imshow("Keypoints_2", img_keypoints_2);
	//计算特征向量
	Ptr<SURF>extractor = SURF::create();
	Mat descriptors1, descriptors2;
	extractor->compute(img_1, keypoints_1, descriptors1);
	extractor->compute(img_2, keypoints_2, descriptors2);
	//使用BruteForce进行匹配
	Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("BruteForce");
	std::vector< DMatch > matches;
	matcher->match(descriptors1, descriptors2, matches);
	//绘制直线连接关键点
	Mat imgMatches;
	drawMatches(img_1, keypoints_1, img_2, keypoints_2, matches, imgMatches);
	imshow("match", imgMatches);
	waitKey(0);
	return 0;
}
/* @function readme */
void readme()
{
	std::cout << " Usage: ./SURF_detector <img1> <img2>" << std::endl;
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值