使用函數detectAndCompute()檢測關鍵點并計算描述符
函數detectAndCompute()參數說明:
void detectAndCompute( InputArray image, //圖像InputArray mask, //掩模CV_OUT std::vector<KeyPoint>& keypoints,//輸出關鍵點的集合OutputArray descriptors,//計算描述符(descriptors[i]是為keypoints[i]的計算描述符)bool useProvidedKeypoints=false //使用提供的關鍵點);
match()從查詢集中查找每個描述符的最佳匹配。
參數說明:
void match( InputArray queryDescriptors, //查詢描述符集InputArray trainDescriptors, //訓練描述符集合CV_OUT std::vector<DMatch>& matches, //匹配InputArray mask=noArray() //指定輸入查詢和描述符的列表矩陣之間的允許匹配的掩碼) const;
FLANN特征匹配示例:
#include<opencv2/opencv.hpp>#include<opencv2/xfeatures2d.hpp>using namespace cv;using namespace cv::xfeatures2d;//FLANN對高維數據較快int main(){  Mat src1,src2;  src1 = imread("E:/image/image/card2.jpg");  src2 = imread("E:/image/image/cards.jpg");  if (src1.empty() || src2.empty())  {    printf("can ont load images..../n");    return -1;  }  imshow("image1", src1);  imshow("image2", src2);  int minHessian = 400;  //選擇SURF特征  Ptr<SURF>detector = SURF::create(minHessian);  std::vector<KeyPoint>keypoints1;  std::vector<KeyPoint>keypoints2;  Mat descriptor1, descriptor2;  //檢測關鍵點并計算描述符  detector->detectAndCompute(src1, Mat(), keypoints1, descriptor1);  detector->detectAndCompute(src2, Mat(), keypoints2, descriptor2);  //基于Flann的描述符匹配器  FlannBasedMatcher matcher;  std::vector<DMatch>matches;  //從查詢集中查找每個描述符的最佳匹配  matcher.match(descriptor1, descriptor2, matches);  double minDist = 1000;  double maxDist = 0;  for (int i = 0; i < descriptor1.rows; i++)  {    double dist = matches[i].distance;    printf("%f /n", dist);    if (dist > maxDist)    {      maxDist = dist;    }    if (dist < minDist)    {      minDist = dist;    }  }  //DMatch類用于匹配關鍵點描述符的  std::vector<DMatch>goodMatches;  for (int i = 0; i < descriptor1.rows; i++)  {    double dist = matches[i].distance;    if (dist < max(2.5*minDist, 0.02))    {      goodMatches.push_back(matches[i]);    }  }  Mat matchesImg;  drawMatches(src1, keypoints1, src2, keypoints2, goodMatches, matchesImg, Scalar::all(-1), Scalar::all(-1), std::vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);  imshow("output", matchesImg);  waitKey();  return 0;}


以上這篇opencv3/C++ FLANN特征匹配方式就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。
新聞熱點
疑難解答
圖片精選