Feature Detection

cv::Canny

void Canny(const Mat& image, Mat& edges, double threshold1, double threshold2, int apertureSize=3, bool L2gradient=false)

Finds edges in an image using Canny algorithm.

Parameters:
  • image – Single-channel 8-bit input image
  • edges – The output edge map. It will have the same size and the same type as image
  • threshold1 – The first threshold for the hysteresis procedure
  • threshold2 – The second threshold for the hysteresis procedure
  • apertureSize – Aperture size for the Sobel() operator
  • L2gradient – Indicates, whether the more accurate L_2 norm =\sqrt{(dI/dx)^2 + (dI/dy)^2} should be used to compute the image gradient magnitude ( L2gradient=true ), or a faster default L_1 norm =|dI/dx|+|dI/dy| is enough ( L2gradient=false )

The function finds edges in the input image image and marks them in the output map edges using the Canny algorithm. The smallest value between threshold1 and threshold2 is used for edge linking, the largest value is used to find the initial segments of strong edges, see http://en.wikipedia.org/wiki/Canny_edge_detector

cv::cornerEigenValsAndVecs

void cornerEigenValsAndVecs(const Mat& src, Mat& dst, int blockSize, int apertureSize, int borderType=BORDER_DEFAULT)

Calculates eigenvalues and eigenvectors of image blocks for corner detection.

Parameters:
  • src – Input single-channel 8-bit or floating-point image
  • dst – Image to store the results. It will have the same size as src and the type CV_32FC(6)
  • blockSize – Neighborhood size (see discussion)
  • apertureSize – Aperture parameter for the Sobel() operator
  • boderType – Pixel extrapolation method; see borderInterpolate()

For every pixel p , the function cornerEigenValsAndVecs considers a blockSize \times blockSize neigborhood S(p) . It calculates the covariation matrix of derivatives over the neighborhood as:

M =  \begin{bmatrix} \sum _{S(p)}(dI/dx)^2 &  \sum _{S(p)}(dI/dx dI/dy)^2  \\ \sum _{S(p)}(dI/dx dI/dy)^2 &  \sum _{S(p)}(dI/dy)^2 \end{bmatrix}

Where the derivatives are computed using Sobel() operator.

After that it finds eigenvectors and eigenvalues of M and stores them into destination image in the form (\lambda_1, \lambda_2, x_1, y_1, x_2, y_2) where

  • \lambda_1, \lambda_2

    are the eigenvalues of M ; not sorted

  • x_1, y_1

    are the eigenvectors corresponding to \lambda_1

  • x_2, y_2

    are the eigenvectors corresponding to \lambda_2

The output of the function can be used for robust edge or corner detection.

See also: cornerMinEigenVal() , cornerHarris() , preCornerDetect()

cv::cornerHarris

void cornerHarris(const Mat& src, Mat& dst, int blockSize, int apertureSize, double k, int borderType=BORDER_DEFAULT)

Harris edge detector.

Parameters:
  • src – Input single-channel 8-bit or floating-point image
  • dst – Image to store the Harris detector responses; will have type CV_32FC1 and the same size as src
  • blockSize – Neighborhood size (see the discussion of cornerEigenValsAndVecs() )
  • apertureSize – Aperture parameter for the Sobel() operator
  • k – Harris detector free parameter. See the formula below
  • boderType – Pixel extrapolation method; see borderInterpolate()

The function runs the Harris edge detector on the image. Similarly to cornerMinEigenVal() and cornerEigenValsAndVecs() , for each pixel (x, y) it calculates a 2\times2 gradient covariation matrix M^{(x,y)} over a \texttt{blockSize} \times \texttt{blockSize} neighborhood. Then, it computes the following characteristic:

\texttt{dst} (x,y) =  \mathrm{det} M^{(x,y)} - k  \cdot \left ( \mathrm{tr} M^{(x,y)} \right )^2

Corners in the image can be found as the local maxima of this response map.

cv::cornerMinEigenVal

void cornerMinEigenVal(const Mat& src, Mat& dst, int blockSize, int apertureSize=3, int borderType=BORDER_DEFAULT)

Calculates the minimal eigenvalue of gradient matrices for corner detection.

Parameters:
  • src – Input single-channel 8-bit or floating-point image
  • dst – Image to store the minimal eigenvalues; will have type CV_32FC1 and the same size as src
  • blockSize – Neighborhood size (see the discussion of cornerEigenValsAndVecs() )
  • apertureSize – Aperture parameter for the Sobel() operator
  • boderType – Pixel extrapolation method; see borderInterpolate()

The function is similar to cornerEigenValsAndVecs() but it calculates and stores only the minimal eigenvalue of the covariation matrix of derivatives, i.e. \min(\lambda_1, \lambda_2) in terms of the formulae in cornerEigenValsAndVecs() description.

cv::cornerSubPix

void cornerSubPix(const Mat& image, vector<Point2f>& corners, Size winSize, Size zeroZone, TermCriteria criteria)

Refines the corner locations.

Parameters:
  • image – Input image
  • corners – Initial coordinates of the input corners; refined coordinates on output
  • winSize – Half of the side length of the search window. For example, if winSize=Size(5,5) , then a 5*2+1 \times 5*2+1 = 11 \times 11 search window would be used
  • zeroZone – Half of the size of the dead region in the middle of the search zone over which the summation in the formula below is not done. It is used sometimes to avoid possible singularities of the autocorrelation matrix. The value of (-1,-1) indicates that there is no such size
  • criteria – Criteria for termination of the iterative process of corner refinement. That is, the process of corner position refinement stops either after a certain number of iterations or when a required accuracy is achieved. The criteria may specify either of or both the maximum number of iteration and the required accuracy

The function iterates to find the sub-pixel accurate location of corners, or radial saddle points, as shown in on the picture below.

_images/cornersubpix.png

Sub-pixel accurate corner locator is based on the observation that every vector from the center q to a point p located within a neighborhood of q is orthogonal to the image gradient at p subject to image and measurement noise. Consider the expression:

\epsilon _i = {DI_{p_i}}^T  \cdot (q - p_i)

where {DI_{p_i}} is the image gradient at the one of the points p_i in a neighborhood of q . The value of q is to be found such that \epsilon_i is minimized. A system of equations may be set up with \epsilon_i set to zero:

\sum _i(DI_{p_i}  \cdot {DI_{p_i}}^T) -  \sum _i(DI_{p_i}  \cdot {DI_{p_i}}^T  \cdot p_i)

where the gradients are summed within a neighborhood (“search window”) of q . Calling the first gradient term G and the second gradient term b gives:

q = G^{-1}  \cdot b

The algorithm sets the center of the neighborhood window at this new center q and then iterates until the center keeps within a set threshold.

cv::goodFeaturesToTrack

void goodFeaturesToTrack(const Mat& image, vector<Point2f>& corners, int maxCorners, double qualityLevel, double minDistance, const Mat& mask=Mat(), int blockSize=3, bool useHarrisDetector=false, double k=0.04)

Determines strong corners on an image.

Parameters:
  • image – The input 8-bit or floating-point 32-bit, single-channel image
  • corners – The output vector of detected corners
  • maxCorners – The maximum number of corners to return. If there are more corners than that will be found, the strongest of them will be returned
  • qualityLevel – Characterizes the minimal accepted quality of image corners; the value of the parameter is multiplied by the by the best corner quality measure (which is the min eigenvalue, see cornerMinEigenVal() , or the Harris function response, see cornerHarris() ). The corners, which quality measure is less than the product, will be rejected. For example, if the best corner has the quality measure = 1500, and the qualityLevel=0.01 , then all the corners which quality measure is less than 15 will be rejected.
  • minDistance – The minimum possible Euclidean distance between the returned corners
  • mask – The optional region of interest. If the image is not empty (then it needs to have the type CV_8UC1 and the same size as image ), it will specify the region in which the corners are detected
  • blockSize – Size of the averaging block for computing derivative covariation matrix over each pixel neighborhood, see cornerEigenValsAndVecs()
  • useHarrisDetector – Indicates, whether to use operator or cornerMinEigenVal()
  • k – Free parameter of Harris detector

The function finds the most prominent corners in the image or in the specified image region, as described in [Shi94] :

  1. the function first calculates the corner quality measure at every source image pixel using the cornerMinEigenVal() or cornerHarris()
  2. then it performs non-maxima suppression (the local maxima in 3\times 3 neighborhood are retained).
  3. the next step rejects the corners with the minimal eigenvalue less than \texttt{qualityLevel} \cdot \max_{x,y} qualityMeasureMap(x,y) .
  4. the remaining corners are then sorted by the quality measure in the descending order.
  5. finally, the function throws away each corner pt_j if there is a stronger corner pt_i ( i < j ) such that the distance between them is less than minDistance

The function can be used to initialize a point-based tracker of an object.

Note that the if the function is called with different values A and B of the parameter qualityLevel , and A > {B}, the vector of returned corners with qualityLevel=A will be the prefix of the output vector with qualityLevel=B .

See also: cornerMinEigenVal() , cornerHarris() , calcOpticalFlowPyrLK() , estimateRigidMotion() , PlanarObjectDetector() , OneWayDescriptor()

cv::HoughCircles

void HoughCircles(Mat& image, vector<Vec3f>& circles, int method, double dp, double minDist, double param1=100, double param2=100, int minRadius=0, int maxRadius=0)

Finds circles in a grayscale image using a Hough transform.

Parameters:
  • image – The 8-bit, single-channel, grayscale input image
  • circles – The output vector of found circles. Each vector is encoded as 3-element floating-point vector (x, y, radius)
  • method – Currently, the only implemented method is CV_HOUGH_GRADIENT , which is basically 21HT , described in [Yuen90] .
  • dp – The inverse ratio of the accumulator resolution to the image resolution. For example, if dp=1 , the accumulator will have the same resolution as the input image, if dp=2 - accumulator will have half as big width and height, etc
  • minDist – Minimum distance between the centers of the detected circles. If the parameter is too small, multiple neighbor circles may be falsely detected in addition to a true one. If it is too large, some circles may be missed
  • param1 – The first method-specific parameter. in the case of CV_HOUGH_GRADIENT it is the higher threshold of the two passed to Canny() edge detector (the lower one will be twice smaller)
  • param2 – The second method-specific parameter. in the case of CV_HOUGH_GRADIENT it is the accumulator threshold at the center detection stage. The smaller it is, the more false circles may be detected. Circles, corresponding to the larger accumulator values, will be returned first
  • minRadius – Minimum circle radius
  • maxRadius – Maximum circle radius

The function finds circles in a grayscale image using some modification of Hough transform. Here is a short usage example:

#include <cv.h>
#include <highgui.h>
#include <math.h>

using namespace cv;

int main(int argc, char** argv)
{
    Mat img, gray;
    if( argc != 2 && !(img=imread(argv[1], 1)).data)
        return -1;
    cvtColor(img, gray, CV_BGR2GRAY);
    // smooth it, otherwise a lot of false circles may be detected
    GaussianBlur( gray, gray, Size(9, 9), 2, 2 );
    vector<Vec3f> circles;
    HoughCircles(gray, circles, CV_HOUGH_GRADIENT,
                 2, gray->rows/4, 200, 100 );
    for( size_t i = 0; i < circles.size(); i++ )
    {
         Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
         int radius = cvRound(circles[i][2]);
         // draw the circle center
         circle( img, center, 3, Scalar(0,255,0), -1, 8, 0 );
         // draw the circle outline
         circle( img, center, radius, Scalar(0,0,255), 3, 8, 0 );
    }
    namedWindow( "circles", 1 );
    imshow( "circles", img );
    return 0;
}

Note that usually the function detects the circles’ centers well, however it may fail to find the correct radii. You can assist the function by specifying the radius range ( minRadius and maxRadius ) if you know it, or you may ignore the returned radius, use only the center and find the correct radius using some additional procedure.

See also: fitEllipse() , minEnclosingCircle()

cv::HoughLines

void HoughLines(Mat& image, vector<Vec2f>& lines, double rho, double theta, int threshold, double srn=0, double stn=0)

Finds lines in a binary image using standard Hough transform.

Parameters:
  • image – The 8-bit, single-channel, binary source image. The image may be modified by the function
  • lines – The output vector of lines. Each line is represented by a two-element vector (\rho, \theta) . \rho is the distance from the coordinate origin (0,0) (top-left corner of the image) and \theta is the line rotation angle in radians ( 0 \sim \textrm{vertical line}, \pi/2 \sim \textrm{horizontal line} )
  • rho – Distance resolution of the accumulator in pixels
  • theta – Angle resolution of the accumulator in radians
  • threshold – The accumulator threshold parameter. Only those lines are returned that get enough votes ( >\texttt{threshold} )
  • srn – For the multi-scale Hough transform it is the divisor for the distance resolution rho . The coarse accumulator distance resolution will be rho and the accurate accumulator resolution will be rho/srn . If both srn=0 and stn=0 then the classical Hough transform is used, otherwise both these parameters should be positive.
  • stn – For the multi-scale Hough transform it is the divisor for the distance resolution theta

The function implements standard or standard multi-scale Hough transform algorithm for line detection. See HoughLinesP() for the code example.

cv::HoughLinesP

void HoughLinesP(Mat& image, vector<Vec4i>& lines, double rho, double theta, int threshold, double minLineLength=0, double maxLineGap=0)

Finds lines segments in a binary image using probabilistic Hough transform.

Parameters:
  • image – The 8-bit, single-channel, binary source image. The image may be modified by the function
  • lines – The output vector of lines. Each line is represented by a 4-element vector (x_1, y_1, x_2, y_2) , where (x_1,y_1) and (x_2, y_2) are the ending points of each line segment detected.
  • rho – Distance resolution of the accumulator in pixels
  • theta – Angle resolution of the accumulator in radians
  • threshold – The accumulator threshold parameter. Only those lines are returned that get enough votes ( >\texttt{threshold} )
  • minLineLength – The minimum line length. Line segments shorter than that will be rejected
  • maxLineGap – The maximum allowed gap between points on the same line to link them.

The function implements probabilistic Hough transform algorithm for line detection, described in [Matas00] . Below is line detection example:

/* This is a standalone program. Pass an image name as a first parameter
of the program.  Switch between standard and probabilistic Hough transform
by changing "#if 1" to "#if 0" and back */
#include <cv.h>
#include <highgui.h>
#include <math.h>

using namespace cv;

int main(int argc, char** argv)
{
    Mat src, dst, color_dst;
    if( argc != 2 || !(src=imread(argv[1], 0)).data)
        return -1;

    Canny( src, dst, 50, 200, 3 );
    cvtColor( dst, color_dst, CV_GRAY2BGR );

#if 0
    vector<Vec2f> lines;
    HoughLines( dst, lines, 1, CV_PI/180, 100 );

    for( size_t i = 0; i < lines.size(); i++ )
    {
        float rho = lines[i][0];
        float theta = lines[i][1];
        double a = cos(theta), b = sin(theta);
        double x0 = a*rho, y0 = b*rho;
        Point pt1(cvRound(x0 + 1000*(-b)),
                  cvRound(y0 + 1000*(a)));
        Point pt2(cvRound(x0 - 1000*(-b)),
                  cvRound(y0 - 1000*(a)));
        line( color_dst, pt1, pt2, Scalar(0,0,255), 3, 8 );
    }
#else
    vector<Vec4i> lines;
    HoughLinesP( dst, lines, 1, CV_PI/180, 80, 30, 10 );
    for( size_t i = 0; i < lines.size(); i++ )
    {
        line( color_dst, Point(lines[i][0], lines[i][1]),
            Point(lines[i][2], lines[i][3]), Scalar(0,0,255), 3, 8 );
    }
#endif
    namedWindow( "Source", 1 );
    imshow( "Source", src );

    namedWindow( "Detected Lines", 1 );
    imshow( "Detected Lines", color_dst );

    waitKey(0);
    return 0;
}

This is the sample picture the function parameters have been tuned for:

_images/building.jpg

And this is the output of the above program in the case of probabilistic Hough transform

_images/houghp.png

cv::perCornerDetect

void preCornerDetect(const Mat& src, Mat& dst, int apertureSize, int borderType=BORDER_DEFAULT)

Calculates the feature map for corner detection

Parameters:
  • src – The source single-channel 8-bit of floating-point image
  • dst – The output image; will have type CV_32F and the same size as src
  • apertureSize – Aperture size of Sobel()
  • borderType – The pixel extrapolation method; see borderInterpolate()

The function calculates the complex spatial derivative-based function of the source image

\texttt{dst} = (D_x  \texttt{src} )^2  \cdot D_{yy}  \texttt{src} + (D_y  \texttt{src} )^2  \cdot D_{xx}  \texttt{src} - 2 D_x  \texttt{src} \cdot D_y  \texttt{src} \cdot D_{xy}  \texttt{src}

where D_x , D_y are the first image derivatives, D_{xx} , D_{yy} are the second image derivatives and D_{xy} is the mixed derivative.

The corners can be found as local maximums of the functions, as shown below:

Mat corners, dilated_corners;
preCornerDetect(image, corners, 3);
// dilation with 3x3 rectangular structuring element
dilate(corners, dilated_corners, Mat(), 1);
Mat corner_mask = corners == dilated_corners;

KeyPoint

KeyPoint

Data structure for salient point detectors

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);

MSER

MSER

Maximally-Stable Extremal Region Extractor

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;
};

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

SURF

SURF

Class for extracting Speeded Up Robust Features from an image.

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;
};

The class SURF implements Speeded Up Robust Features descriptor [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 find_obj.cpp demo in OpenCV samples directory.

StarDetector

StarDetector

Implements Star keypoint detector

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;
};

The class implements a modified version of CenSurE keypoint detector described in [Agrawal08]