Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ OpenCV Feature Detection and Description

C++ OpenCV Feature Detection and Description

Feature detection and description is a core technique in computer vision, used to extract keypoints from an image and compute their descriptors. These keypoints and descriptors can be used for image matching, target recognition, 3D reconstruction, and other tasks.

Main steps

  • Feature Detection and Description: Uses feature matching to find similar keypoints in two images, for image stitching, panorama generation, etc.
  • Object Recognition: Extracts features of the target image and matches them with features in a database to achieve object recognition.
  • 3D Reconstruction: Through feature point matching in multi-view images, computes the camera pose and reconstructs the 3D scene.
  • Real-time Tracking: Detects and tracks feature points in video sequences, for applications such as SLAM (Simultaneous Localization and Mapping).
  • Common Feature Detection and Description Algorithms

    OpenCV provides a variety of feature detection and description algorithms; the following are some common ones:

    Algorithm Feature Applicable scenarios
    SIFT Scale-Invariant Feature Transform, robust to changes in rotation, scale, and brightness. Image Matching, Object Recognition
    SURF An accelerated version of SIFT, with faster computation but slightly less robust to rotation and scale changes. Real-time Image Matching
    ORB Based on FAST keypoint detection and BRIEF descriptors, fast and suitable for real-time applications. Real-time Image Matching, SLAM
    FAST A fast corner detection algorithm that only detects keypoints and does not generate descriptors. Real-time Corner Detection
    BRIEF A binary descriptor with fast computation, but less robust to rotation and scale changes. Real-time Image Matching
    AKAZE A feature detection and description algorithm based on non-linear scale space, robust to rotation and scale changes. Image Matching, Object Recognition

    Corner Detection

    Corners are points in an image with drastic brightness changes, usually located at object edges or texture-rich regions. Corner detection is the foundation of feature detection; common corner detection algorithms include Harris corner detection and Shi-Tomasi corner detection.

    Harris Corner Detection

    Harris corner detection is a classic corner detection algorithm. It determines whether a point is a corner by computing the autocorrelation matrix of each pixel in the image. The eigenvalues of the autocorrelation matrix reflect the local structure of the point: if both eigenvalues are large, the point is likely a corner; if one eigenvalue is large and the other small, the point may be an edge; if both eigenvalues are small, the point may be a flat region.

    In OpenCV, you can use the cv::cornerHarris function to implement Harris corner detection:

    Example

    #include

    int

    main

    (

    )

    {

        cv

    ::

    Mat

    src

    =

    cv

    ::

    imread

    (

    "image.jpg"

    , cv

    ::

    IMREAD_GRAYSCALE

    )

    ;

        cv

    ::

    Mat

    dst, dst_norm

    ;

       

    // Harris corner detection

        cv

    ::

    cornerHarris

    (

    src, dst,

    2

    ,

    3

    ,

    0.04

    )

    ;

       

    // Normalize and display the result

        cv

    ::

    normalize

    (

    dst, dst_norm,

    0

    ,

    255

    , cv

    ::

    NORM_MINMAX

    , CV_32FC1, cv

    ::

    Mat

    (

    )

    )

    ;

        cv

    ::

    imshow

    (

    "Harris Corners"

    , dst_norm

    )

    ;

        cv

    ::

    waitKey

    (

    0

    )

    ;

       

    return

    0

    ;

    }

    Shi-Tomasi Corner Detection

    Shi-Tomasi corner detection is an improvement on Harris corner detection. It determines whether a point is a corner by computing the minimum eigenvalue of each pixel in the image. Compared with Harris corner detection, Shi-Tomasi corner detection is more stable and requires less computation.

    In OpenCV, you can use thecv::goodFeaturesToTrackfunction to implement Shi-Tomasi corner detection:

    Example

    #include

    int

    main

    (

    )

    {

        cv

    ::

    Mat

    src

    =

    cv

    ::

    imread

    (

    "image.jpg"

    , cv

    ::

    IMREAD_GRAYSCALE

    )

    ;

        std

    ::

    vector

    <

    cv

    ::

    Point2f

    corners

    ;

       

    // Shi-Tomasi corner detection

        cv

    ::

    goodFeaturesToTrack

    (

    src, corners,

    100

    ,

    0.01

    ,

    10

    )

    ;

       

    // Draw the corners

       

    for

    (

    size_t

    i

    =

    0

    ;

    i

    <

    corners.

    size

    (

    )

    ;

    i

    ++

    )

    {

            cv

    ::

    circle

    (

    src, corners

    [

    i

    ]

    ,

    5

    , cv

    ::

    Scalar

    (

    0

    ,

    0

    ,

    255

    )

    ,

    2

    )

    ;

       

    }

        cv

    ::

    imshow

    (

    "Shi-Tomasi Corners"

    , src

    )

    ;

        cv

    ::

    waitKey

    (

    0

    )

    ;

       

    return

    0

    ;

    }


    Feature Point Detection

    Feature point detection extracts points with distinctive properties from an image; these points are usually invariant to rotation, scale, and illumination. Common feature point detection algorithms include SIFT, SURF, and ORB.

    SIFT Algorithm

    SIFT (Scale-Invariant Feature Transform) is a scale-space-based feature point detection algorithm that is invariant to image rotation, scale, and brightness changes. The SIFT algorithm generates feature descriptors by detecting extreme points in the image and computing histograms of gradient orientations at these points.

    In OpenCV, you can use thecv::xfeatures2d::SIFTclass to implement SIFT feature point detection:

    Example

    #include

    #include

    int

    main

    (

    )

    {

        cv

    ::

    Mat

    src

    =

    cv

    ::

    imread

    (

    "image.jpg"

    , cv

    ::

    IMREAD_GRAYSCALE

    )

    ;

        std

    ::

    vector

    <

    cv

    ::

    KeyPoint

    keypoints

    ;

        cv

    ::

    Mat

    descriptors

    ;

       

    // SIFT feature point detection

        cv

    ::

    Ptr

    <

    cv

    ::

    xfeatures2d

    ::

    SIFT

    sift

    =

    cv

    ::

    xfeatures2d

    ::

    SIFT

    ::

    create

    (

    )

    ;

        sift

    detectAndCompute

    (

    src, cv

    ::

    noArray

    (

    )

    , keypoints, descriptors

    )

    ;

       

    // Draw the feature points

        cv

    ::

    Mat

    output

    ;

        cv

    ::

    drawKeypoints

    (

    src, keypoints, output

    )

    ;

        cv

    ::

    imshow

    (

    "SIFT Keypoints"

    , output

    )

    ;

        cv

    ::

    waitKey

    (

    0

    )

    ;

       

    return

    0

    ;

    }

    SURF Algorithm

    SURF (Speeded-Up Robust Features) is an improvement on the SIFT algorithm. It accelerates the feature point detection process by using integral images and the Hessian matrix. While maintaining high detection accuracy, the SURF algorithm significantly improves computation speed.

    In OpenCV, you can use thecv::xfeatures2d::SURFclass to implement SURF feature point detection:

    Example

    #include

    #include

    int

    main

    (

    )

    {

        cv

    ::

    Mat

    src

    =

    cv

    ::

    imread

    (

    "image.jpg"

    , cv

    ::

    IMREAD_GRAYSCALE

    )

    ;

        std

    ::

    vector

    <

    cv

    ::

    KeyPoint

    keypoints

    ;

        cv

    ::

    Mat

    descriptors

    ;

       

    // SURF feature point detection

        cv

    ::

    Ptr

    <

    cv

    ::

    xfeatures2d

    ::

    SURF

    surf

    =

    cv

    ::

    xfeatures2d

    ::

    SURF

    ::

    create

    (

    )

    ;

        surf

    detectAndCompute

    (

    src, cv

    ::

    noArray

    (

    )

    , keypoints, descriptors

    )

    ;

       

    // Draw the feature points

        cv

    ::

    Mat

    output

    ;

        cv

    ::

    drawKeypoints

    (

    src, keypoints, output

    )

    ;

        cv

    ::

    imshow

    (

    "SURF Keypoints"

    , output

    )

    ;

        cv

    ::

    waitKey

    (

    0

    )

    ;

       

    return

    0

    ;

    }

    ORB Algorithm

    ORB (Oriented FAST and Rotated BRIEF) is a feature point detection algorithm that combines FAST corner detection with BRIEF descriptors. The ORB algorithm has high computational efficiency and is suitable for real-time applications.

    In OpenCV, you can use thecv::ORBclass to implement ORB feature point detection:

    Example

    #include

    int

    main

    (

    )

    {

        cv

    ::

    Mat

    src

    =

    cv

    ::

    imread

    (

    "image.jpg"

    , cv

    ::

    IMREAD_GRAYSCALE

    )

    ;

        std

    ::

    vector

    <

    cv

    ::

    KeyPoint

    keypoints

    ;

        cv

    ::

    Mat

    descriptors

    ;

       

    // ORB feature point detection

        cv

    ::

    Ptr

    <

    cv

    ::

    ORB

    orb

    =

    cv

    ::

    ORB

    ::

    create

    (

    )

    ;

        orb

    detectAndCompute

    (

    src, cv

    ::

    noArray

    (

    )

    , keypoints, descriptors

    )

    ;

       

    // Draw the feature points

        cv

    ::

    Mat

    output

    ;

        cv

    ::

    drawKeypoints

    (

    src, keypoints, output

    )

    ;

        cv

    ::

    imshow

    (

    "ORB Keypoints"

    , output

    )

    ;

        cv

    ::

    waitKey

    (

    0

    )

    ;

       

    return

    0

    ;

    }


    Feature Matching

    Feature matching is the process of matching feature points in two images. Common feature matching algorithms include BFMatcher (Brute-Force Matcher) and the FLANN matcher.

    BFMatcher

    BFMatcher (Brute-Force Matcher) is a simple feature matching algorithm that finds the best matching points by computing the Euclidean distance between feature descriptors. BFMatcher is suitable when the number of feature points is small.

    In OpenCV, you can use thecv::BFMatcherclass to implement BFMatcher:

    Example

    #include

    #include

    int

    main

    (

    )

    {

        cv

    ::

    Mat

    src1

    =

    cv

    ::

    imread

    (

    "image1.jpg"

    , cv

    ::

    IMREAD_GRAYSCALE

    )

    ;

        cv

    ::

    Mat

    src2

    =

    cv

    ::

    imread

    (

    "image2.jpg"

    , cv

    ::

    IMREAD_GRAYSCALE

    )

    ;

        std

    ::

    vector

    <

    cv

    ::

    KeyPoint

    keypoints1, keypoints2

    ;

        cv

    ::

    Mat

    descriptors1, descriptors2

    ;

       

    // SIFT feature point detection

        cv

    ::

    Ptr

    <

    cv

    ::

    xfeatures2d

    ::

    SIFT

    sift

    =

    cv

    ::

    xfeatures2d

    ::

    SIFT

    ::

    create

    (

    )

    ;

        sift

    detectAndCompute

    (

    src1, cv

    ::

    noArray

    (

    )

    , keypoints1, descriptors1

    )

    ;

        sift

    detectAndCompute

    (

    src2, cv

    ::

    noArray

    (

    )

    , keypoints2, descriptors2

    )

    ;

       

    // BFMatcher matching

        cv

    ::

    BFMatcher

    matcher

    (

    cv

    ::

    NORM_L2

    )

    ;

        std

    ::

    vector

    <

    cv

    ::

    DMatch

    matches

    ;

        matcher.

    match

    (

    descriptors1, descriptors2, matches

    )

    ;

       

    // Draw the matching results

        cv

    ::

    Mat

    output

    ;

        cv

    ::

    drawMatches

    (

    src1, keypoints1, src2, keypoints2, matches, output

    )

    ;

        cv

    ::

    imshow

    (

    "BFMatcher Matches"

    , output

    )

    ;

        cv

    ::

    waitKey

    (

    0

    )

    ;

       

    return

    0

    ;

    }

    FLANN Matcher

    FLANN (Fast Library for Approximate Nearest Neighbors) is an approximate nearest neighbor search algorithm that accelerates feature matching by building KD-trees or K-means trees. The FLANN matcher is suitable when the number of feature points is large.

    In OpenCV, you can use thecv::FlannBasedMatcherclass to implement the FLANN matcher:

    Example

    #include

    #include

    int

    main

    (

    )

    {

        cv

    ::

    Mat

    src1

    =

    cv

    ::

    imread

    (

    "image1.jpg"

    , cv

    ::

    IMREAD_GRAYSCALE

    )

    ;

        cv

    ::

    Mat

    src2

    =

    cv

    ::

    imread

    (

    "image2.jpg"

    , cv

    ::

    IMREAD_GRAYSCALE

    )

    ;

        std

    ::

    vector

    <

    cv

    ::

    KeyPoint

    keypoints1, keypoints2

    ;

        cv

    ::

    Mat

    descriptors1, descriptors2

    ;

       

    // SIFT feature point detection

        cv

    ::

    Ptr

    <

    cv

    ::

    xfeatures2d

    ::

    SIFT

    sift

    =

    cv

    ::

    xfeatures2d

    ::

    SIFT

    ::

    create

    (

    )

    ;

        sift

    detectAndCompute

    (

    src1, cv

    ::

    noArray

    (

    )

    , keypoints1, descriptors1

    )

    ;

        sift

    detectAndCompute

    (

    src2, cv

    ::

    noArray

    (

    )

    , keypoints2, descriptors2

    )

    ;

       

    // FLANN matcher

        cv

    ::

    FlannBasedMatcher

    matcher

    ;

        std

    ::

    vector

    <

    cv

    ::

    DMatch

    matches

    ;

        matcher.

    match

    (

    descriptors1, descriptors2, matches

    )

    ;

       

    // Draw the matching results

        cv

    ::

    Mat

    output

    ;

        cv

    ::

    drawMatches

    (

    src1, keypoints1, src2, keypoints2, matches, output

    )

    ;

        cv

    ::

    imshow

    (

    "FLANN Matches"

    , output

    )

    ;

        cv

    ::

    waitKey

    (

    0

    )

    ;

       

    return

    0

    ;

    }

    Feature Point Matching and Filtering

    In practical applications, the results of feature point matching may contain some incorrect matches. To improve matching accuracy, the matching results usually need to be filtered. Common filtering methods include distance-based filtering and geometric-constraint-based filtering.

    Distance-Based Filtering

    Distance-based filtering filters out matches with large distances by setting a threshold. In OpenCV, you can use thecv::DescriptorMatcher::radiusMatchfunction to implement distance-based filtering.

    Example

    #include

    #include

    int

    main

    (

    )

    {

        cv

    ::

    Mat

    src1

    =

    cv

    ::

    imread

    (

    "image1.jpg"

    , cv

    ::

    IMREAD_GRAYSCALE

    )

    ;

        cv

    ::

    Mat

    src2

    =

    cv

    ::

    imread

    (

    "image2.jpg"

    , cv

    ::

    IMREAD_GRAYSCALE

    )

    ;

        std

    ::

    vector

    <

    cv

    ::

    KeyPoint

    keypoints1, keypoints2

    ;

        cv

    ::

    Mat

    descriptors1, descriptors2

    ;

       

    // SIFT feature point detection

        cv

    ::

    Ptr

    <

    cv

    ::

    xfeatures2d

    ::

    SIFT

    sift

    =

    cv

    ::

    xfeatures2d

    ::

    SIFT

    ::

    create

    (

    )

    ;

        sift

    detectAndCompute

    (

    src1, cv

    ::

    noArray

    (

    )

    , keypoints1, descriptors1

    )

    ;

        sift

    detectAndCompute

    (

    src2, cv

    ::

    noArray

    (

    )

    , keypoints2, descriptors2

    )

    ;

       

    // BFMatcher matching

        cv

    ::

    BFMatcher

    matcher

    (

    cv

    ::

    NORM_L2

    )

    ;

        std

    ::

    vector

    <

    std

    ::

    vector

    <

    cv

    ::

    DMatch

    matches

    ;

        matcher.

    radiusMatch

    (

    descriptors1, descriptors2, matches,

    50.0

    )

    ;

       

    // Draw the matching results

        cv

    ::

    Mat

    output

    ;

        cv

    ::

    drawMatches

    (

    src1, keypoints1, src2, keypoints2, matches, output

    )

    ;

        cv

    ::

    imshow

    (

    "Filtered Matches"

    , output

    )

    ;

        cv

    ::

    waitKey

    (

    0

    )

    ;

       

    return

    0

    ;

    }

    Geometric-Constraint-Based Filtering

    Geometric-constraint-based filtering filters out matches that do not satisfy geometric constraints by computing the fundamental matrix or homography matrix. In OpenCV, you can use thecv::findFundamentalMatorcv::findHomographyfunction to implement geometric-constraint-based filtering.

    Example

    #include

    #include

    int

    main

    (

    )

    {

        cv

    ::

    Mat

    src1

    =

    cv

    ::

    imread

    (

    "image1.jpg"

    , cv

    ::

    IMREAD_GRAYSCALE

    )

    ;

        cv

    ::

    Mat

    src2

    =

    cv

    ::

    imread

    (

    "image2.jpg"

    , cv

    ::

    IMREAD_GRAYSCALE

    )

    ;

        std

    ::

    vector

    <

    cv

    ::

    KeyPoint

    keypoints1, keypoints2

    ;

        cv

    ::

    Mat

    descriptors1, descriptors2

    ;

       

    // SIFT feature point detection

        cv

    ::

    Ptr

    <

    cv

    ::

    xfeatures2d

    ::

    SIFT

    sift

    =

    cv

    ::

    xfeatures2d

    ::

    SIFT

    ::

    create

    (

    )

    ;

        sift

    detectAndCompute

    (

    src1, cv

    ::

    noArray

    (

    )

    , keypoints1, descriptors1

    )

    ;

        sift

    detectAndCompute

    (

    src2, cv

    ::

    noArray

    (

    )

    , keypoints2, descriptors2

    )

    ;

       

    // BFMatcher matching

        cv

    ::

    BFMatcher

    matcher

    (

    cv

    ::

    NORM_L2

    )

    ;

        std

    ::

    vector

    <

    cv

    ::

    DMatch

    matches

    ;

        matcher.

    match

    (

    descriptors1, descriptors2, matches

    )

    ;

       

    // Geometric-constraint-based filtering

        std

    ::

    vector

    <

    cv

    ::

    Point2f

    points1, points2

    ;

       

    for

    (

    size_t

    i

    =

    0

    ;

    i

    <

    matches.

    size

    (

    )

    ;

    i

    ++

    )

    {

            points1.

    push_back

    (

    keypoints1

    [

    matches

    [

    i

    ]

    .

    queryIdx

    ]

    .

    pt

    )

    ;

            points2.

    push_back

    (

    keypoints2

    [

    matches

    [

    i

    ]

    .

    trainIdx

    ]

    .

    pt

    )

    ;

       

    }

        cv

    ::

    Mat

    mask

    ;

        cv

    ::

    findHomography

    (

    points1, points2, cv

    ::

    RANSAC

    ,

    3

    , mask

    )

    ;

       

    // Filter the matches

        std

    ::

    vector

    <

    cv

    ::

    DMatch

    good_matches

    ;

       

    for

    (

    size_t

    i

    =

    0

    ;

    i

    <

    matches.

    size

    (

    )

    ;

    i

    ++

    )

    {

           

    if

    (

    mask.

    at

    <
    
    uchar
    
    (
    
    i
    
    )
    
    )
    
    {

                good_matches.

    push_back

    (

    matches

    [

    i

    ]

    )

    ;

           

    }

       

    }

       

    // Draw the matching results

        cv

    ::

    Mat

    output

    ;

        cv

    ::

    drawMatches

    (

    src1, keypoints1, src2, keypoints2, good_matches, output

    )

    ;

        cv

    ::

    imshow

    (

    "Filtered Matches"

    , output

    )

    ;

        cv

    ::

    waitKey

    (

    0

    )

    ;

       

    return

    0

    ;

    }