学习OpenCV范例(二十一)——Keypoints+Knn+findHomography进行目标定位

本范例的代码主要都是 学习OpenCV——通过KeyPoints进行目标定位这篇博客提供的,然后在它的基础上稍加修改,检测keypoints点的检测器是SURF,获取描述子也是用到SURF来描述,而用到的匹配器是FlannBased,匹配的方式是Knn方式,最后通过findHomography寻找单映射矩阵,perspectiveTransform获得最终的目标,在这个过程中还通过单映射矩阵来进一步去除伪匹配,这里只是贴出代码和代码解析,至于原理还没弄得特别明白,希望接下来可以继续学习,学懂了算法原理再来补充。

1、代码实现

#include "stdafx.h"
#include "opencv2/opencv.hpp"
#include <vector>  
#include <iostream>  

using namespace cv;  
using namespace std;  
Mat src,frameImg;  
int width;  
int height;  
vector<Point> srcCorner(4);  
vector<Point> dstCorner(4);  

static bool createDetectorDescriptorMatcher( const string& detectorType, const string& descriptorType, const string& matcherType,  
	Ptr<FeatureDetector>& featureDetector,  
	Ptr<DescriptorExtractor>& descriptorExtractor,  
	Ptr<DescriptorMatcher>& descriptorMatcher )  
{  
	cout << "< Creating feature detector, descriptor extractor and descriptor matcher ..." << endl;  
	if (detectorType=="SIFT"||detectorType=="SURF")  
		initModule_nonfree();  
	featureDetector = FeatureDetector::create( detectorType );  
	descriptorExtractor = DescriptorExtractor::create( descriptorType );  
	descriptorMatcher = DescriptorMatcher::create( matcherType );  
	cout << ">" << endl;  
	bool isCreated = !( featureDetector.empty() || descriptorExtractor.empty() || descriptorMatcher.empty() );  
	if( !isCreated )  
		cout << "Can not create feature detector or descriptor extractor or descriptor matcher of given types." << endl << ">" << endl;  
	return isCreated;  
}  


bool refineMatchesWithHomography(const std::vector<cv::KeyPoint>& queryKeypoints,    
	const std::vector<cv::KeyPoint>& trainKeypoints,     
	float reprojectionThreshold,    
	std::vector<cv::DMatch>& matches,    
	cv::Mat& homography  )  
{  
	const int minNumberMatchesAllowed = 4;    
	if (matches.size() < minNumberMatchesAllowed)    
		return false;    
	// Prepare data for cv::findHomography    
	std::vector<cv::Point2f> queryPoints(matches.size());    
	std::vector<cv::Point2f> trainPoints(matches.size());    
	for (size_t i = 0; i < matches.size(); i++)    
	{    
		queryPoints[i] = queryKeypoints[matches[i].queryIdx].pt;    
		trainPoints[i] = trainKeypoints[matches[i].trainIdx].pt;    
	}   
  • 7
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值