opencv3中FLANN结合SURF进行关键点的描述和匹配

#include
#include
#include

using namespace cv;
using namespace std;

int main()
{
	Mat srcImage = imread("NIBook.jpg");
	imshow("【原图】", srcImage);

	//对BGR空间的图像直接进行计算很费时间,所以,需要转换为灰度图
	Mat srcGrayImage;
	cvtColor(srcImage, srcGrayImage, CV_BGR2GRAY);

	//首先对两幅图像进行特征点的检测和描述子的计算
	vector keyPoint1;
	SURF surf;
	surf.detect(srcGrayImage, keyPoint1);
	Mat descriImage1;
	surf.compute(srcGrayImage, keyPoint1, descriImage1);

	//先对原图的描述子进行保留
	FlannBasedMatcher FLMatcher;
	//因为FlannBasedMatcher类的成员函数add()的参数是一个vector的容器,所以先定义一个这样的变量,并将原图的描述子放入容器中
	vector g_vdescriImage1(1, descriImage1);
	/*g_vdescriImage1.push_back(descriImage1);*/
	//调用FlannBasedMatcher类的成员函数add,将原图的描述子放在FlannBasedMatcher的对象FLMatcher中
	FLMatcher.add(g_vdescriImage1);
	//...........................................................
	FLMatcher.train();

	VideoCapture capture;
	capture.open(0);

	Mat frameImage, frameGrayImage;
	while (waitKey(1) != 27)
	{
		capture >> frameImage;

		//为了提高计算效率,将图像转换为灰度图像
		cvtColor(frameImage, frameGrayImage, CV_BGR2GRAY);

		//计算特征点和描述子
		vector keyPoints2;
		surf.detect(frameGrayImage, keyPoints2);
		Mat descriImage2;
		surf.compute(frameGrayImage, keyPoints2, descriImage2);

		//将之前得到的原图的描述子和现在得到的描述子进行匹配
		//成员函数knnMatch的参数是二维的DMatch向量,所以首先定义一个该容器的向量
		vector> knnDMatches;
		//用之前已经存放原图描述子的对象来计算
		FLMatcher.knnMatch(descriImage2, knnDMatches, 2);

		//采集优秀的匹配点
		vector goodMatches;
		for (size_t i = 0; i < knnDMatches.size(); i++)
		{
			//........................................................................
			if (knnDMatches[i][0].distance < 0.6 * knnDMatches[i][1].distance)
			{
				goodMatches.push_back(knnDMatches[i][0]);
			}
		}

		Mat dstImage;
		drawMatches(frameImage, keyPoints2, srcImage, keyPoint1, goodMatches, dstImage);

		imshow("【结果图】", dstImage);
	}

	return 0;
}

你可能感兴趣的:(opencv学习之路)