Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to use DescriptorMatcher to get matching images?

I have some images, and I want to use DescriptorMatcher to get matching images, such as load 1.png, 2.png, 3..png, and then match to 1,2,3.

Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create(DescriptorMatcher::FLANNBASED);
std::vector<std::vector<DMatch> > knn_matches;
for (int i = 0; i < trainingImages.size(); i++) {
    std::vector<KeyPoint> keypoints;
    Mat descriptors;
    detector->detectAndCompute(trainingImages.at(i), noArray(), keypoints,
            descriptors);
    if (descriptors.type() != CV_32F) {
        descriptors.convertTo(descriptors, CV_32F);
    }
    matcher->add(descriptors);
}
matcher->train();
Mat re;
matcher->knnMatch(descriptors1, knn_matches, 2, re);

the result re is []

How to use DescriptorMatcher to get matching images?

I have some images, and I want to use DescriptorMatcher to get matching images, such as load 1.png, 2.png, 3..png, and then match to 1,2,3.

Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create(DescriptorMatcher::FLANNBASED);
std::vector<std::vector<DMatch> > knn_matches;
for (int i = 0; i < trainingImages.size(); i++) {
    std::vector<KeyPoint> keypoints;
    Mat descriptors;
    detector->detectAndCompute(trainingImages.at(i), noArray(), keypoints,
            descriptors);
    if (descriptors.type() != CV_32F) {
        descriptors.convertTo(descriptors, CV_32F);
    }
    matcher->add(descriptors);
}
matcher->train();
Mat re;
matcher->knnMatch(descriptors1, knn_matches, 2, re);

the result re is []

---------------------------update-------------------------------------

code:

//-- Step 1: Detect the keypoints using SURF Detector, compute the descriptors
Ptr<ORB> detector = ORB::create();
std::vector<KeyPoint> keypoints1;
Mat descriptors1;
Mat img1 = imread("3.jpg", IMREAD_GRAYSCALE);
detector->detectAndCompute(img1, noArray(), keypoints1, descriptors1);
//-- Step 2: Matching descriptor vectors with a FLANN based matcher
Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create(
        DescriptorMatcher::BRUTEFORCE_HAMMING);
std::vector<std::vector<DMatch> > knn_matches;
for (int i = 0; i < trainingImages.size(); i++) {
    std::vector<KeyPoint> keypoints;
    Mat descriptors;
    detector->detectAndCompute(trainingImages.at(i), noArray(), keypoints,
            descriptors);
    matcher->add(descriptors);
}
matcher->train();
Mat reponse;
matcher->knnMatch(descriptors1, knn_matches, 2, reponse);
cout << reponse << endl;
//-- Filter matches using the Lowe's ratio test
const float ratio_thresh = 0.7f;
std::vector<DMatch> good_matches;
// 过滤
for (size_t i = 0; i < knn_matches.size(); i++) {
    if (knn_matches[i][0].distance
            < ratio_thresh * knn_matches[i][1].distance) {
        good_matches.push_back(knn_matches[i][0]);
    }
}
cout << "ORB Matching Results" << endl;
cout << "*******************************" << endl;
cout << "# Matches:                            \t" << good_matches.size()
        << endl;
cout << endl;

output:

[]
ORB Matching Results
*******************************
# Matches:                              102

How to use DescriptorMatcher to get matching images?

I have some images, and I want to use DescriptorMatcher to get matching images, such as load 1.png, 2.png, 3..png, and then match to 1,2,3.

Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create(DescriptorMatcher::FLANNBASED);
std::vector<std::vector<DMatch> > knn_matches;
for (int i = 0; i < trainingImages.size(); i++) {
    std::vector<KeyPoint> keypoints;
    Mat descriptors;
    detector->detectAndCompute(trainingImages.at(i), noArray(), keypoints,
            descriptors);
    if (descriptors.type() != CV_32F) {
        descriptors.convertTo(descriptors, CV_32F);
    }
    matcher->add(descriptors);
}
matcher->train();
Mat re;
matcher->knnMatch(descriptors1, knn_matches, 2, re);

the result re is []

---------------------------update-------------------------------------

code:

//-- Step 1: Detect the keypoints using SURF Detector, compute the descriptors
Ptr<ORB> detector = ORB::create();
std::vector<KeyPoint> keypoints1;
Mat descriptors1;
Mat img1 = imread("3.jpg", IMREAD_GRAYSCALE);
detector->detectAndCompute(img1, noArray(), keypoints1, descriptors1);
//-- Step 2: Matching descriptor vectors with a FLANN based matcher
Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create(
        DescriptorMatcher::BRUTEFORCE_HAMMING);
std::vector<std::vector<DMatch> > knn_matches;
for (int i = 0; i < trainingImages.size(); i++) {
    std::vector<KeyPoint> keypoints;
    Mat descriptors;
    detector->detectAndCompute(trainingImages.at(i), noArray(), keypoints,
            descriptors);
    matcher->add(descriptors);
}
matcher->train();
// Mat reponse;
matcher->knnMatch(descriptors1, knn_matches, 2, reponse);
2);
// cout << reponse << endl;
//-- Filter matches using the Lowe's ratio test
const float ratio_thresh = 0.7f;
std::vector<DMatch> good_matches;
// 过滤
for (size_t i = 0; i < knn_matches.size(); i++) {
    if (knn_matches[i][0].distance
            < ratio_thresh * knn_matches[i][1].distance) {
        good_matches.push_back(knn_matches[i][0]);
    }
}
cout << "ORB Matching Results" << endl;
cout << "*******************************" << endl;
cout << "# Matches:                            \t" << good_matches.size()
        << endl;
cout << endl;

output:

[]
ORB Matching Results
*******************************
# Matches:                              102