OpenCV入門,opencv

來源:互聯網
上載者:User

OpenCV入門,opencv
OpenCV入門 - 關鍵點描述子匹配Flann-based


   和前面利用暴力法找距離最近的descriptor,Flann-based matcher使用快速近似最近鄰搜尋演算法,在匹配前可以利用圖片訓練該matcher,從而加快檢測速度(TODO).
   What is it Flann? FLANN is a library for performing fast approximate nearest neighbor searches in high dimensional spaces. It contains a collection of algorithms we found to work best for nearest neighbor search and a system for automatically choosing the best algorithm and optimum parameters depending on the dataset.

#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <opencv2/nonfree/features2d.hpp> //#include <iostream>using namespace cv;using namespace std;int main(int argc, const char *argv[]){    Mat car1 = imread("car1.jpeg", 0);// load as grayscale    Mat car2 = imread("car2.jpeg", 0);    //cv::resize(car1,car1,car2.size());    SiftFeatureDetector detector;    vector<KeyPoint> keypoints1, keypoints2;    detector.detect(car1, keypoints1);    detector.detect(car2, keypoints2);    cout << "# keypoints of car1 :" << keypoints1.size() << endl;    cout << "# keypoints of car2 :" << keypoints2.size() << endl;       Mat descriptors1,descriptors2;    Ptr<DescriptorExtractor> extractor = DescriptorExtractor::create("SIFT");    extractor->compute(car1,keypoints1,descriptors1);    extractor->compute(car2,keypoints2,descriptors2);        FlannBasedMatcher matcher;    vector<DMatch> matches;    matcher.match(descriptors1, descriptors2, matches);    cout << "# matches : " << matches.size() << endl;    //calc max and min keypoints distance    double maxdist=0, mindist=100;    for(int i=0;i<descriptors1.rows;i++){        double dist = matches[i].distance;        //cout << "test" << dist << endl;        if(dist<mindist)            mindist = dist;        if(dist > maxdist)            maxdist = dist;    }    cout << "Max distance:" << maxdist << "Min distance:" << mindist << endl;    // Get good matches (? distance is lesss than 2*min)    vector<DMatch> goods;    for(int i=0;i<descriptors1.rows;i++){        if(matches[i].distance < 2*mindist)            goods.push_back(matches[i]);    }    cout << "# good matches : " << goods.size() << endl;    // show it on an image    Mat output;    drawMatches(car1, keypoints1, car2, keypoints2,             goods, output, Scalar::all(-1), Scalar::all(-1),            vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);    imshow("good matches result",output);    waitKey(0);    return 0;}


運行結果,可看到經過濾後匹配數目大大減少。


參考:
1. Flann首頁 http://www.cs.ubc.ca/research/flann/
2. Fast Approximate Nearest Neighbor Search





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.