opencv3 SIFT

因为需要用到一些比较新的跟踪算法,这两天装了opencv3.1并配置了opencv_contrib,并使用了SIFT算法测试是否配置成功。
1.opencv3.1安装与配置
这里不多言,不熟悉的可以参考浅墨的博客:http://blog.csdn.net/poem_qianmo/article/details/19809337
2.opencv_contrib安装与配置
从opencv3以来,一些比较新的功能都挪到了“opencv_contrib”库里。配置这个库需要重新编译opencv,关于此部分可以参考教程:http://blog.csdn.net/linshuhe1/article/details/51221015
关于此教程需要补充两点:A,使用cmake编译的过程中经常会失败,因为国内网络问题ippicv_windows_20151201.zip 文件下载失败导致,可以直接从这里下载:http://download.csdn.net/detail/qjj2857/9495013 B.教程最后配置包含目录、库目录时没有提及添加环境变量,这里也是同样需要的。还有一切配置完成后别忘了重启电脑哟。
3.写个程序测试一下配置是否成功吧
opencv3.1中SIFT匹配是在opencv_contrib库中的,这里我们就用它来做一个简单的测试。
参考:
1. cv::xfeatures2d::SIFT Class Reference:http://docs.opencv.org/3.1.0/d5/d3c/classcv_1_1xfeatures2d_1_1SIFT.html#gsc.tab=0
2. OpenCV3.1 xfeatures2d::SIFT 使用:http://blog.csdn.net/lijiang1991/article/details/50855279
程序:

#include <iostream>
#include <opencv2/opencv.hpp>  //头文件
#include <opencv2/xfeatures2d.hpp>
using namespace cv;  //包含cv命名空间
using namespace std;

int main()
{
    //Create SIFT class pointer
    Ptr<Feature2D> f2d = xfeatures2d::SIFT::create();
    //读入图片
    Mat img_1 = imread("1.jpg");
    Mat img_2 = imread("2.jpg");
    //Detect the keypoints
    vector<KeyPoint> keypoints_1, keypoints_2;
    f2d->detect(img_1, keypoints_1);
    f2d->detect(img_2, keypoints_2);
    //Calculate descriptors (feature vectors)
    Mat descriptors_1, descriptors_2;
    f2d->compute(img_1, keypoints_1, descriptors_1);
    f2d->compute(img_2, keypoints_2, descriptors_2);    
    //Matching descriptor vector using BFMatcher
    BFMatcher matcher;
    vector<DMatch> matches;
    matcher.match(descriptors_1, descriptors_2, matches);
    //绘制匹配出的关键点
    Mat img_matches;
    drawMatches(img_1, keypoints_1, img_2, keypoints_2, matches, img_matches);
    imshow("【match图】", img_matches);
    //等待任意按键按下
    waitKey(0);
}

猜你喜欢

转载自blog.csdn.net/max2009verygood/article/details/79177571