\section{Feature Detection}

\ifCPy

\ifPy
\cvclass{CvSURFPoint}
A SURF keypoint, represented as a tuple \texttt{((x, y), laplacian, size, dir, hessian)}.

\begin{description}
\cvarg{x}{x-coordinate of the feature within the image}
\cvarg{y}{y-coordinate of the feature within the image}
\cvarg{laplacian}{-1, 0 or +1. sign of the laplacian at the point.  Can be used to speedup feature comparison since features with laplacians of different signs can not match}
\cvarg{size}{size of the feature}
\cvarg{dir}{orientation of the feature: 0..360 degrees}
\cvarg{hessian}{value of the hessian (can be used to approximately estimate the feature strengths; see also params.hessianThreshold)}
\end{description}
\fi

\cvCPyFunc{ExtractSURF}
Extracts Speeded Up Robust Features from an image.

\cvdefC{
void cvExtractSURF( \par const CvArr* image,\par const CvArr* mask,\par CvSeq** keypoints,\par CvSeq** descriptors,\par CvMemStorage* storage,\par CvSURFParams params );
}
\cvdefPy{ExtractSURF(image,mask,storage,params)-> (keypoints,descriptors)}

\begin{description}
\cvarg{image}{The input 8-bit grayscale image}
\cvarg{mask}{The optional input 8-bit mask. The features are only found in the areas that contain more than 50\% of non-zero mask pixels}
\ifC
\cvarg{keypoints}{The output parameter; double pointer to the sequence of keypoints. The sequence of CvSURFPoint structures is as follows:}
\begin{lstlisting}
 typedef struct CvSURFPoint
 {
    CvPoint2D32f pt; // position of the feature within the image
    int laplacian;   // -1, 0 or +1. sign of the laplacian at the point.
                     // can be used to speedup feature comparison
                     // (normally features with laplacians of different 
             // signs can not match)
    int size;        // size of the feature
    float dir;       // orientation of the feature: 0..360 degrees
    float hessian;   // value of the hessian (can be used to 
             // approximately estimate the feature strengths;
                     // see also params.hessianThreshold)
 }
 CvSURFPoint;
\end{lstlisting}
\cvarg{descriptors}{The optional output parameter; double pointer to the sequence of descriptors. Depending on the params.extended value, each element of the sequence will be either a 64-element or a 128-element floating-point (\texttt{CV\_32F}) vector. If the parameter is NULL, the descriptors are not computed}
\else
\cvarg{keypoints}{sequence of keypoints.}
\cvarg{descriptors}{sequence of descriptors.  Each SURF descriptor is a list of floats, of length 64 or 128.}
\fi
\cvarg{storage}{Memory storage where keypoints and descriptors will be stored}
\ifC
\cvarg{params}{Various algorithm parameters put to the structure CvSURFParams:}
\begin{lstlisting}
 typedef struct CvSURFParams
 {
    int extended; // 0 means basic descriptors (64 elements each),
                  // 1 means extended descriptors (128 elements each)
    double hessianThreshold; // only features with keypoint.hessian 
          // larger than that are extracted.
                  // good default value is ~300-500 (can depend on the 
          // average local contrast and sharpness of the image).
                  // user can further filter out some features based on 
          // their hessian values and other characteristics.
    int nOctaves; // the number of octaves to be used for extraction.
                  // With each next octave the feature size is doubled 
          // (3 by default)
    int nOctaveLayers; // The number of layers within each octave 
          // (4 by default)
 }
 CvSURFParams;

 CvSURFParams cvSURFParams(double hessianThreshold, int extended=0); 
          // returns default parameters
\end{lstlisting}
\else
\cvarg{params}{Various algorithm parameters in a tuple \texttt{(extended, hessianThreshold, nOctaves, nOctaveLayers)}:
\begin{description}
\cvarg{extended}{0 means basic descriptors (64 elements each), 1 means extended descriptors (128 elements each)}
\cvarg{hessianThreshold}{only features with hessian larger than that are extracted.  good default value is ~300-500 (can depend on the average local contrast and sharpness of the image).  user can further filter out some features based on their hessian values and other characteristics.}
\cvarg{nOctaves}{the number of octaves to be used for extraction.  With each next octave the feature size is doubled (3 by default)}
\cvarg{nOctaveLayers}{The number of layers within each octave (4 by default)}
\end{description}}
\fi
\end{description}

The function cvExtractSURF finds robust features in the image, as
described in \cite{Bay06}. For each feature it returns its location, size,
orientation and optionally the descriptor, basic or extended. The function
can be used for object tracking and localization, image stitching etc.

\ifC
See the
\texttt{find\_obj.cpp} demo in OpenCV samples directory.
\else
To extract strong SURF features from an image

\begin{lstlisting}
>>> import cv
>>> im = cv.LoadImageM("building.jpg", cv.CV_LOAD_IMAGE_GRAYSCALE)
>>> (keypoints, descriptors) = cv.ExtractSURF(im, None, cv.CreateMemStorage(), (0, 30000, 3, 1))
>>> print len(keypoints), len(descriptors)
6 6
>>> for ((x, y), laplacian, size, dir, hessian) in keypoints:
...     print "x=\%d y=\%d laplacian=\%d size=\%d dir=\%f hessian=\%f" \% (x, y, laplacian, size, dir, hessian)
x=30 y=27 laplacian=-1 size=31 dir=69.778503 hessian=36979.789062
x=296 y=197 laplacian=1 size=33 dir=111.081039 hessian=31514.349609
x=296 y=266 laplacian=1 size=32 dir=107.092300 hessian=31477.908203
x=254 y=284 laplacian=1 size=31 dir=279.137360 hessian=34169.800781
x=498 y=525 laplacian=-1 size=33 dir=278.006592 hessian=31002.759766
x=777 y=281 laplacian=1 size=70 dir=167.940964 hessian=35538.363281
\end{lstlisting}

\fi

\cvCPyFunc{GetStarKeypoints}
Retrieves keypoints using the StarDetector algorithm.

\cvdefC{
CvSeq* cvGetStarKeypoints( \par const CvArr* image,\par CvMemStorage* storage,\par CvStarDetectorParams params=cvStarDetectorParams() );
}
\cvdefPy{GetStarKeypoints(image,storage,params)-> keypoints}

\begin{description}
\cvarg{image}{The input 8-bit grayscale image}
\cvarg{storage}{Memory storage where the keypoints will be stored}
\ifC
\cvarg{params}{Various algorithm parameters given to the structure CvStarDetectorParams:}
\begin{lstlisting}
 typedef struct CvStarDetectorParams
 {
    int maxSize; // maximal size of the features detected. The following 
                 // values of the parameter are supported:
                 // 4, 6, 8, 11, 12, 16, 22, 23, 32, 45, 46, 64, 90, 128
    int responseThreshold; // threshold for the approximatd laplacian,
                           // used to eliminate weak features
    int lineThresholdProjected; // another threshold for laplacian to 
                // eliminate edges
    int lineThresholdBinarized; // another threshold for the feature 
                // scale to eliminate edges
    int suppressNonmaxSize; // linear size of a pixel neighborhood 
                // for non-maxima suppression
 }
 CvStarDetectorParams;
\end{lstlisting}
\else
\cvarg{params}{Various algorithm parameters in a tuple \texttt{(maxSize, responseThreshold, lineThresholdProjected, lineThresholdBinarized, suppressNonmaxSize)}:
\begin{description}
\cvarg{maxSize}{maximal size of the features detected. The following values of the parameter are supported: 4, 6, 8, 11, 12, 16, 22, 23, 32, 45, 46, 64, 90, 128}
\cvarg{responseThreshold}{threshold for the approximatd laplacian, used to eliminate weak features}
\cvarg{lineThresholdProjected}{another threshold for laplacian to eliminate edges}
\cvarg{lineThresholdBinarized}{another threshold for the feature scale to eliminate edges}
\cvarg{suppressNonmaxSize}{linear size of a pixel neighborhood for non-maxima suppression}
\end{description}
}
\fi
\end{description}

The function GetStarKeypoints extracts keypoints that are local
scale-space extremas. The scale-space is constructed by computing
approximate values of laplacians with different sigma's at each
pixel. Instead of using pyramids, a popular approach to save computing
time, all of the laplacians are computed at each pixel of the original
high-resolution image. But each approximate laplacian value is computed
in O(1) time regardless of the sigma, thanks to the use of integral
images. The algorithm is based on the paper 
Agrawal08
, but instead
of a square, hexagon or octagon it uses an 8-end star shape, hence the name,
consisting of overlapping upright and tilted squares.

\ifC
Each computed feature is represented by the following structure:

\begin{lstlisting}
typedef struct CvStarKeypoint
{
    CvPoint pt; // coordinates of the feature
    int size; // feature size, see CvStarDetectorParams::maxSize
    float response; // the approximated laplacian value at that point.
}
CvStarKeypoint;

inline CvStarKeypoint cvStarKeypoint(CvPoint pt, int size, float response);
\end{lstlisting}
\else
Each keypoint is represented by a tuple \texttt{((x, y), size, response)}:
\begin{description}
\cvarg{x, y}{Screen coordinates of the keypoint}
\cvarg{size}{feature size, up to \texttt{maxSize}}
\cvarg{response}{approximated laplacian value for the keypoint}
\end{description}
\fi

\ifC
Below is the small usage sample:

\begin{lstlisting}
#include "cv.h"
#include "highgui.h"

int main(int argc, char** argv)
{
    const char* filename = argc > 1 ? argv[1] : "lena.jpg";
    IplImage* img = cvLoadImage( filename, 0 ), *cimg;
    CvMemStorage* storage = cvCreateMemStorage(0);
    CvSeq* keypoints = 0;
    int i;

    if( !img )
        return 0;
    cvNamedWindow( "image", 1 );
    cvShowImage( "image", img );
    cvNamedWindow( "features", 1 );
    cimg = cvCreateImage( cvGetSize(img), 8, 3 );
    cvCvtColor( img, cimg, CV_GRAY2BGR );

    keypoints = cvGetStarKeypoints( img, storage, cvStarDetectorParams(45) );

    for( i = 0; i < (keypoints ? keypoints->total : 0); i++ )
    {
        CvStarKeypoint kpt = *(CvStarKeypoint*)cvGetSeqElem(keypoints, i);
        int r = kpt.size/2;
        cvCircle( cimg, kpt.pt, r, CV_RGB(0,255,0));
        cvLine( cimg, cvPoint(kpt.pt.x + r, kpt.pt.y + r),
            cvPoint(kpt.pt.x - r, kpt.pt.y - r), CV_RGB(0,255,0));
        cvLine( cimg, cvPoint(kpt.pt.x - r, kpt.pt.y + r),
            cvPoint(kpt.pt.x + r, kpt.pt.y - r), CV_RGB(0,255,0));
    }
    cvShowImage( "features", cimg );
    cvWaitKey();
}
\end{lstlisting}
\fi

\fi
\ifCpp

\cvclass{KeyPoint}
Data structure for salient point detectors

\begin{lstlisting}
KeyPoint
{
public:
    // default constructor
    KeyPoint();
    // two complete constructors
    KeyPoint(Point2f _pt, float _size, float _angle=-1,
            float _response=0, int _octave=0, int _class_id=-1);
    KeyPoint(float x, float y, float _size, float _angle=-1,
             float _response=0, int _octave=0, int _class_id=-1);
    // coordinate of the point
    Point2f pt;
    // feature size
    float size;
    // feature orintation in degrees
    // (has negative value if the orientation
    // is not defined/not computed)
    float angle;
    // feature strength
    // (can be used to select only
    // the most prominent key points)
    float response;
    // scale-space octave in which the feature has been found;
    // may correlate with the size
    int octave;
    // point (can be used by feature
    // classifiers or object detectors)
    int class_id;
};

// reading/writing a vector of keypoints to a file storage
void write(FileStorage& fs, const string& name, const vector<KeyPoint>& keypoints);
void read(const FileNode& node, vector<KeyPoint>& keypoints);    
\end{lstlisting}


\cvclass{MSER}
Maximally-Stable Extremal Region Extractor

\begin{lstlisting}
class MSER : public CvMSERParams
{
public:
    // default constructor
    MSER();
    // constructor that initializes all the algorithm parameters
    MSER( int _delta, int _min_area, int _max_area,
          float _max_variation, float _min_diversity,
          int _max_evolution, double _area_threshold,
          double _min_margin, int _edge_blur_size );
    // runs the extractor on the specified image; returns the MSERs,
    // each encoded as a contour (vector<Point>, see findContours)
    // the optional mask marks the area where MSERs are searched for
    void operator()( const Mat& image, vector<vector<Point> >& msers, const Mat& mask ) const;
};
\end{lstlisting}

The class encapsulates all the parameters of MSER (see \url{http://en.wikipedia.org/wiki/Maximally_stable_extremal_regions}) extraction algorithm. 

\cvclass{StarDetector}
Implements Star keypoint detector

\begin{lstlisting}
class StarDetector : CvStarDetectorParams
{
public:
    // default constructor
    StarDetector();
    // the full constructor initialized all the algorithm parameters:
    // maxSize - maximum size of the features. The following 
    //      values of the parameter are supported:
    //      4, 6, 8, 11, 12, 16, 22, 23, 32, 45, 46, 64, 90, 128
    // responseThreshold - threshold for the approximated laplacian,
    //      used to eliminate weak features. The larger it is,
    //      the less features will be retrieved
    // lineThresholdProjected - another threshold for the laplacian to 
    //      eliminate edges
    // lineThresholdBinarized - another threshold for the feature 
    //      size to eliminate edges.
    // The larger the 2 threshold, the more points you get.
    StarDetector(int maxSize, int responseThreshold,
                 int lineThresholdProjected,
                 int lineThresholdBinarized,
                 int suppressNonmaxSize);

    // finds keypoints in an image
    void operator()(const Mat& image, vector<KeyPoint>& keypoints) const;
};
\end{lstlisting}

The class implements a modified version of CenSurE keypoint detector described in
\cite{Agrawal08}

\cvclass{SIFT}
Class for extracting keypoints and computing descriptors using approach named Scale Invariant Feature Transform (SIFT).

\begin{lstlisting}
class CV_EXPORTS SIFT
{
public:
    struct CommonParams
    {
        static const int DEFAULT_NOCTAVES = 4;
        static const int DEFAULT_NOCTAVE_LAYERS = 3;
        static const int DEFAULT_FIRST_OCTAVE = -1;
        enum{ FIRST_ANGLE = 0, AVERAGE_ANGLE = 1 };

        CommonParams();
        CommonParams( int _nOctaves, int _nOctaveLayers, int _firstOctave, 
					  int _angleMode );
        int nOctaves, nOctaveLayers, firstOctave;
        int angleMode;
    };

    struct DetectorParams
    {
        static double GET_DEFAULT_THRESHOLD() 
          { return 0.04 / SIFT::CommonParams::DEFAULT_NOCTAVE_LAYERS / 2.0; }
        static double GET_DEFAULT_EDGE_THRESHOLD() { return 10.0; }

        DetectorParams();
        DetectorParams( double _threshold, double _edgeThreshold );
        double threshold, edgeThreshold;
    };

    struct DescriptorParams
    {
        static double GET_DEFAULT_MAGNIFICATION() { return 3.0; }
        static const bool DEFAULT_IS_NORMALIZE = true;
        static const int DESCRIPTOR_SIZE = 128;

        DescriptorParams();
        DescriptorParams( double _magnification, bool _isNormalize, 
						  bool _recalculateAngles );
        double magnification;
        bool isNormalize;
        bool recalculateAngles;
    };

    SIFT();
    //! sift-detector constructor
    SIFT( double _threshold, double _edgeThreshold,
          int _nOctaves=CommonParams::DEFAULT_NOCTAVES,
          int _nOctaveLayers=CommonParams::DEFAULT_NOCTAVE_LAYERS,
          int _firstOctave=CommonParams::DEFAULT_FIRST_OCTAVE,
          int _angleMode=CommonParams::FIRST_ANGLE );
    //! sift-descriptor constructor
    SIFT( double _magnification, bool _isNormalize=true,
          bool _recalculateAngles = true,
          int _nOctaves=CommonParams::DEFAULT_NOCTAVES,
          int _nOctaveLayers=CommonParams::DEFAULT_NOCTAVE_LAYERS,
          int _firstOctave=CommonParams::DEFAULT_FIRST_OCTAVE,
          int _angleMode=CommonParams::FIRST_ANGLE );
    SIFT( const CommonParams& _commParams,
          const DetectorParams& _detectorParams = DetectorParams(),
          const DescriptorParams& _descriptorParams = DescriptorParams() );

    //! returns the descriptor size in floats (128)
    int descriptorSize() const { return DescriptorParams::DESCRIPTOR_SIZE; }
    //! finds the keypoints using SIFT algorithm
    void operator()(const Mat& img, const Mat& mask,
                    vector<KeyPoint>& keypoints) const;
    //! finds the keypoints and computes descriptors for them using SIFT algorithm. 
    //! Optionally it can compute descriptors for the user-provided keypoints
    void operator()(const Mat& img, const Mat& mask,
                    vector<KeyPoint>& keypoints,
                    Mat& descriptors,
                    bool useProvidedKeypoints=false) const;

    CommonParams getCommonParams () const { return commParams; }
    DetectorParams getDetectorParams () const { return detectorParams; }
    DescriptorParams getDescriptorParams () const { return descriptorParams; }
protected:
    ...
};
\end{lstlisting}

\cvclass{SURF}
Class for extracting Speeded Up Robust Features from an image.

\begin{lstlisting}
class SURF : public CvSURFParams
{
public:
    // default constructor
    SURF();
    // constructor that initializes all the algorithm parameters
    SURF(double _hessianThreshold, int _nOctaves=4,
         int _nOctaveLayers=2, bool _extended=false);
    // returns the number of elements in each descriptor (64 or 128)
    int descriptorSize() const;
    // detects keypoints using fast multi-scale Hessian detector
    void operator()(const Mat& img, const Mat& mask,
                    vector<KeyPoint>& keypoints) const;
    // detects keypoints and computes the SURF descriptors for them
    void operator()(const Mat& img, const Mat& mask,
                    vector<KeyPoint>& keypoints,
                    vector<float>& descriptors,
                    bool useProvidedKeypoints=false) const;
};
\end{lstlisting}

The class \texttt{SURF} implements Speeded Up Robust Features descriptor \cite{Bay06}.
There is fast multi-scale Hessian keypoint detector that can be used to find the keypoints
(which is the default option), but the descriptors can be also computed for the user-specified keypoints.
The function can be used for object tracking and localization, image stitching etc. See the
\texttt{find\_obj.cpp} demo in OpenCV samples directory.

\section{Common Interfaces for Feature Detection and Descriptor Extraction}
Both detectors and descriptors in OpenCV have wrappers with common interface that enables to switch easily 
between different algorithms solving the same problem. All objects that implement keypoint detectors inherit 
FeatureDetector interface. Descriptors that are represented as vectors in a multidimensional space can be 
computed with DescriptorExtractor interface. DescriptorMatcher interface can be used to find matches between 
two sets of descriptors. GenericDescriptorMatch is a more generic interface for descriptors. It does not make any 
assumptions about descriptor representation. Every descriptor with DescriptorExtractor interface has a wrapper with 
GenericDescriptorMatch interface (see VectorDescriptorMatch). There are descriptors such as one way descriptor and 
ferns that have GenericDescriptorMatch interface implemented, but do not support DescriptorExtractor.

\cvclass{FeatureDetector}
Abstract base class for 2D image feature detectors.

\begin{lstlisting}
class FeatureDetector
{
public:
    void detect( const Mat& image, vector<KeyPoint>& keypoints, 
                 const Mat& mask=Mat() ) const;
                 
    virtual void read( const FileNode& fn ) {};
    virtual void write( FileStorage& fs ) const {};

protected:
    ...
};
\end{lstlisting}




\cvCppFunc{FeatureDetector::detect}
Detect keypoints in an image.

\cvdefCpp{
void FeatureDetector::detect( const Mat\& image, vector<KeyPoint>\& keypoints, const Mat\& mask=Mat() ) const;
}

\begin{description}
\cvarg{image}{The image.}
\cvarg{keypoints}{The detected keypoints.}
\cvarg{mask}{Mask specifying where to look for keypoints (optional). Must be a char matrix with non-zero values in the region of interest.}
\end{description}

\cvCppFunc{FeatureDetector::read}
Read feature detector from file node.

\cvdefCpp{
void FeatureDetector::read( const FileNode\& fn );
}

\begin{description}
\cvarg{fn}{File node from which detector will be read.}
\end{description}

\cvCppFunc{FeatureDetector::write}
Write feature detector to file storage.

\cvdefCpp{
void FeatureDetector::write( FileStorage\& fs ) const;
}

\begin{description}
\cvarg{fs}{File storage in which detector will be written.}
\end{description}

\cvclass{FastFeatureDetector}
Wrapping class for feature detection using \cvCppCross{FAST} method.

\begin{lstlisting}
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:
	...
};
\end{lstlisting}

\cvclass{GoodFeaturesToTrackDetector}
Wrapping class for feature detection using \cvCppCross{goodFeaturesToTrack} method.

\begin{lstlisting}
class GoodFeaturesToTrackDetector : public FeatureDetector
{
public:
    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:
	...
}
\end{lstlisting}

\cvclass{MserFeatureDetector}
Wrapping class for feature detection using \cvCppCross{MSER} class.

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

\cvclass{StarFeatureDetector}
Wrapping class for feature detection using \cvCppCross{StarDetector} class.

\begin{lstlisting}
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:
	...
}
\end{lstlisting}

\cvclass{SiftFeatureDetector}
Wrapping class for feature detection using \cvCppCross{SIFT} class.

\begin{lstlisting}
class SiftFeatureDetector : public FeatureDetector
{
public:
    SiftFeatureDetector( double threshold=SIFT::DetectorParams::GET_DEFAULT_THRESHOLD(),
		 double edgeThreshold=SIFT::DetectorParams::GET_DEFAULT_EDGE_THRESHOLD(),
		 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:
	...
}
\end{lstlisting}

\cvclass{SurfFeatureDetector}
Wrapping class for feature detection using \cvCppCross{SURF} class.

\begin{lstlisting}
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:
	...
}
\end{lstlisting}

\cvclass{DescriptorExtractor}
Abstract base class for computing descriptors for image keypoints.

\begin{lstlisting}
class DescriptorExtractor
{
public:
    virtual void compute( const Mat& image, vector<KeyPoint>& keypoints,
                          Mat& descriptors ) const = 0;

    virtual void read (const FileNode &fn) {};
    virtual void write (FileStorage &fs) const {};

protected:
    ...
};
\end{lstlisting}
In this interface we assume a keypoint descriptor can be represented as a
dense, fixed-dimensional vector of some basic type. Most descriptors used
in practice follow this pattern, as it makes it very easy to compute
distances between descriptors. Therefore we represent a collection of
descriptors as a \cvCppCross{Mat}, where each row is one keypoint descriptor.

\cvCppFunc{DescriptorExtractor::compute}
Compute the descriptors for a set of keypoints in an image. Must be implemented by the subclass.

\cvdefCpp{
void DescriptorExtractor::compute( const Mat\& image, vector<KeyPoint>\& keypoints, Mat\& descriptors ) const;
}

\begin{description}
\cvarg{image}{The image.}
\cvarg{keypoints}{The keypoints. Keypoints for which a descriptor cannot be computed are removed.}
\cvarg{descriptors}{The descriptors. Row i is the descriptor for keypoint i.}
\end{description}

\cvCppFunc{DescriptorExtractor::read}
Read descriptor extractor from file node.

\cvdefCpp{
void DescriptorExtractor::read( const FileNode\& fn );
}

\begin{description}
\cvarg{fn}{File node from which detector will be read.}
\end{description}

\cvCppFunc{DescriptorExtractor::write}
Write descriptor extractor to file storage.

\cvdefCpp{
void DescriptorExtractor::write( FileStorage\& fs ) const;
}

\begin{description}
\cvarg{fs}{File storage in which detector will be written.}
\end{description}

\cvclass{SiftDescriptorExtractor}
Wrapping class for descriptors computing using \cvCppCross{SIFT} class.

\begin{lstlisting}
class SiftDescriptorExtractor : public DescriptorExtractor
{
public:
    SiftDescriptorExtractor( 
	     double magnification=SIFT::DescriptorParams::GET_DEFAULT_MAGNIFICATION(),
	     bool isNormalize=true, bool recalculateAngles=true,
	     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 compute( const Mat& image, vector<KeyPoint>& keypoints, Mat& descriptors) const;

    virtual void read (const FileNode &fn);
    virtual void write (FileStorage &fs) const;
protected:
    ...
}
\end{lstlisting}

\cvclass{SurfDescriptorExtractor}
Wrapping class for descriptors computing using \cvCppCross{SURF} class.

\begin{lstlisting}
class SurfDescriptorExtractor : public DescriptorExtractor
{
public:
    SurfDescriptorExtractor( int nOctaves=4,
                             int nOctaveLayers=2, bool extended=false );

    virtual void compute( const Mat& image, vector<KeyPoint>& keypoints, Mat& descriptors) const;

    virtual void read (const FileNode &fn);
    virtual void write (FileStorage &fs) const;
   
protected:
    ...
}
\end{lstlisting}

\cvclass{DescriptorMatcher}
Abstract base class for matching two sets of descriptors.

\begin{lstlisting}
class DescriptorMatcher
{
public:
    void add( const Mat& descriptors );
    // Index the descriptors training set.
    void index();
    void match( const Mat& query, vector<int>& matches ) const;
    void match( const Mat& query, const Mat& mask,
	        vector<int>& matches ) const;
    virtual void clear();
protected:
   ...
};
\end{lstlisting} 

\cvCppFunc{DescriptorMatcher::add}
Add descriptors to the training set.

\cvdefCpp{
void DescriptorMatcher::add( const Mat\& descriptors );
}

\begin{description}
\cvarg{descriptors}{Descriptors to add to the training set.}
\end{description}

\cvCppFunc{DescriptorMatcher::match}
Find the best match for each descriptor from a query set. In one version 
of this method the mask is used to describe which descriptors can be matched.
\texttt{descriptors\_1[i]} can be matched with \texttt{descriptors\_2[j]} only if \texttt{mask.at<char>(i,j)} is non-zero.

\cvdefCpp{
void DescriptorMatcher::match( const Mat\& query, vector<int>\& matches ) const;
}
\cvdefCpp{
void DescriptorMatcher::match( const Mat\& query, const Mat\& mask,
            vector<int>\& matches ) const;
}

\begin{description}
\cvarg{query}{The query set of descriptors.}
\cvarg{matches}{Indices of the closest matches from the training set}
\cvarg{mask}{Mask specifying permissible matches.}
\end{description}

\cvCppFunc{DescriptorMatcher::clear}
Clear training keypoints.

\cvdefCpp{
void DescriptorMatcher::clear();
}

\cvclass{BruteForceMatcher}
Brute-force descriptor matcher. For each descriptor in the first set, this matcher finds the closest
descriptor in the second set by trying each one.

\begin{lstlisting}
template<class Distance>
class BruteForceMatcher : public DescriptorMatcher
{
public:
    BruteForceMatcher( Distance d = Distance() ) : distance(d) {}

protected:
    ...
}
\end{lstlisting}

For efficiency, BruteForceMatcher is templated on the distance metric.
For float descriptors, a common choice would be \texttt{L2<float>}. Class \texttt{L2} is defined as:
\begin{lstlisting}
template<typename T>
struct Accumulator
{
    typedef T Type;
};

template<> struct Accumulator<unsigned char>  { typedef unsigned int Type; };
template<> struct Accumulator<unsigned short> { typedef unsigned int Type; };
template<> struct Accumulator<char>   { typedef int Type; };
template<> struct Accumulator<short>  { typedef int Type; };

/*
 * Squared Euclidean distance functor
 */
template<class T>
struct L2
{
    typedef T ValueType;
    typedef typename Accumulator<T>::Type ResultType;

    ResultType operator()( const T* a, const T* b, int size ) const;
    {
        ResultType result = ResultType();
        for( int i = 0; i < size; i++ )
        {
            ResultType diff = a[i] - b[i];
            result += diff*diff;
        }
        return sqrt(result);
    }
};
\end{lstlisting}

\cvclass{KeyPointCollection}
A storage for sets of keypoints together with corresponding images and class IDs

\begin{lstlisting}
class KeyPointCollection
{
public:
    // Adds keypoints from a single image to the storage.
    // image    Source image
    // points   A vector of keypoints
    void add( const Mat& _image, const vector<KeyPoint>& _points );

    // Returns the total number of keypoints in the collection
    size_t calcKeypointCount() const;

    // Returns the keypoint by its global index
    KeyPoint getKeyPoint( int index ) const;

    // Clears images, keypoints and startIndices
    void clear();

    vector<Mat> images;
    vector<vector<KeyPoint> > points;

    // global indices of the first points in each image,
    // startIndices.size() = points.size()
    vector<int> startIndices;
};
\end{lstlisting}

\cvclass{GenericDescriptorMatch}
Abstract interface for a keypoint descriptor.

\begin{lstlisting}
class GenericDescriptorMatch
{
public:
    enum IndexType
    {
        NoIndex,
        KDTreeIndex
    };

    GenericDescriptorMatch() {}
    virtual ~GenericDescriptorMatch() {}

    virtual void add( KeyPointCollection& keypoints );
    virtual void add( const Mat& image, vector<KeyPoint>& points ) = 0;

    virtual void classify( const Mat& image, vector<KeyPoint>& points );
    virtual void match( const Mat& image, vector<KeyPoint>& points,
                        vector<int>& indices ) = 0;
    
    virtual void clear();
    virtual void read( const FileNode& fn );
    virtual void write( FileStorage& fs ) const;
    
protected:
    KeyPointCollection collection;
};

\end{lstlisting}
\cvCppFunc{GenericDescriptorMatch::add}
Adds keypoints to the training set (descriptors are supposed to be calculated here).
Keypoints can be passed using \cvCppCross{KeyPointCollection} (with with corresponding images) or as a vector of \cvCppCross{KeyPoint} from a single image.

\cvdefCpp{
void GenericDescriptorMatch::add( KeyPointCollection\& keypoints );
}

\begin{description}
\cvarg{keypoints}{Keypoints collection with corresponding images.}
\end{description}


\cvdefCpp{
void GenericDescriptorMatch::add( const Mat\& image, vector<KeyPoint>\& points );
}

\begin{description}
\cvarg{image}{The source image.}
\cvarg{points}{Test keypoints from the source image.}
\end{description}

\cvCppFunc{GenericDescriptorMatch::classify}
Classifies test keypoints.

\cvdefCpp{
void GenericDescriptorMatch::classify( const Mat\& image, vector<KeyPoint>\& points );
}

\begin{description}
\cvarg{image}{The source image.}
\cvarg{points}{Test keypoints from the source image.}
\end{description}

\cvCppFunc{GenericDescriptorMatch::match}
Matches test keypoints to the training set.

\cvdefCpp{
void GenericDescriptorMatch::match( const Mat\& image, vector<KeyPoint>\& points, vector<int>\& indices );
}

\begin{description}
\cvarg{image}{The source image.}
\cvarg{points}{Test keypoints from the source image.}
\cvarg{indices}{A vector to be filled with keypoint class indices.}
\end{description}

\cvCppFunc{GenericDescriptorMatch::clear}
Clears keypoints storing in collection

\cvdefCpp{
void GenericDescriptorMatch::clear();
}

\cvCppFunc{GenericDescriptorMatch::read}
Reads match object from a file node
    
\cvdefCpp{
void GenericDescriptorMatch::read( const FileNode\& fn );
}

\cvCppFunc{GenericDescriptorMatch::write}
Writes match object to a file storage
    
\cvdefCpp{
void GenericDescriptorMatch::write( FileStorage\& fs ) const;
}

\cvclass{VectorDescriptorMatch}
Class used for matching descriptors that can be described as vectors in a finite-dimensional space.

\begin{lstlisting}
template<class Extractor, class Matcher>
class VectorDescriptorMatch : public GenericDescriptorMatch
{
public:
    VectorDescriptorMatch( const Extractor& _extractor = Extractor(),
                           const Matcher& _matcher = Matcher() );
    ~VectorDescriptorMatch();

    // Builds flann index
    void index();

    // Calculates descriptors for a set of keypoints from a single image
    virtual void add( const Mat& image, vector<KeyPoint>& keypoints );

    // Matches a set of keypoints with the training set
    virtual void match( const Mat& image, vector<KeyPoint>& points, 
                        vector<int>& keypointIndices );

    // Clears object (i.e. storing keypoints)
    virtual void clear();

    // Reads object from file node
    virtual void read (const FileNode& fn);
    // Writes object to file storage
    virtual void write (FileStorage& fs) const;
protected:
    Extractor extractor;
    Matcher matcher;
};
\end{lstlisting}

\cvclass{OneWayDescriptorMatch}
Wrapping class for computing, matching and classification of descriptors using \cvCppCross{OneWayDescriptorBase} class.

\begin{lstlisting}
class OneWayDescriptorMatch : public GenericDescriptorMatch
{
public:
    class Params
    {
    public:
        static const int POSE_COUNT = 500;
        static const int PATCH_WIDTH = 24;
        static const int PATCH_HEIGHT = 24;
        static float GET_MIN_SCALE() { return 0.7f; }
        static float GET_MAX_SCALE() { return 1.5f; }
        static float GET_STEP_SCALE() { return 1.2f; }

        Params( int _poseCount = POSE_COUNT,
                Size _patchSize = Size(PATCH_WIDTH, PATCH_HEIGHT),
                string _pcaFilename = string (),
                string _trainPath = string(),
                string _trainImagesList = string(),
                float _minScale = GET_MIN_SCALE(), float _maxScale = GET_MAX_SCALE(),
                float _stepScale = GET_STEP_SCALE() );

        int poseCount;
        Size patchSize;
        string pcaFilename;
        string trainPath;
        string trainImagesList;

        float minScale, maxScale, stepScale;
    };

    OneWayDescriptorMatch();

    // Equivalent to calling PointMatchOneWay() followed by Initialize(_params)
    OneWayDescriptorMatch( const Params& _params );
    virtual ~OneWayDescriptorMatch();

    // Sets one way descriptor parameters
    void initialize( const Params& _params, OneWayDescriptorBase *_base = 0 );

    // Calculates one way descriptors for a set of keypoints
    virtual void add( const Mat& image, vector<KeyPoint>& keypoints );

    // Calculates one way descriptors for a set of keypoints
    virtual void add( KeyPointCollection& keypoints );

    // Matches a set of keypoints from a single image of the training set.
    // A rectangle with a center in a keypoint and size 
    // (patch_width/2*scale, patch_height/2*scale) is cropped from the source image
    // for each keypoint. scale is iterated from DescriptorOneWayParams::min_scale
    // to DescriptorOneWayParams::max_scale. The minimum distance to each 
    // training patch with all its affine poses is found over all scales.
    // The class ID of a match is returned for each keypoint. The distance 
    // is calculated over PCA components loaded with DescriptorOneWay::Initialize,
    // kd tree is used for finding minimum distances.
    virtual void match( const Mat& image, vector<KeyPoint>& points, 
	                    vector<int>& indices );

    // Classify a set of keypoints. The same as match, but returns point 
    // classes rather than indices.
    virtual void classify( const Mat& image, vector<KeyPoint>& points );

    // Clears keypoints storing in collection and OneWayDescriptorBase
    virtual void clear ();

    // Reads match object from a file node
    virtual void read (const FileNode &fn);
    
    // Writes match object to a file storage
    virtual void write (FileStorage& fs) const;

protected:
    Ptr<OneWayDescriptorBase> base;
    Params params;
};
\end{lstlisting}

\cvclass{CalonderDescriptorMatch}
Wrapping class for computing, matching and classification of descriptors using \cvCppCross{RTreeClassifier} class.

\begin{lstlisting}
class CV_EXPORTS CalonderDescriptorMatch : public GenericDescriptorMatch
{
public:
    class Params
    {
    public:
        static const int DEFAULT_NUM_TREES = 80;
        static const int DEFAULT_DEPTH = 9;
        static const int DEFAULT_VIEWS = 5000;
        static const size_t DEFAULT_REDUCED_NUM_DIM = 176;
        static const size_t DEFAULT_NUM_QUANT_BITS = 4;
        static const int DEFAULT_PATCH_SIZE = PATCH_SIZE;

        Params( const RNG& _rng = RNG(), 
                const PatchGenerator& _patchGen = PatchGenerator(),
                int _numTrees=DEFAULT_NUM_TREES,
                int _depth=DEFAULT_DEPTH,
                int _views=DEFAULT_VIEWS,
                size_t _reducedNumDim=DEFAULT_REDUCED_NUM_DIM,
                int _numQuantBits=DEFAULT_NUM_QUANT_BITS,
                bool _printStatus=true,
                int _patchSize=DEFAULT_PATCH_SIZE );
        Params( const string& _filename );

        RNG rng;
        PatchGenerator patchGen;
        int numTrees;
        int depth;
        int views;
        int patchSize;
        size_t reducedNumDim;
        int numQuantBits;
        bool printStatus;

        string filename;
    };

    CalonderDescriptorMatch();
    CalonderDescriptorMatch( const Params& _params );
    virtual ~CalonderDescriptorMatch();
    void initialize( const Params& _params );

    virtual void add( const Mat& image, vector<KeyPoint>& keypoints );
    virtual void match( const Mat& image, vector<KeyPoint>& keypoints, 
                        vector<int>& indices );
    virtual void classify( const Mat& image, vector<KeyPoint>& keypoints );

    virtual void clear ();
    virtual void read( const FileNode &fn );
    virtual void write( FileStorage& fs ) const;
    
protected:
	...
};
\end{lstlisting}

\cvclass{FernDescriptorMatch}
Wrapping class for computing, matching and classification of descriptors using \cvCppCross{FernClassifier} class.

\begin{lstlisting}
class FernDescriptorMatch : public GenericDescriptorMatch
{
public:
    class Params
    {
    public:
        Params( int _nclasses=0,
                int _patchSize=FernClassifier::PATCH_SIZE,
                int _signatureSize=FernClassifier::DEFAULT_SIGNATURE_SIZE,
                int _nstructs=FernClassifier::DEFAULT_STRUCTS,
                int _structSize=FernClassifier::DEFAULT_STRUCT_SIZE,
                int _nviews=FernClassifier::DEFAULT_VIEWS,
                int _compressionMethod=FernClassifier::COMPRESSION_NONE,
                const PatchGenerator& patchGenerator=PatchGenerator() );

        Params( const string& _filename );

        int nclasses;
        int patchSize;
        int signatureSize;
        int nstructs;
        int structSize;
        int nviews;
        int compressionMethod;
        PatchGenerator patchGenerator;

        string filename;
    };

    FernDescriptorMatch();
    FernDescriptorMatch( const Params& _params );
    virtual ~FernDescriptorMatch();
    void initialize( const Params& _params );

    virtual void add( const Mat& image, vector<KeyPoint>& keypoints );
    virtual void match( const Mat& image, vector<KeyPoint>& keypoints, 
                        vector<int>& indices );
    virtual void classify( const Mat& image, vector<KeyPoint>& keypoints );

    virtual void clear ();
    virtual void read( const FileNode &fn );
    virtual void write( FileStorage& fs ) const;
    
protected:
	...
};
\end{lstlisting}

\cvCppFunc{drawMatches}
This function draws matches of keypints from two images on output image. 
Match is a line connecting two keypoints (circles).

\cvdefCpp{
void drawMatches( const Mat\& img1, const vector<KeyPoint>\& keypoints1,
		  const Mat\& img2, const vector<KeyPoint>\& keypoints2,
                  const vector<int>\& matches, Mat\& outImg,
                  const Scalar\& matchColor = Scalar::all(-1), 
	          const Scalar\& singlePointColor = Scalar::all(-1),
                  const vector<char>\& matchesMask = vector<char>(), 
		  int flags = DrawMatchesFlags::DEFAULT );
}

\begin{description}
\cvarg{img1}{First source image.}
\end{description}

\begin{description}
\cvarg{keypoints1}{Keypoints from first source image.}
\end{description}

\begin{description}
\cvarg{img1}{Second source image.}
\end{description}

\begin{description}
\cvarg{keypoints2}{Keypoints from second source image.}
\end{description}

\begin{description}
\cvarg{matches}{Matches from first image to second one, i.e. keypoints1[i] has corresponding point keypoints2[matches[i]]}
\end{description}

\begin{description}
\cvarg{outImg}{Output image. Its content depends on \texttt{flags} value what is drawn in output image. See below possible \texttt{flags} bit values. }
\end{description}

\begin{description}
\cvarg{matchColor}{Color of matches (lines and connected keypoints). If \texttt{matchColor}==Scalar::all(-1) color will be generated randomly.}
\end{description}

\begin{description}
\cvarg{singlePointColor}{Color of single keypoints (circles), i.e. keypoints not having the matches. If \texttt{singlePointColor}==Scalar::all(-1) color will be generated randomly.}
\end{description}

\begin{description}
\cvarg{matchesMask}{Mask determining which matches will be drawn. If mask is empty all matches will be drawn. }
\end{description}

\begin{description}
\cvarg{flags}{Each bit of \texttt{flags} sets some feature of drawing. Possible \texttt{flags} bit values is defined by DrawMatchesFlags, see below. }
\end{description}

\begin{lstlisting}
struct DrawMatchesFlags
{
    enum{ DEFAULT = 0, // Output image matrix will be created (Mat::create),
                       // i.e. existing memory of output image may be reused. 
                       // Two source image, matches and single keypoints will be drawn.
          DRAW_OVER_OUTIMG = 1, // Output image matrix will not be created (Mat::create).
                                // Matches will be drawn on existing content 
				// of output image.
          NOT_DRAW_SINGLE_POINTS = 2 // Single keypoints will not be drawn.
        };
};

\end{lstlisting}

\fi
