opencv3:suft+ransac

环境:vs2017+opencv3.3.0

代码:


#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;
//计算原始图像点位在经过矩阵变换后在目标图像上对应位置  
Point2f getTransformPoint(const Point2f originalPoint, const Mat &transformMaxtri)
{
	Mat originelP, targetP;
	originelP = (Mat_<double>(3, 1) << originalPoint.x, originalPoint.y, 1.0);
	targetP = transformMaxtri * originelP;
	float x = targetP.at<double>(0, 0) / targetP.at<double>(2, 0);
	float y = targetP.at<double>(1, 0) / targetP.at<double>(2, 0);
	return Point2f(x, y);
}

int main(int argc, char *argv[])
{
	//Mat image01 = imread("D:\\myvs\\Projects\\cvTest3\\Project1\\Project1\\拼接一次.jpg");
	Mat image01 = imread("D:\\myvs\\Projects\\cvTest\\cvTest\\拼接结果1.jpg");
	//Mat image01 = imread("D:\\Chrome Download\\dataset\\CAB\\P1010435.JPG");
	Mat image02 = imread("D:\\Chrome Download\\dataset\\CAB\\P1010437.JPG");
	
	//imshow("拼接图像1", image01);
	//imshow("拼接图像2", image02);


	//灰度图转换  
	Mat image1, image2;
	cvtColor(image01, image1, CV_RGB2GRAY);
	cvtColor(image02, image2, CV_RGB2GRAY);

	//提取特征点    
	int minHessian = 4000;//2000
	Ptr<xfeatures2d::SURF> surfDetector = xfeatures2d::SURF::create(minHessian);
	vector<KeyPoint> keyPoint1, keyPoint2,keyPoint3,keyPoint4;
	surfDetector->detect(image1, keyPoint1);
	surfDetector->detect(image2, keyPoint2);

	//特征点描述,为下边的特征点匹配做准备    
	Mat imageDesc1, imageDesc2;
	surfDetector->compute(image1, keyPoint1, imageDesc1);
	surfDetector->compute(image2, keyPoint2, imageDesc2);

	//获得匹配特征点,并提取最优配对     
	FlannBasedMatcher matcher;
	vector<vector<DMatch> > GoodMatchePoints;
	vector<DMatch> matchePoints, matchePoints2;
	//matcher.match(imageDesc1, imageDesc2, matchePoints, Mat());
	matcher.knnMatch(imageDesc1, imageDesc2, GoodMatchePoints, 2);
	

	// Lowe's algorithm,获取优秀匹配点
	for (int i = 0; i < GoodMatchePoints.size(); i++)
	{
		if (GoodMatchePoints[i][0].distance < 0.5 * GoodMatchePoints[i][1].distance)
		{
			matchePoints.push_back(GoodMatchePoints[i][0]);
		}
	}
	cout << "total match points: " << matchePoints.size() << endl;
	sort(matchePoints.begin(), matchePoints.end()); //特征点排序    
													//获取排在前N个的最优匹配特征点  
	vector<Point2f> imagePoints1, imagePoints2;
	int t=10;
	if (matchePoints.size()<10)
	{
		t = matchePoints.size();
	}
		
	for (int i = 0; i < t; i++)
	{
		matchePoints2.push_back(matchePoints[i]);
	}

	for (int i = 0; i < matchePoints.size(); i++)
	{
		imagePoints1.push_back(keyPoint1[matchePoints[i].queryIdx].pt);
		imagePoints2.push_back(keyPoint2[matchePoints[i].trainIdx].pt);
	}
	
	Mat first_match;
	drawMatches(image01, keyPoint1, image02, keyPoint2, matchePoints2, first_match, Scalar(0,0,255));
	imwrite("first_match.jpg ", first_match);
	//imshow("first_match ", first_match);
	Mat m_Fundamental;
	vector<uchar> m_RANSACStatus;
	m_Fundamental=findFundamentalMat(imagePoints1, imagePoints2, m_RANSACStatus, FM_RANSAC);
	
	// 计算野点个数
	int OutlinerCount = 0;
	for (int i = 0; i < matchePoints.size(); i++)
	{
		if (m_RANSACStatus[i] == 0) // 状态为0表示野点
		{
			OutlinerCount++;
		}
	}
	vector<Point2f> m_LeftInlier;
	vector<Point2f> m_RightInlier;
	vector<DMatch> m_InlierMatches;
	// 上面三个变量用于保存内点和匹配关系
	int ptCount = (int)matchePoints.size();
	int InlinerCount = ptCount - OutlinerCount;
	m_InlierMatches.resize(InlinerCount);
	m_LeftInlier.resize(InlinerCount);
	m_RightInlier.resize(InlinerCount);
	InlinerCount = 0;
	for (int i = 0; i < ptCount; i++)
	{
		if (m_RANSACStatus[i] != 0)
		{
			m_LeftInlier[InlinerCount].x = imagePoints1[i].x;
			m_LeftInlier[InlinerCount].y = imagePoints1[i].y;
			m_RightInlier[InlinerCount].x = imagePoints2[i].x;
			m_RightInlier[InlinerCount].y = imagePoints2[i].y;
			m_InlierMatches[InlinerCount].queryIdx = InlinerCount;
			m_InlierMatches[InlinerCount].trainIdx = InlinerCount;
			m_InlierMatches[InlinerCount].distance = matchePoints[i].distance;
			InlinerCount++;
		}
	}
	vector<KeyPoint> key1(InlinerCount);
	vector<KeyPoint> key2(InlinerCount);
	KeyPoint::convert(m_LeftInlier, key1);
	KeyPoint::convert(m_RightInlier, key2);
	printf("最终的匹配点个数为:%d\n",InlinerCount);
	
	for (int i = 0; i < t; i++)
	{
		matchePoints2.push_back(matchePoints[i]);
	}
	Mat first_match2;
	drawMatches(image01, key1, image02, key2,  m_InlierMatches, first_match2, Scalar(0, 0, 255));
	imwrite("first_match2.jpg ", first_match2);

	waitKey();
	return 0;

}

效果图:radsac之前

ransac之后:

 效果还是有的,白线处的错误匹配被消除了,但是没有消除所有错误匹配 : (

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值