New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
resolve #11855 #12908
resolve #11855 #12908
Conversation
Please add test for this patch (code from answers doesn't expect empty entries). |
@alalek #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)) |
Proposed code works without this patch. Added the simple test to this patch. |
There was a problem hiding this comment.
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 👍
Merge with extra: opencv/opencv_extra#543
resolves #11855