使用Opencv2.4.9进行SIFT特征点提取和匹配

主要使用的类:FeatureDetector FeatureExtractor FeatureMatcher

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp> #include <opencv2/features2d/features2d.hpp> #include <opencv2/nonfree/nonfree.hpp> #include <iostream> using namespace cv; using namespace std; int main(){ cv::initModule_nonfree(); Mat img1_mat = imread("1.bmp",CV_LOAD_IMAGE_GRAYSCALE); Mat img2_mat = imread("2.bmp",CV_LOAD_IMAGE_GRAYSCALE); std::vector<KeyPoint> img1_keypoint,img2_keypoint;
/*特征点检测*/ // FeatureDetector *sift_detector = FeatureDetector::create("SIFT"); Ptr<FeatureDetector> sift_detector = FeatureDetector::create("SIFT"); sift_detector->detect(img1_mat,img1_keypoint); sift_detector->detect(img2_mat,img2_keypoint);
/*在图片上显示特征点*/ Mat img_kp_1,img_kp_2; drawKeypoints(img1_mat,img1_keypoint,img_kp_1,Scalar::all(-1),DrawMatchesFlags::DEFAULT); drawKeypoints(img2_mat,img2_keypoint,img_kp_2,Scalar::all(-1),DrawMatchesFlags::DEFAULT); imshow("sift_keypoint_image 1",img_kp_1); imshow("sift_keypoint_image 2",img_kp_2);
/*特征向量提取*/ Mat img1_descriptor,img2_descriptor; Ptr<DescriptorExtractor> sift_descriptor_extractor = DescriptorExtractor::create("SIFT"); sift_descriptor_extractor->compute(img1_mat,img1_keypoint,img1_descriptor); sift_descriptor_extractor->compute(img2_mat,img2_keypoint,img2_descriptor); /*特征点匹配*/ std::vector<DMatch> img_match; Ptr<DescriptorMatcher> bruteforce_matcher = DescriptorMatcher::create("BruteForce"); bruteforce_matcher->match(img1_descriptor,img2_descriptor,img_match); /*在图片上显示匹配结果*/ Mat match_img; drawMatches(img1_mat,img1_keypoint,img2_mat,img2_keypoint,accurate_match,match_img); imshow("match image",match_img); waitKey(0); system("pause"); return 0; }

 需要注意的地方:

如果完全按老版本的opencv的写法会出现下图中Access violation的异常,解决方法如下:

 

1.在新版本的opencv中,像SIFT,SURF等专利方法放在了单独的模块中,使用前需

  a) include <opencv2/nonfree/nonfree.hpp>

  b) 使用前添加语句 cv::initModule_nonfree()

  c) 在链接库中添加 opencv_nonfree2.4.9.lib,一般在配置opencv时已经添加

2.FeatureDetector 等是三个抽象基类,不能直接实例化,也不能直接使用 *,必须用Ptr<>的方式来使用

 Ptr<FeatureDetector> sift_detector = FeatureDetector::create("SIFT");

运行结果如下:

 

转载于:https://www.cnblogs.com/codyzh/p/3770198.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值