特征检测子公用接口

OpenCV封装了一些特征检测子(关键点)算法,使得用户能够解决该问题时候方便使用各种算法。这章用来计算的描述子匹配被表达成一个高维空间的向量 vector.所有实现 vector 特征关键点检测子部分继承了 FeatureDetector 接口.

KeyPoint

class KeyPoint

Data structure for salient point detectors.

Point2f pt

coordinates of the keypoint

float size

diameter of the meaningful keypoint neighborhood

float angle

computed orientation of the keypoint (-1 if not applicable)

float response

the response by which the most strong keypoints have been selected. Can be used for further sorting or subsampling

int octave

octave (pyramid layer) from which the keypoint has been extracted

int class_id

object id that can be used to clustered keypoints by an object they belong to

KeyPoint::KeyPoint

The keypoint constructors

C++: KeyPoint::KeyPoint()
C++: KeyPoint::KeyPoint(Point2f _pt, float _size, float _angle=-1, float _response=0, int _octave=0, int _class_id=-1)
C++: KeyPoint::KeyPoint(float x, float y, float _size, float _angle=-1, float _response=0, int _octave=0, int _class_id=-1)
Python: cv2.KeyPoint(x, y, _size[, _angle[, _response[, _octave[, _class_id]]]]) → <KeyPoint object>
Parameters:
  • x – x-coordinate of the keypoint
  • y – y-coordinate of the keypoint
  • _pt – x & y coordinates of the keypoint
  • _size – keypoint diameter
  • _angle – keypoint orientation
  • _response – keypoint detector response on the keypoint (that is, strength of the keypoint)
  • _octave – pyramid octave in which the keypoint has been detected
  • _class_id – object id

FeatureDetector

class FeatureDetector

Abstract base class for 2D image feature detectors.

class CV_EXPORTS FeatureDetector
{
public:
    virtual ~FeatureDetector();

    void detect( const Mat& image, vector<KeyPoint>& keypoints,
                 const Mat& mask=Mat() ) const;

    void detect( const vector<Mat>& images,
                 vector<vector<KeyPoint> >& keypoints,
                 const vector<Mat>& masks=vector<Mat>() ) const;

    virtual void read(const FileNode&);
    virtual void write(FileStorage&) const;

    static Ptr<FeatureDetector> create( const string& detectorType );

protected:
...
};

FeatureDetector::detect

Detects keypoints in an image (first variant) or image set (second variant).

C++: void FeatureDetector::detect(const Mat& image, vector<KeyPoint>& keypoints, const Mat& mask=Mat() ) const
C++: void FeatureDetector::detect(const vector<Mat>& images, vector<vector<KeyPoint>>& keypoints, const vector<Mat>& masks=vector<Mat>() ) const
Parameters:
  • image – Image.
  • images – Image set.
  • keypoints – The detected keypoints. In the second variant of the method keypoints[i] is a set of keypoints detected in images[i] .
  • mask – Mask specifying where to look for keypoints (optional). It must be a 8-bit integer matrix with non-zero values in the region of interest.
  • masks – Masks for each input image specifying where to look for keypoints (optional). masks[i] is a mask for images[i].

FeatureDetector::read

从文件中读取特征检测子对象.

C++: void FeatureDetector::read(const FileNode& fn)
Parameters:
  • fn – File node from which the detector is read.

FeatureDetector::write

向文件中写入特征检测子对象.

C++: void FeatureDetector::write(FileStorage& fs) const
Parameters:
  • fs – File storage where the detector is written.

FeatureDetector::create

根据名字创建特征检测子.

C++: Ptr<FeatureDetector> FeatureDetector::create(const string& detectorType)
Parameters:
  • detectorType – Feature detector type.

The following detector types are supported:

Also a combined format is supported: feature detector adapter name ( "Grid"GridAdaptedFeatureDetector, "Pyramid"PyramidAdaptedFeatureDetector ) + feature detector name (see above), for example: "GridFAST", "PyramidSTAR" .

FastFeatureDetector

class FastFeatureDetector

用:ocv:func:FAST 方法封装的特征检测子的类.

class FastFeatureDetector : public FeatureDetector
{
public:
    FastFeatureDetector( int threshold=1, bool nonmaxSuppression=true );
    virtual void read( const FileNode& fn );
    virtual void write( FileStorage& fs ) const;
protected:
    ...
};

GoodFeaturesToTrackDetector

class GoodFeaturesToTrackDetector

goodFeaturesToTrack() 函数实现的特征检测子封装类.

class GoodFeaturesToTrackDetector : public FeatureDetector
{
public:
    class Params
    {
    public:
        Params( int maxCorners=1000, double qualityLevel=0.01,
                double minDistance=1., int blockSize=3,
                bool useHarrisDetector=false, double k=0.04 );
        void read( const FileNode& fn );
        void write( FileStorage& fs ) const;

        int maxCorners;
        double qualityLevel;
        double minDistance;
        int blockSize;
        bool useHarrisDetector;
        double k;
    };

    GoodFeaturesToTrackDetector( const GoodFeaturesToTrackDetector::Params& params=
                                            GoodFeaturesToTrackDetector::Params() );
    GoodFeaturesToTrackDetector( int maxCorners, double qualityLevel,
                                 double minDistance, int blockSize=3,
                                 bool useHarrisDetector=false, double k=0.04 );
    virtual void read( const FileNode& fn );
    virtual void write( FileStorage& fs ) const;
protected:
    ...
};

MserFeatureDetector

class MserFeatureDetector

MSER 函数实现的特征检测子封装类.

class MserFeatureDetector : public FeatureDetector
{
public:
    MserFeatureDetector( CvMSERParams params=cvMSERParams() );
    MserFeatureDetector( int delta, int minArea, int maxArea,
                         double maxVariation, double minDiversity,
                         int maxEvolution, double areaThreshold,
                         double minMargin, int edgeBlurSize );
    virtual void read( const FileNode& fn );
    virtual void write( FileStorage& fs ) const;
protected:
    ...
};

StarFeatureDetector

class StarFeatureDetector

StarDetector 函数实现的特征检测子封装类.:

class StarFeatureDetector : public FeatureDetector
{
public:
    StarFeatureDetector( int maxSize=16, int responseThreshold=30,
                         int lineThresholdProjected = 10,
                         int lineThresholdBinarized=8, int suppressNonmaxSize=5 );
    virtual void read( const FileNode& fn );
    virtual void write( FileStorage& fs ) const;
protected:
    ...
};

SiftFeatureDetector

class SiftFeatureDetector

SIFT 函数实现的特征检测子封装类.:

class SiftFeatureDetector : public FeatureDetector
{
public:
    SiftFeatureDetector(
        const SIFT::DetectorParams& detectorParams=SIFT::DetectorParams(),
        const SIFT::CommonParams& commonParams=SIFT::CommonParams() );
    SiftFeatureDetector( double threshold, double edgeThreshold,
                         int nOctaves=SIFT::CommonParams::DEFAULT_NOCTAVES,
                         int nOctaveLayers=SIFT::CommonParams::DEFAULT_NOCTAVE_LAYERS,
                         int firstOctave=SIFT::CommonParams::DEFAULT_FIRST_OCTAVE,
                         int angleMode=SIFT::CommonParams::FIRST_ANGLE );
    virtual void read( const FileNode& fn );
    virtual void write( FileStorage& fs ) const;
protected:
    ...
};

SurfFeatureDetector

class SurfFeatureDetector

SURF 函数实现的特征检测子封装类.

class SurfFeatureDetector : public FeatureDetector
{
public:
    SurfFeatureDetector( double hessianThreshold = 400., int octaves = 3,
                         int octaveLayers = 4 );
    virtual void read( const FileNode& fn );
    virtual void write( FileStorage& fs ) const;
protected:
    ...
};

OrbFeatureDetector

class OrbFeatureDetector

ORB 函数实现的特征检测子封装类.

class OrbFeatureDetector : public FeatureDetector
{
public:
    OrbFeatureDetector( size_t n_features );
    virtual void read( const FileNode& fn );
    virtual void write( FileStorage& fs ) const;
protected:
    ...
};

SimpleBlobDetector

class SimpleBlobDetector

从图像中提取blobs的类.

class SimpleBlobDetector : public FeatureDetector
{
public:
struct Params
{
    Params();
    float thresholdStep;
    float minThreshold;
    float maxThreshold;
    size_t minRepeatability;
    float minDistBetweenBlobs;

    bool filterByColor;
    uchar blobColor;

    bool filterByArea;
    float minArea, maxArea;

    bool filterByCircularity;
    float minCircularity, maxCircularity;

    bool filterByInertia;
    float minInertiaRatio, maxInertiaRatio;

    bool filterByConvexity;
    float minConvexity, maxConvexity;
};

SimpleBlobDetector(const SimpleBlobDetector::Params &parameters = SimpleBlobDetector::Params());

protected:
    ...
};

The class implements a simple algorithm for extracting blobs from an image:

  1. Convert the source image to binary images by applying thresholding with several thresholds from minThreshold (inclusive) to maxThreshold (exclusive) with distance thresholdStep between neighboring thresholds.
  2. Extract connected components from every binary image by findContours() and calculate their centers.
  3. Group centers from several binary images by their coordinates. Close centers form one group that corresponds to one blob, which is controlled by the minDistBetweenBlobs parameter.
  4. From the groups, estimate final centers of blobs and their radiuses and return as locations and sizes of keypoints.

This class performs several filtrations of returned blobs. You should set filterBy* to true/false to turn on/off corresponding filtration. Available filtrations:

  • By color. This filter compares the intensity of a binary image at the center of a blob to blobColor. If they differ, the blob is filtered out. Use blobColor = 0 to extract dark blobs and blobColor = 255 to extract light blobs.
  • By area. Extracted blobs have an area between minArea (inclusive) and maxArea (exclusive).
  • By circularity. Extracted blobs have circularity (\frac{4*\pi*Area}{perimeter * perimeter}) between minCircularity (inclusive) and maxCircularity (exclusive).
  • By ratio of the minimum inertia to maximum inertia. Extracted blobs have this ratio between minInertiaRatio (inclusive) and maxInertiaRatio (exclusive).
  • By convexity. Extracted blobs have convexity (area / area of blob convex hull) between minConvexity (inclusive) and maxConvexity (exclusive).

默认参数可以调节来提取深色圆形的blobs.

GridAdaptedFeatureDetector

class GridAdaptedFeatureDetector

调整检测子在源图像划分为grid,在每个cell检测点.

class GridAdaptedFeatureDetector : public FeatureDetector
{
public:
    /*
     * detector            Detector that will be adapted.
     * maxTotalKeypoints   Maximum count of keypoints detected on the image.
     *                     Only the strongest keypoints will be kept.
     * gridRows            Grid row count.
     * gridCols            Grid column count.
     */
    GridAdaptedFeatureDetector( const Ptr<FeatureDetector>& detector,
                                int maxTotalKeypoints, int gridRows=4,
                                int gridCols=4 );
    virtual void read( const FileNode& fn );
    virtual void write( FileStorage& fs ) const;
protected:
    ...
};

PyramidAdaptedFeatureDetector

class PyramidAdaptedFeatureDetector

调整一个检测子来在多层高斯金字塔上检测的类. 注意这个类的输出结果没有归一化.

class PyramidAdaptedFeatureDetector : public FeatureDetector
{
public:
    PyramidAdaptedFeatureDetector( const Ptr<FeatureDetector>& detector,
                                   int levels=2 );
    virtual void read( const FileNode& fn );
    virtual void write( FileStorage& fs ) const;
protected:
    ...
};

DynamicAdaptedFeatureDetector

class DynamicAdaptedFeatureDetector

直到期待的值被找到,调整检测子迭代式检测特征.

class DynamicAdaptedFeatureDetector: public FeatureDetector
{
public:
    DynamicAdaptedFeatureDetector( const Ptr<AdjusterAdapter>& adjuster,
        int min_features=400, int max_features=500, int max_iters=5 );
    ...
};

If the detector is persisted, it “remembers” the parameters used for the last detection. In this case, the detector may be used for consistent numbers of keypoints in a set of temporally related images, such as video streams or panorama series.

DynamicAdaptedFeatureDetector uses another detector, such as FAST or SURF, to do the dirty work, with the help of AdjusterAdapter . If the detected number of features is not large enough, AdjusterAdapter adjusts the detection parameters so that the next detection results in a bigger or smaller number of features. This is repeated until either the number of desired features are found or the parameters are maxed out.

Adapters can be easily implemented for any detector via the AdjusterAdapter interface.

Beware that this is not thread-safe since the adjustment of parameters requires modification of the feature detector class instance.

创建 DynamicAdaptedFeatureDetector 的例子 :

//sample usage:
//will create a detector that attempts to find
//100 - 110 FAST Keypoints, and will at most run
//FAST feature detection 10 times until that
//number of keypoints are found
Ptr<FeatureDetector> detector(new DynamicAdaptedFeatureDetector (100, 110, 10,
                              new FastAdjuster(20,true)));

DynamicAdaptedFeatureDetector::DynamicAdaptedFeatureDetector

构造函数

C++: DynamicAdaptedFeatureDetector::DynamicAdaptedFeatureDetector(const Ptr<AdjusterAdapter>& adjuster, int min_features, int max_features, int max_iters)
Parameters:
  • adjusterAdjusterAdapter that detects features and adjusts parameters.
  • min_features – Minimum desired number of features.
  • max_features – Maximum desired number of features.
  • max_iters – Maximum number of times to try adjusting the feature detector parameters. For FastAdjuster , this number can be high, but with Star or Surf many iterations can be time-comsuming. At each iteration the detector is rerun.

AdjusterAdapter

class AdjusterAdapter

这个类提供了调整特征检测子参数的接口.通过 DynamicAdaptedFeatureDetector 实现接口. 这个接口是 FeatureDetector 的一个封装。这个封装允许了在特征检测后调整参数.

class AdjusterAdapter: public FeatureDetector
{
public:
   virtual ~AdjusterAdapter() {}
   virtual void tooFew(int min, int n_detected) = 0;
   virtual void tooMany(int max, int n_detected) = 0;
   virtual bool good() const = 0;
   virtual Ptr<AdjusterAdapter> clone() const = 0;
   static Ptr<AdjusterAdapter> create( const string& detectorType );
};

See FastAdjuster, StarAdjuster, and SurfAdjuster for concrete implementations.

AdjusterAdapter::tooFew

调整检测子参数检测更多的特征.

C++: void AdjusterAdapter::tooFew(int min, int n_detected)
Parameters:
  • min – Minimum desired number of features.
  • n_detected – Number of features detected during the latest run.

Example:

void FastAdjuster::tooFew(int min, int n_detected)
{
        thresh_--;
}

AdjusterAdapter::tooMany

调整检测子参数检测更少的特征.

C++: void AdjusterAdapter::tooMany(int max, int n_detected)
Parameters:
  • max – Maximum desired number of features.
  • n_detected – Number of features detected during the latest run.

Example:

void FastAdjuster::tooMany(int min, int n_detected)
{
        thresh_++;
}

AdjusterAdapter::good

如果检测子参数不能被调整返回false.

C++: bool AdjusterAdapter::good() const

Example:

bool FastAdjuster::good() const
{
        return (thresh_ > 1) && (thresh_ < 200);
}

AdjusterAdapter::create

通过名字创建调整接合器.

C++: Ptr<AdjusterAdapter> AdjusterAdapter::create(const string& detectorType)

Creates an adjuster adapter by name detectorType. The detector name is the same as in FeatureDetector::create(), but now supports "FAST", "STAR", and "SURF" only.

FastAdjuster

class FastAdjuster

AdjusterAdapter for FastFeatureDetector. This class decreases or increases the threshold value by 1.

class FastAdjuster FastAdjuster: public AdjusterAdapter
{
public:
        FastAdjuster(int init_thresh = 20, bool nonmax = true);
        ...
};

StarAdjuster

class StarAdjuster

AdjusterAdapter for StarFeatureDetector. This class adjusts the responseThreshhold of StarFeatureDetector.

class StarAdjuster: public AdjusterAdapter
{
        StarAdjuster(double initial_thresh = 30.0);
        ...
};

SurfAdjuster

class SurfAdjuster

AdjusterAdapter for SurfFeatureDetector. This class adjusts the hessianThreshold of SurfFeatureDetector.

class SurfAdjuster: public SurfAdjuster
{
        SurfAdjuster();
        ...
};