OPENCV 图像图片SURF图片特征提取和匹配+投影映射矩阵的求解

#include "stdafx.h"
#include <opencv2/core/core.hpp>  
#include <opencv2/highgui/highgui.hpp> 
#include "imgproc/imgproc.hpp"  
#include <opencv2/features2d/features2d.hpp> 
#include "opencv2/xfeatures2d/nonfree.hpp" 
#include<vector>
#include "opencv2/opencv.hpp"

using namespace cv;
using namespace std;
/*************************************************
Copyright:bupt
Author:
Date:2010-08-25
Description:描述主要实现的功能  使用Fast算子提取特征点
**************************************************/
template<class KPMatcher>
struct SURFMatcher
{
	KPMatcher matcher;
	template<class T>
	void match(const T& in1, const T& in2, std::vector<cv::DMatch>& matches)
	{
		matcher.match(in1, in2, matches);
	}
};
int main(int argc, char*argv[])
{	Mat objImage = imread("poster_book.jpg", IMREAD_COLOR);
	Mat sceneImage = imread("poster_book_1.jpg", IMREAD_COLOR);
	//-- Step 1: Detect the keypoints using SURF Detector
	int minHessian = 400;
	Ptr<xfeatures2d::SURF> detector = xfeatures2d::SURF::create(minHessian);
	std::vector<KeyPoint> obj_keypoint, scene_keypoint;
	detector->detect(objImage, obj_keypoint);
	detector->detect(sceneImage, scene_keypoint);
	//computer the descriptors
	Mat obj_descriptors, scene_descriptors;
	detector->compute(objImage, obj_keypoint, obj_descriptors);
	detector->compute(sceneImage, scene_keypoint, scene_descriptors);
	//use BruteForce to match,and get good_matches
	BFMatcher matcher;
	vector<DMatch> matches;
	matcher.match(obj_descriptors, scene_descriptors, matches);
	sort(matches.begin(), matches.end());  //筛选匹配点
	vector<DMatch> good_matches;
	for (int i = 0; i < min(50, (int)(matches.size()*0.15)); i++) {
		good_matches.push_back(matches[i]);
	}
	//draw matches
	Mat imgMatches;
	drawMatches(objImage, obj_keypoint, sceneImage, scene_keypoint, good_matches, imgMatches);
	//get obj bounding
	vector<Point2f> obj_good_keypoint;
	vector<Point2f> scene_good_keypoint;
	for (int i = 0; i < good_matches.size(); i++) {
		obj_good_keypoint.push_back(obj_keypoint[good_matches[i].queryIdx].pt);
		scene_good_keypoint.push_back(scene_keypoint[good_matches[i].trainIdx].pt);
	}
	vector<Point2f> obj_box(4);
	vector<Point2f> scene_box(4);
	obj_box[0] = Point(0, 0);
	obj_box[1] = Point(objImage.cols, 0);
	obj_box[2] = Point(objImage.cols, objImage.rows);
	obj_box[3] = Point(0, objImage.rows);
	Mat H = findHomography(obj_good_keypoint, scene_good_keypoint, RANSAC); //find the perspective transformation between the source and the destination
	perspectiveTransform(obj_box, scene_box, H);
	line(imgMatches, scene_box[0] + Point2f((float)objImage.cols, 0), scene_box[1] + Point2f((float)objImage.cols, 0), Scalar(0, 255, 0), 2);
	line(imgMatches, scene_box[1] + Point2f((float)objImage.cols, 0), scene_box[2] + Point2f((float)objImage.cols, 0), Scalar(0, 255, 0), 2);
	line(imgMatches, scene_box[2] + Point2f((float)objImage.cols, 0), scene_box[3] + Point2f((float)objImage.cols, 0), Scalar(0, 255, 0), 2);
	line(imgMatches, scene_box[3] + Point2f((float)objImage.cols, 0), scene_box[0] + Point2f((float)objImage.cols, 0), Scalar(0, 255, 0), 2);
	//show the result                                                                   //show the result
	imshow("匹配图", imgMatches);
	waitKey(0);
	return 0;
	

}

转载地址:https://blog.csdn.net/nnnnnnnnnnnny/article/details/52182091




OPenCV3.3+VS2017验证可用

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值