您当前的位置: 首页 >  opencv

程序员正茂

暂无认证

  • 5浏览

    0关注

    283博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

opencv 特征匹配

程序员正茂 发布时间:2018-04-23 15:36:26 ,浏览量:5

#include "stdafx.h"
#include 
#include 
#include 


using namespace cv;
using namespace cv::xfeatures2d;
using namespace std;


int main()
{
	Mat img1 = imread("C:\\Users\\administered\\Pictures\\template2.png", IMREAD_GRAYSCALE);
	Mat img2 = imread("C:\\Users\\administered\\Pictures\\target.jpg", IMREAD_GRAYSCALE);
	if (!img1.data || !img1.data)
	{
		printf("could not load image...\n");
		return -1;
	}
	imshow("bag1 image", img1);
	imshow("bag2 image", img2);
	
	int minHessian = 10;
	Ptr detector = SURF::create(minHessian);
	vector keypoints_bag1;
	vector keypoints_bag2;

	Mat descriptor_bag1, descriptor_bag2;
	detector->detectAndCompute(img1, Mat(), keypoints_bag1, descriptor_bag1);
	detector->detectAndCompute(img2, Mat(), keypoints_bag2, descriptor_bag2);

	FlannBasedMatcher matcher;
	vector matches;
	matcher.match(descriptor_bag1, descriptor_bag2, matches);

	double minDist = 400.0;
	double maxDist = 0.0;

	for (int i = 0; i < descriptor_bag1.rows; i++)
	{
		double dist = matches[i].distance;
		if (dist > maxDist)
		{
			maxDist = dist;
		}

		if (dist < minDist)
		{
			minDist = dist;
		}		
	}
	printf("max distance: %f\n", maxDist);
	printf("min distance: %f\n", minDist);

	double sect = maxDist - minDist;
	vector goodMatches;
	for (int i = 0; i < descriptor_bag1.rows; i++)
	{
		double dist = matches[i].distance;
		if (dist < (minDist+0.1*sect))
		{
			goodMatches.push_back(matches[i]);
		}
	}

	Mat matchesImg;
	drawMatches(img1, keypoints_bag1, img2, keypoints_bag2, goodMatches, matchesImg, Scalar::all(-1),
		Scalar::all(-1), vector(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);

	imshow("Flann Matching Result", matchesImg);
	waitKey(0);
    return 0;
}
关注
打赏
1660743125
查看更多评论
立即登录/注册

微信扫码登录

0.1245s