回覆列表
  • 1 # 淘寶客老妖

    #include <stdio.h>

    #include <iostream>

    #include "opencv2/core/core.hpp"

    #include "opencv2/highgui/highgui.hpp"

    #include<opencv2/legacy/legacy.hpp>

    #include "opencv2/nonfree/features2d.hpp" //SurfFeatureDetector實際在該標頭檔案中

    using namespace cv;

    using namespace std;

    int main(int argc, char** argv)

    {

    Mat src = imread("1.jpg", 1);

    Mat src2 = imread("2.jpg", 1);//旋轉後

    if (!src.data||!src2.data)

    {

    cout << " --(!) Error reading images " << endl;

    }

    //1--初始化SIFT檢測運算元

    int minHessian = 40;

    SiftFeatureDetector detector(minHessian);

    //2--使用SIFT運算元檢測特徵點

    vector<KeyPoint> kp1,kp2;

    detector.detect(src, kp1);

    detector.detect(src2, kp2);

    //--繪製特徵點

    Mat keypointImg, keypointImg2;

    drawKeypoints(src, kp1, keypointImg, Scalar::all(-1), DrawMatchesFlags::DEFAULT);

    imshow("SIFT kp1", keypointImg);

    cout << "keypoint1 number: " << kp1.size() << endl;

    drawKeypoints(src, kp2, keypointImg2, Scalar::all(-1), DrawMatchesFlags::DEFAULT);

    imshow("SIFT kp2", keypointImg2);

    cout << "keypoint2 number: " << kp2.size() << endl;

    //3--特徵向量提取

    SiftDescriptorExtractor extractor;

    Mat descriptor1, descriptor2;

    extractor.compute(src, kp1, descriptor1);

    extractor.compute(src2, kp2, descriptor2);

    //imshow("desc", descriptor1);

    //cout << endl << descriptor1 << endl;

    //4--特徵匹配

    BruteForceMatcher<L2<float>> matcher;

    vector<DMatch> matches;

    Mat matches;

    matcher.match(descriptor1, descriptor2, matches);

    //--繪製匹配

    drawMatches(src, kp1, src2, kp2, matches, matches);

    imshow("matches", matches);

    waitKey(0);

    return 0;

    }

  • 中秋節和大豐收的關聯?
  • 創業路上,你最怕什麼?