OpenCV Tutorials —— Feature Matching with FLANN

Extractors of keypoint descriptors in OpenCV have wrappers with a common interface that enables you to easily switch between different algorithms solving the same problem.

 

DescriptorExtractor::compute

Computes the descriptors for a set of keypoints detected in an image (first variant) or image set (second variant). 计算描述子

 

DescriptorExtractor::create

The current implementation supports the following types of a descriptor extractor:

A combined format is also supported: descriptor extractor adapter name ( "Opponent"OpponentColorDescriptorExtractor ) + descriptor extractor name (see above), for example: "OpponentSIFT" .

 

OpponentColorDescriptorExtractor

Class adapting a descriptor extractor to compute descriptors in the Opponent Color Space (refer to Van de Sande et al., CGIV 2008 Color Descriptors for Object Category Recognition). Input RGB image is transformed in the Opponent Color Space.

Then, an unadapted descriptor extractor (set in the constructor) computes descriptors on each of three channels and concatenates them into a single color descriptor.

 

检测子 —— 描述子 —— 匹配子

Code

#include "stdafx.h"

#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/features2d.hpp"

using namespace cv;

void readme();

/**
 * @function main
 * @brief Main function
 */
int main( int argc, char** argv )
{
  if( argc != 3 )
  { readme(); return -1; }

  Mat img_1 = imread( argv[1], CV_LOAD_IMAGE_GRAYSCALE );
  Mat img_2 = imread( argv[2], CV_LOAD_IMAGE_GRAYSCALE );

  if( !img_1.data || !img_2.data )
  { std::cout<< " --(!) Error reading images " << std::endl; return -1; }

  //-- Step 1: Detect the keypoints using SURF Detector
  int minHessian = 400;

  SurfFeatureDetector detector( minHessian );

  std::vector<KeyPoint> keypoints_1, keypoints_2;

  detector.detect( img_1, keypoints_1 );
  detector.detect( img_2, keypoints_2 );

  //-- Step 2: Calculate descriptors (feature vectors)
  SurfDescriptorExtractor extractor;	// 描述子

  Mat descriptors_1, descriptors_2;

  extractor.compute( img_1, keypoints_1, descriptors_1 );
  extractor.compute( img_2, keypoints_2, descriptors_2 );

  //-- Step 3: Matching descriptor vectors using FLANN matcher
  FlannBasedMatcher matcher;
  std::vector< DMatch > matches;
  matcher.match( descriptors_1, descriptors_2, matches );

  double max_dist = 0; double min_dist = 100;

  //-- Quick calculation of max and min distances between keypoints
  for( int i = 0; i < descriptors_1.rows; i++ )
  { double dist = matches[i].distance;
    if( dist < min_dist ) min_dist = dist;
    if( dist > max_dist ) max_dist = dist;
  }

  printf("-- Max dist : %f \n", max_dist );
  printf("-- Min dist : %f \n", min_dist );

  //-- Draw only "good" matches (i.e. whose distance is less than 2*min_dist,
  //-- or a small arbitary value ( 0.02 ) in the event that min_dist is very
  //-- small)
  //-- PS.- radiusMatch can also be used here.
  std::vector< DMatch > good_matches;

  for( int i = 0; i < descriptors_1.rows; i++ )
  { if( matches[i].distance <= max(2*min_dist, 0.02) )
    { good_matches.push_back( matches[i]); }
  }

  //-- Draw only "good" matches
  Mat img_matches;
  drawMatches( img_1, keypoints_1, img_2, keypoints_2,
               good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
               vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );

  //-- Show detected matches
  imshow( "Good Matches", img_matches );

  for( int i = 0; i < (int)good_matches.size(); i++ )
  { printf( "-- Good Match [%d] Keypoint 1: %d  -- Keypoint 2: %d  \n", i, good_matches[i].queryIdx, good_matches[i].trainIdx ); }

  waitKey(0);

  return 0;
}

/**
 * @function readme
 */
void readme()
{ std::cout << " Usage: ./SURF_FlannMatcher <img1> <img2>" << std::endl; }
时间: 2024-10-10 21:53:53

OpenCV Tutorials —— Feature Matching with FLANN的相关文章

OpenCV Tutorials &mdash;&mdash; Template Matching

模板匹配 从源图像中发掘目标图像 将目标图像块逐像素滑动,然后度量此区域的源图像块和目标图像块的匹配程度 匹配都最高的像素位置作为最终定位   void matchTemplate(InputArray image, InputArray templ, OutputArray result, int method) 度量手段 -- 相关性 method=CV_TM_SQDIFF method=CV_TM_SQDIFF_NORMED method=CV_TM_CCORR method=CV_TM_

OpenCV Tutorials &mdash;&mdash; Feature Detection

FeatureDetector::detect Detects keypoints in an image (first variant) or image set (second variant). FeatureDetector::create The following detector types are supported: "FAST" – FastFeatureDetector "STAR" – StarFeatureDetector "SI

【计算机视觉】OpenCV的最近邻开源库FLANN

FLANN介绍 FLANN库全称是Fast Library for Approximate Nearest Neighbors,它是目前最完整的(近似)最近邻开源库.不但实现了一系列查找算法,还包含了一种自动选取最快算法的机制. flann::Index_类 该类模板是最近邻索引类,该类用于抽象不同类型的最近邻搜索的索引. 以下是flann::Index_类的声明: template <typename T> class #ifndef _MSC_VER FLANN_DEPRECATED #e

学习opencv tutorials

1.opencv里头动态库和静态库的区别 lib是动态库,staticlib是静态库. 这是opencv tutorials中对动态库和静态库的说明.动态库是在runtime时候才load的库文件.而静态库文件会在你build的时候build-in inside your exe file.优点是可以避免误删,缺点是应用程序变大,加载时间也会变长. 2.  Visual Studio中solution和project的关系 在VS中,一个solution中可以包含多个project. 3.  两

[OpenCV] Feature Matching

得到了杂乱无章的特征点后,要筛选出好的特征点,也就是good matches. BruteForceMatcher FlannBasedMatcher 两者的区别:http://yangshen998.iteye.com/blog/1311575 flann的含义:http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/features2d/feature_flann_matcher/feature_flann_matcher.ht

OpenCV Tutorials &mdash;&mdash; Features2D + Homography to find a known object

通过特征检测和单应性匹配来发掘已知物体 ~~ Use the function findHomography to find the transform between matched keypoints. Use the function perspectiveTransform to map the points.   检测 + 描述 + 匹配 ~ ~ 得到匹配的keypoints 之后可计算出形变矩阵 findHomography Finds a perspective transform

OpenCV Tutorials &mdash;&mdash; Histogram Calculation

Let's identify some parts of the histogram: 1,dims: The number of parameters you want to collect data of. 2,bins: It is the number of subdivisions in each dim. 3,range: The limits for the values to be measured. void calcHist(const Mat* images, int ni

OpenCV Tutorials &mdash;&mdash; Camera calibration With OpenCV

获取摄像机参数是为了来处理图像失真或者实现图像度量 ~~ Unfortunately, this cheapness comes with its price: significant distortion. Luckily, these are constants and with a calibration and some remapping we can correct this. Furthermore, with calibration you may also determine

OpenCV K-d树实现之FLANN (Fast Library for Approximate Nearest Neighbors) 算法实现及解析

k-d树搜索最近点,在opencv中使用FLANN算法,其包含: 1:建树   2.查询 程序见下: #include "kdtree.h" #include <iostream> #include <iomanip> #include "cv.h" #include "highgui.h" #include <fstream> #include "cv.h" #include "