Skip to content

resolve #11855 - #12908

Merged
opencv-pushbot merged 2 commits into
opencv:3.4from
alexevans:Issue11855
Nov 16, 2018
Merged

opencv-pushbot merged 2 commits into
opencv:3.4from
alexevans:Issue11855

Conversation

@alexevans

@alexevans alexevans commented Oct 24, 2018 •

Copy link
Copy Markdown
Contributor

Merge with extra: opencv/opencv_extra#543

resolves #11855

allow_multiple_commits=1

@alalek

alalek commented Nov 11, 2018

Copy link
Copy Markdown
Member

Please add test for this patch (code from answers doesn't expect empty entries).

@alexevans

Copy link
Copy Markdown
Contributor Author

@alalek
Here's what I used, based on the tests in the original issue and answers post.

#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/features2d.hpp>

using namespace cv;
using namespace std;

int main()
{
Mat targets = (Mat_<uchar>(2, 3) <<  1, 1, 0, 1, 1, 1);
Mat sources = (Mat_<uchar>(2, 3) << 1, 1, 1, 0,0,0);
cout << sources << "\n";
cout << targets << "\n";
cout << "SOURCES TARGETS\n";
    for (int i = 0; i < sources.rows; i++)
        for (int j = 0; j < targets.rows; j++)
            cout << i << " -> " << j << " = " << norm(sources.row(i), targets.row(j), NORM_HAMMING) << "\n";
    cout << "TARGETS SOURCES\n";
    for (int i = 0; i < targets.rows; i++)
        for (int j = 0; j < sources.rows; j++)
            cout << i << " -> " << j << " = " << norm(targets.row(i), sources.row(j), NORM_HAMMING) << "\n";

Ptr<BFMatcher> bf = BFMatcher::create(NORM_HAMMING, true);
vector<vector<DMatch>> match;
cout << "Match with crosscheck TRUE\n";
bf->knnMatch(sources, targets, match, 1);

for (auto m : match)
    if(m.size() > 0)
        cout << m[0].queryIdx << " -> " << m[0].trainIdx << " = " << m[0].distance << "\n";

Ptr<BFMatcher> bf2 = BFMatcher::create(NORM_HAMMING, false);
cout << "Match with crosscheck FALSE \n";
match.clear();
bf2->knnMatch(sources, targets, match, 1);

for (auto m : match)
    cout << m[0].queryIdx << " -> " << m[0].trainIdx << " = " << m[0].distance << "\n";
return 0;
}
import numpy
import cv2

targets = numpy.array([[1, 1, 0], [1, 1, 1]], dtype=numpy.uint8)
sources = numpy.array([[1, 1, 1], [0, 0, 0]], dtype=numpy.uint8)
for si, source in enumerate(sources):
    for ti, target in enumerate(targets):
        print('%d -> %d: %d' % (si, ti, cv2.norm(source, target, cv2.NORM_HAMMING)))

bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.knnMatch(sources, targets, k=1)
print('-- crosscheck=True')
for match in matches:
    if match:
        print('%d -> %d: %f' % (match[0].queryIdx, match[0].trainIdx, match[0].distance))

bf_nocc = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=False)
matches_nocc = bf_nocc.knnMatch(sources, targets, k=1)
print('-- crosscheck=False')
for match in matches_nocc:
    if match:
        print('%d -> %d: %f' % (match[0].queryIdx, match[0].trainIdx, match[0].distance))

@alalek

alalek commented Nov 16, 2018

Copy link
Copy Markdown
Member

Proposed code works without this patch.
Looks like there is mess with sources/targets values above.

Added the simple test to this patch.

@alalek alalek left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! Thank you 👍

@opencv-pushbot
opencv-pushbot merged commit a68835f into opencv:3.4 Nov 16, 2018
@alalek alalek mentioned this pull request Nov 17, 2018
savuor pushed a commit to nickyu-zhu/opencv that referenced this pull request Oct 27, 2023
SavyaSanchi-Sharma pushed a commit to SavyaSanchi-Sharma/opencv that referenced this pull request Jul 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants