【openCV】特征点提取与匹



#include "opencv2/opencv.hpp"  
#include "opencv2/core/core.hpp"    
#include "highgui.h"    
#include "opencv2/imgproc/imgproc.hpp"    
#include "opencv2/features2d/features2d.hpp"    
#include "opencv2/nonfree/nonfree.hpp" 
#include "opencv2/legacy/legacy.hpp"


using namespace cv;
int main(int argc, char** argv)
{
	//两张图像及sift检测器    
	Mat orgimage1 = imread("image1.jpg");
	Mat orgimage2 = imread("image2.jpg");
	Mat image1, image2;
	SIFT sift1, sift2;
	cvtColor(orgimage1, image1, CV_BGR2GRAY);
	cvtColor(orgimage2, image2, CV_BGR2GRAY);


	//sift操作  
	vector<KeyPoint>keyPoint1, keyPoint2;
	Mat descriptors1, descriptors2, mascara;
	sift1(image1, mascara, keyPoint1, descriptors1);
	sift2(image2, mascara, keyPoint2, descriptors2);


	//匹配器及算子  
	BruteForceMatcher<L2<float>> matcher;//原先在features2d里,2.4.2后在opencv2/legacy/legacy.hpp
	vector<DMatch>matches;
	matcher.match(descriptors1, descriptors2, matches);


	//提取部分匹配结果  
	std::nth_element(matches.begin(), matches.begin() + 14, matches.end());
	matches.erase(matches.begin() + 15, matches.end());


	//显示结果  
	namedWindow("SIFTmatchResult");
	Mat resultImage;
	drawMatches(image1, keyPoint1, image2, keyPoint2, matches, resultImage, Scalar(255, 255, 255));
	imshow("SIFTmatchResult", resultImage);
	waitKey(0);
	imwrite("output.jpg", resultImage);


	image1.release();
	image2.release();
	resultImage.release();
	descriptors1.release();
	descriptors2.release();
	mascara.release();
	
return 0;
}


#include <vector>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/features2d.hpp"
using namespace cv;

int main(int argc, char** argv)
{
	Mat img1 = imread("1.jpg");
	Mat img2 = imread("2.jpg");

	
	//-- Step 1: Detect the keypoints using SURF Detector
	int minHessian = 100;

	//SiftFeatureDetector detector(minHessian);
	//SurfFeatureDetector detector(minHessian);
	//FastFeatureDetector detector(40);
	std::vector<KeyPoint> keypoints1, keypoints2;

	detector.detect(img1, keypoints1);
	detector.detect(img2, keypoints2);

	Mat keypts1, keypts2;
	drawKeypoints(img1,     //输入图像  
		keypoints1,      //特征点矢量  
		keypts1,      //输出图像  
		Scalar::all(-1),      //绘制特征点的颜色,为随机  
							  //以特征点为中心画圆,圆的半径表示特征点的大小,直线表示特征点的方向  
		DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
	imshow("KeyPoints1", keypts1);
	waitKey(0);
	keypts1.release();

	//-- Step 2: Calculate descriptors (feature vectors)
	//SiftDescriptorExtractor extractor;
	SurfDescriptorExtractor extractor;

	Mat descriptors1, descriptors2;

	extractor.compute(img1, keypoints1, descriptors1);
	extractor.compute(img2, keypoints2, descriptors2);

	//-- Step 3: Matching descriptor vectors using FLANN matcher
	FlannBasedMatcher matcher;
	std::vector< DMatch > matches;
	matcher.match(descriptors1, descriptors2, matches);

	//提取部分匹配结果  
	std::nth_element(matches.begin(), matches.begin() + 14, matches.end());
	matches.erase(matches.begin() + 15, matches.end());

	//显示结果  
	//namedWindow("SIFTmatchResult");
	Mat resultImage;
	drawMatches(img1, keypoints1, img2, keypoints2, matches, resultImage, Scalar(255, 255, 255));
	imshow("SIFTmatchResult", resultImage);
	waitKey(0);
	//imwrite("output.jpg", resultImage);

	img1.release();
	img2.release();
	resultImage.release();
	descriptors1.release();
	descriptors2.release();
	return 0;
}


openCV 2.4.31

SIFT参考:SIFT算法详解

SURF参考:SURF算法解析

                     Opencv学习笔记(六)SURF学习笔记

FAST参考:OpenCV学习笔记(四十六)——FAST特征点检测features2D


                
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值