Ask Your Question
0

How can I increase the patch size of keypoints?

asked 2015-12-07 18:58:57 -0600

cvlia gravatar image

I'm using Akaze detectors and descriptors to build a BOW codebook, but the detected keypoints are too small to be discriminative, often containing only a vertical line or a patch of color. I'd like to use larger patches, but I can't figure out how to pass this parameter to the Akaze detector.

For example, using the tutorial code (below), how can I include keypoints which have a radius of at least 15px?

#include <opencv2/features2d.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/opencv.hpp>
#include <vector>
#include <iostream>

using namespace std;
using namespace cv;

const float inlier_threshold = 2.5f; // Distance threshold to identify inliers
const float nn_match_ratio = 0.8f;   // Nearest neighbor matching ratio

int main(void)
{
    Mat img1 = imread("../data/graf1.png", IMREAD_GRAYSCALE);
    Mat img2 = imread("../data/graf3.png", IMREAD_GRAYSCALE);

    Mat homography;
    FileStorage fs("../data/H1to3p.xml", FileStorage::READ);
    fs.getFirstTopLevelNode() >> homography;

    vector<KeyPoint> kpts1, kpts2;
    Mat desc1, desc2;

    Ptr<AKAZE> akaze = AKAZE::create();
    akaze->detectAndCompute(img1, noArray(), kpts1, desc1);
    akaze->detectAndCompute(img2, noArray(), kpts2, desc2);

    BFMatcher matcher(NORM_HAMMING);
    vector< vector<DMatch> > nn_matches;
    matcher.knnMatch(desc1, desc2, nn_matches, 2);

    vector<KeyPoint> matched1, matched2, inliers1, inliers2;
    vector<DMatch> good_matches;
    for(size_t i = 0; i < nn_matches.size(); i++) {
        DMatch first = nn_matches[i][0];
        float dist1 = nn_matches[i][0].distance;
        float dist2 = nn_matches[i][1].distance;

        if(dist1 < nn_match_ratio * dist2) {
            matched1.push_back(kpts1[first.queryIdx]);
            matched2.push_back(kpts2[first.trainIdx]);
        }
    }

    for(unsigned i = 0; i < matched1.size(); i++) {
        Mat col = Mat::ones(3, 1, CV_64F);
        col.at<double>(0) = matched1[i].pt.x;
        col.at<double>(1) = matched1[i].pt.y;

        col = homography * col;
        col /= col.at<double>(2);
        double dist = sqrt( pow(col.at<double>(0) - matched2[i].pt.x, 2) +
                            pow(col.at<double>(1) - matched2[i].pt.y, 2));

        if(dist < inlier_threshold) {
            int new_i = static_cast<int>(inliers1.size());
            inliers1.push_back(matched1[i]);
            inliers2.push_back(matched2[i]);
            good_matches.push_back(DMatch(new_i, new_i, 0));
        }
    }

    Mat res;
    drawMatches(img1, inliers1, img2, inliers2, good_matches, res);
    imwrite("res.png", res);

    double inlier_ratio = inliers1.size() * 1.0 / matched1.size();
    cout << "A-KAZE Matching Results" << endl;
    cout << "*******************************" << endl;
    cout << "# Keypoints 1:                        \t" << kpts1.size() << endl;
    cout << "# Keypoints 2:                        \t" << kpts2.size() << endl;
    cout << "# Matches:                            \t" << matched1.size() << endl;
    cout << "# Inliers:                            \t" << inliers1.size() << endl;
    cout << "# Inliers Ratio:                      \t" << inlier_ratio << endl;
    cout << endl;

    return 0;
}
edit retag flag offensive close merge delete

Comments

1

Are you sure your regions are too small? By default drawMatches just draw points, not full patches. Use DRAW_RICH_KEYPOINTS flag for drawMatches(...). See docs.

Vit gravatar imageVit ( 2015-12-08 19:05:19 -0600 )edit

@Vit DRAW_RICH_KEYPOINTS does show more variation in patch size. Thanks for the tip.

cvlia gravatar imagecvlia ( 2015-12-09 15:15:20 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
1

answered 2015-12-08 18:53:18 -0600

As I can see from source code, you can fill options for constructor AKAZEFeatures::AKAZEFeatures(const AKAZEOptions& options) and change its descriptor_pattern_size which is set by default to 10 ( see AKAZEConfig.h )

edit flag offensive delete link more
0

answered 2015-12-08 14:40:01 -0600

LBerger gravatar image

You can use method setDescriptorSize after AKAZE::Create After descriptor_size is used like this in source code

edit flag offensive delete link more

Comments

It is size in bits (not pixels), and it is set to max by default: descriptor_size Size of the descriptor in bits. 0 -> Full size

Vit gravatar imageVit ( 2015-12-08 18:18:27 -0600 )edit

Isn't this the dimension of the descriptor, not the size of the keypoint?

cvlia gravatar imagecvlia ( 2015-12-09 15:13:21 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-12-07 18:58:57 -0600

Seen: 1,098 times

Last updated: Dec 08 '15