opencv 特征点提取、匹配(一)

opencv 特征点提取、匹配(一)

opencv中特征点提取和匹配步骤:
提取特征点
生成特征点的描述子
特征点匹配

opencv对应类:
图像特征点的提取 — FeatureDetector
特征点描述子生成 – DescriptorExtractor
特征点的匹配 – DescriptorMatcher
(可从这三个基类派生出了不同的类来实现不同的特征提取算法、描述及匹配)

特征提取:FeatureDetector,实现二维图像特征的提取

/*
 * Abstract base class for 2D image feature detectors.
 */
class CV_EXPORTS_W FeatureDetector : public virtual Algorithm //派生于Algorithm算法类
/*
{
public:
    virtual ~FeatureDetector();

    /*
     * Detect keypoints in an image.
     * image        The image.
     * keypoints    The detected keypoints.
     * mask         Mask specifying where to look for keypoints (optional). Must be a char
     *              matrix with non-zero values in the region of interest.
     */
    CV_WRAP void detect( const Mat& image, CV_OUT vector& keypoints, const Mat& mask=Mat() ) const;

    /*
     * Detect keypoints in an image set.
     * images       Image collection.
     * keypoints    Collection of keypoints detected in an input images. keypoints[i] is a set of keypoints detected in an images[i].
     * masks        Masks for image set. masks[i] is a mask for images[i].
     */
    void detect( const vector& images, vector<vector >& keypoints, const vector& masks=vector() ) const;

    ...
    // Create feature detector by detector name.
    CV_WRAP static Ptr create( const string& detectorType ); //根据名字来创建具体的特征检测方法,如“ORB”--OrbFeatureDetector,“FAST” -- FastFeatureDetector
    ...
};

生成特征点的描述子: DescriptorExtractor
关键点的特征描述子被表达成密集的、固定维数的向量。特征描述子的集合被表达成一个Mat:
其中每一行是一个关键特征点的描述子,Mat矩阵的行数代表提取的特征点个数,列数代表特征点描述子的维数

/*
 * Abstract base class for computing descriptors for image keypoints.
 */
class CV_EXPORTS_W DescriptorExtractor : public virtual Algorithm
{
public:
    virtual ~DescriptorExtractor();

    /*
     * Compute the descriptors for a set of keypoints in an image.
     * image        The image.
     * keypoints    The input keypoints. Keypoints for which a descriptor cannot be computed are removed.
     * descriptors  Copmputed descriptors. Row i is the descriptor for keypoint i.
     */
    CV_WRAP void compute( const Mat& image, CV_OUT CV_IN_OUT vector& keypoints, CV_OUT Mat& descriptors ) const;

    /*
     * Compute the descriptors for a keypoints collection detected in image collection.
     * images       Image collection.
     * keypoints    Input keypoints collection. keypoints[i] is keypoints detected in images[i].
     *              Keypoints for which a descriptor cannot be computed are removed.
     * descriptors  Descriptor collection. descriptors[i] are descriptors computed for set keypoints[i].
     */
    void compute( const vector& images, vector<vector >& keypoints, vector& descriptors ) const;

    ...
    CV_WRAP static Ptr create( const string& descriptorExtractorType );
    //根据名字来创建具体的特征检测方法,如“ORB”--OrbDescriptorExtractor,“FAST” -- FastDescriptorExtractor
    ...
};

特征点匹配: DecriptorMatcher
是进行特征关键点描述子匹配的基类,主要用来匹配两个图像之间的特征描述子,
或者一个图像和一个图像集的特征描述子。主要包括两种匹配方法:
BFMatcher和FlannBasedMatcher

/*
 * Abstract base class for matching two sets of descriptors.
 */
class CV_EXPORTS_W DescriptorMatcher : public Algorithm
{
public:
    virtual ~DescriptorMatcher();

    ...
    /*
     * Group of methods to match descriptors from image pair.
     * Method train() is run in this methods.
     */
    // Find one best match for each query descriptor (if mask is empty).
    CV_WRAP void match( const Mat& queryDescriptors, const Mat& trainDescriptors,
                CV_OUT vector& matches, const Mat& mask=Mat() ) const;
    // Find k best matches for each query descriptor (in increasing order of distances).
    // compactResult is used when mask is not empty. If compactResult is false matches
    // vector will have the same size as queryDescriptors rows. If compactResult is true
    // matches vector will not contain matches for fully masked out query descriptors.
    CV_WRAP void knnMatch( const Mat& queryDescriptors, const Mat& trainDescriptors,
                   CV_OUT vector<vector >& matches, int k,
                   const Mat& mask=Mat(), bool compactResult=false ) const;
    // Find best matches for each query descriptor which have distance less than
    // maxDistance (in increasing order of distances).
    void radiusMatch( const Mat& queryDescriptors, const Mat& trainDescriptors,
                      vector<vector >& matches, float maxDistance,
                      const Mat& mask=Mat(), bool compactResult=false ) const;
    ...
    CV_WRAP static Ptr create( const string& descriptorMatcherType );
    ...
}

DMatch结构体封装了匹配的特征描述子的一些特性:
特征描述子的索引、
训练图像的索引、
特征描述子之间的距离等

//Struct for matching: query descriptor index, train descriptor index, train image index and distance between descriptors.
struct CV_EXPORTS_W_SIMPLE DMatch
{
    ...
    CV_PROP_RW int queryIdx; // query descriptor index
    CV_PROP_RW int trainIdx; // train descriptor index
    CV_PROP_RW int imgIdx;   // train image index

    CV_PROP_RW float distance;
    ...
};

class CV_EXPORTS_W BFMatcher : public DescriptorMatcher {
public:   
    CV_WRAP BFMatcher( int normType=NORM_L2, bool crossCheck=false );
    //normType:
    //NORM_L1, NORM_L2, NORM_HAMMING, NORM_HAMMING2.
    //SIFT、SURF:一般用NROM_L1和NORM_L2;
    //ORB、BRISK、BRIEF:一般用NORM_HAMMING;
    //NORM_HAMMING2用于ORB且ORB的构造函数的参数WTA = 3或者4时。
    //crossCheck参数:
    //为false时寻找k个最邻近的匹配点
    //为true时寻找最好的匹配点对
    ...
}
template
class CV_EXPORTS BruteForceMatcher : public BFMatcher // 
{
public:
    BruteForceMatcher( Distance d = Distance() ) : BFMatcher(Distance::normType, false) {(void)d;}
    virtual ~BruteForceMatcher() {}
};

//实例化一个匹配器: 
BruteForceMatcherfloat> > matcher;  
class CV_EXPORTS_W FlannBasedMatcher : public DescriptorMatcher{ //最邻近算法查找匹配点
    //Flann算法--最近邻近似匹配,找到一个相对好的匹配而不是最佳匹配的时候使用FlannBasedMatcher。
    //可通过调整FlannBasedMatcher的参数来提高匹配的精度或者算法速度,但相应地算法速度或者算法精度会受到影响
}

/******************************************************************/

//Abstract base class for simultaneous 2D feature detection descriptor extraction.
class CV_EXPORTS_W Feature2D : public FeatureDetector, public DescriptorExtractor
{
    ...
    //Feature2D派生于FeatureDetector类和DescriptorExtractor类;
}

//ORB implementation --- FeatureS2d.hpp中有此类的声明,如下:
class CV_EXPORTS_W ORB : public Feature2D
{
public:
    // the size of the signature in bytes
    enum { kBytes = 32, HARRIS_SCORE=0, FAST_SCORE=1 };

    CV_WRAP explicit ORB(int nfeatures = 500, float scaleFactor = 1.2f, int nlevels = 8, int edgeThreshold = 31,
        int firstLevel = 0, int WTA_K=2, int scoreType=ORB::HARRIS_SCORE, int patchSize=31 );

    // returns the descriptor size in bytes
    int descriptorSize() const;
    // returns the descriptor type
    int descriptorType() const;

    // Compute the ORB features and descriptors on an image
    void operator()(InputArray image, InputArray mask, vector& keypoints) const;

    // Compute the ORB features and descriptors on an image
    void operator()( InputArray image, InputArray mask, vector& keypoints,
                     OutputArray descriptors, bool useProvidedKeypoints=false ) const;

    AlgorithmInfo* info() const;

protected:
    void computeImpl( const Mat& image, vector& keypoints, Mat& descriptors ) const;
    void detectImpl( const Mat& image, vector& keypoints, const Mat& mask=Mat() ) const;
    ...
};

typedef ORB OrbFeatureDetector;
typedef ORB OrbDescriptorExtractor;

ORB派生于feature2D类的类的对象既可以调用FeatureDetector的成员函数实现特征的提取,又可以调用DescroptorExtractor的成员函数来生成特征描述子

SIFT和SURF(属付费使用部分)

添加头文件:
<opencv2/nonfree/feature2d.hpp>
<opencv2/nonfree/nonfree.hpp>,
并在程序的开始处添加:
initModule_nonfree(); //

你可能感兴趣的:(opencv)