SIFT特征检测与RANSAC过滤


/*
 *@function SiftDetect.cpp
 *@brief 对sift特征检测和匹配进行测试,并实现RANSAC算法进行过滤错配点
 *@author ltc
 *@date 11:20 Saturday,28 November,2015
 */
#include
#include
#include
using namespace std;
using namespace cv;


//RANSAC算法
vector ransac(vector matches,vector queryKeyPoint,vector trainKeyPoint);

int main(int argc,char* argv[])
{
	//图像读取
	Mat img1,img2;
	img1=imread("1 (2).jpg",CV_WINDOW_AUTOSIZE);
	img2=imread("2 (2).jpg",CV_WINDOW_AUTOSIZE);

	if(img1.empty()||img2.empty())
	{
		return -1;
	}

	//sift特征提取
	SiftFeatureDetector detector;	
	vector keyPoint1,keyPoint2;
	detector.detect(img1,keyPoint1);
	detector.detect(img2,keyPoint2);
	//cout<<"Number of KeyPoint1:"< matches;

	vector> matches_knn;
	matcher.match(des1,des2,matches);
	matcher.knnMatch(des1,des2,matches_knn,2);

//	cout<<"matches_knn.size:"< match_knn;
	for(int i=0;i matches_ransac=ransac(matches,keyPoint1,keyPoint2);
	Mat img_match,img_match_flann;
	
	drawMatches(img1,keyPoint1,img2,keyPoint2,matches_ransac,img_match);
	drawMatches(img1,keyPoint1,img2,keyPoint2,match_knn,img_match_flann);

	imshow("img_match",img_match);
	imshow("img_match_flann",img_match_flann);
	
	//for(size_t i=0;i ransac(vector matches,vector queryKeyPoint,vector trainKeyPoint)
{
	//定义保存匹配点对坐标
	vector srcPoints(matches.size()),dstPoints(matches.size());
	//保存从关键点中提取到的匹配点对的坐标
	for(int i=0;i inliersMask(srcPoints.size()); 
	//匹配点对进行RANSAC过滤
	homography = findHomography(srcPoints,dstPoints,CV_RANSAC,5,inliersMask);
	//RANSAC过滤后的点对匹配信息
	vector matches_ransac;
	//手动的保留RANSAC过滤后的匹配点对
	for(int i=0;i


SIFT原理:http://blog.csdn.net/quincuntial/article/details/50460975


匹配结果:

SIFT特征检测与RANSAC过滤_第1张图片

你可能感兴趣的:(Opencv,计算机视觉)