OpenCV3中的SURF特徵點的尋找和匹配

來源:互聯網
上載者:User

不多說什麼了,直接上代碼吧:

#include <iostream>#include <stdio.h>#include "opencv2/core.hpp"#include "opencv2/core/utility.hpp"#include "opencv2/core/ocl.hpp"#include "opencv2/imgcodecs.hpp"#include "opencv2/highgui.hpp"#include "opencv2/features2d.hpp"#include "opencv2/calib3d.hpp"#include "opencv2/imgproc.hpp"#include"opencv2/flann.hpp"#include"opencv2/xfeatures2d.hpp"#include"opencv2/ml.hpp"using namespace cv;using namespace std;using namespace cv::xfeatures2d;using namespace cv::ml;int main(){    Mat a = imread("box.png", IMREAD_GRAYSCALE);    //讀取灰階映像    Mat b = imread("box_in_scene.png", IMREAD_GRAYSCALE);    Ptr<SURF> surf;      //建立方式和2中的不一樣    surf = SURF::create(800);    BFMatcher matcher;    Mat c, d;    vector<KeyPoint>key1, key2;    vector<DMatch> matches;    surf->detectAndCompute(a, Mat(), key1, c);    surf->detectAndCompute(b, Mat(), key2, d);    matcher.match(c, d, matches);       //匹配    sort(matches.begin(), matches.end());  //篩選匹配點    vector< DMatch > good_matches;                  int ptsPairs = std::min(50, (int)(matches.size() * 0.15));    cout << ptsPairs << endl;    for (int i = 0; i < ptsPairs; i++)    {        good_matches.push_back(matches[i]);    }    Mat outimg;    drawMatches(a, key1, b, key2, good_matches, outimg, Scalar::all(-1), Scalar::all(-1),vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);  //繪製匹配點    std::vector<Point2f> obj;    std::vector<Point2f> scene;    for (size_t i = 0; i < good_matches.size(); i++)    {        obj.push_back(key1[good_matches[i].queryIdx].pt);        scene.push_back(key2[good_matches[i].trainIdx].pt);    }    std::vector<Point2f> obj_corners(4);    obj_corners[0] = Point(0, 0);    obj_corners[1] = Point(a.cols, 0);    obj_corners[2] = Point(a.cols, a.rows);    obj_corners[3] = Point(0, a.rows);    std::vector<Point2f> scene_corners(4);    Mat H = findHomography(obj, scene, RANSAC);      //尋找匹配的映像    perspectiveTransform(obj_corners, scene_corners, H);    line(outimg,scene_corners[0] + Point2f((float)a.cols, 0), scene_corners[1] + Point2f((float)a.cols, 0),Scalar(0, 255, 0), 2, LINE_AA);       //繪製    line(outimg,scene_corners[1] + Point2f((float)a.cols, 0), scene_corners[2] + Point2f((float)a.cols, 0),Scalar(0, 255, 0), 2, LINE_AA);    line(outimg,scene_corners[2] + Point2f((float)a.cols, 0), scene_corners[3] + Point2f((float)a.cols, 0),Scalar(0, 255, 0), 2, LINE_AA);    line(outimg,scene_corners[3] + Point2f((float)a.cols, 0), scene_corners[0] + Point2f((float)a.cols, 0),Scalar(0, 255, 0), 2, LINE_AA);    imshow("aaaa",outimg);    cvWaitKey(0);}

運行圖:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.