opencv flann

//【1】载入图像,显示并转换为灰度图
	Mat trainImage = imread("1.jpg"), trainImage_gray;
	imshow("原始图", trainImage);
	cvtColor(trainImage, trainImage_gray, COLOR_BGR2GRAY);

	//【2】检测surf关键点,提取训练图像描述符
	vector<KeyPoint>train_keyPoint;
	Mat trainDescriptor;
	Ptr<SurfFeatureDetector>featureDetector = SurfFeatureDetector::create(80);
	featureDetector->detect(trainImage_gray, train_keyPoint);
	Ptr<SurfDescriptorExtractor>featureExtractor = SurfDescriptorExtractor::create();
	featureExtractor->compute(trainImage_gray, train_keyPoint, trainDescriptor);

	//【3】创建基于FLANN的描述符匹配对象
	FlannBasedMatcher matcher;
	vector<Mat>train_desc_collection(1, trainDescriptor);
	matcher.add(train_desc_collection);
	matcher.train();

	//【4】创建视频对象,定义帧率
	VideoCapture cap(1);
	unsigned frameCount = 0;//帧数

	//【5】不断循环,直到Q键被按下
	while (char(waitKey(1)) != 'q')
	{
		//<1>参数设置
		int64 time0 = getTickCount();
		Mat testImage, testImage_gray;
		cap >> testImage;//采集视频到testImage
		//imshow("摄像头", testImage);
		if (testImage.empty())
			continue;
		//<2>转换为灰度图
		cvtColor(testImage, testImage_gray, COLOR_BGR2GRAY);

		//<3>检测关键点,提取测试图像描述符
		vector<KeyPoint>test_KeyPoint;
		Mat testDescriptor;
		featureDetector->detect(testImage_gray, test_KeyPoint);
		featureExtractor->compute(testImage_gray, test_KeyPoint, testDescriptor);

		//<4>匹配训练和测试描述符
		vector<vector<DMatch>>matches;
		matcher.knnMatch(testDescriptor, matches, 2);

		//<5>根据劳氏算法,得到优秀的匹配点
		vector<DMatch>goodMatches;
		for (unsigned int i = 0; i < matches.size(); i++)
		{
			if (matches[i][0].distance < 0.6*matches[i][1].distance)
				goodMatches.push_back(matches[i][0]);
		}
		//<6>绘制匹配点并显示窗口
		Mat dstimage;
		drawMatches(testImage, test_KeyPoint, trainImage, train_keyPoint, goodMatches, dstimage);
		imshow("匹配窗口", dstimage);

		//<7>输出帧率信息
		cout << "当前帧率为:" << getTickFrequency() / (getTickCount() - time0) << endl;
	}


	return 0;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值