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);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章