Skip to content

Commit

Permalink
Add Match method for BFMatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
mishabeliy15 authored and deadprogram committed Oct 6, 2023
1 parent 579545a commit e30bef5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
13 changes: 13 additions & 0 deletions features2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,19 @@ void BFMatcher_Close(BFMatcher b) {
delete b;
}

struct DMatches BFMatcher_Match(BFMatcher b, Mat query, Mat train) {
std::vector<cv::DMatch> matches;
(*b)->match(*query, *train, matches);

DMatch *dmatches = new DMatch[matches.size()];
for (size_t i = 0; i < matches.size(); ++i) {
DMatch dmatch = {matches[i].queryIdx, matches[i].trainIdx, matches[i].imgIdx, matches[i].distance};
dmatches[i] = dmatch;
}
DMatches ret = {dmatches, (int) matches.size()};
return ret;
}

struct MultiDMatches BFMatcher_KnnMatch(BFMatcher b, Mat query, Mat train, int k) {
std::vector< std::vector<cv::DMatch> > matches;
(*b)->knnMatch(*query, *train, matches, k);
Expand Down
12 changes: 12 additions & 0 deletions features2d.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,18 @@ func (b *BFMatcher) Close() error {
return nil
}

// Match Finds the best match for each descriptor from a query set.
//
// For further details, please see:
// https://docs.opencv.org/4.x/db/d39/classcv_1_1DescriptorMatcher.html#a0f046f47b68ec7074391e1e85c750cba
//
func (b *BFMatcher) Match(query, train Mat) []DMatch {
ret := C.BFMatcher_Match((C.BFMatcher)(b.p), query.p, train.p)
defer C.DMatches_Close(ret)

return getDMatches(ret)
}

// KnnMatch Finds the k best matches for each descriptor from a query set.
//
// For further details, please see:
Expand Down
1 change: 1 addition & 0 deletions features2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ SimpleBlobDetectorParams SimpleBlobDetectorParams_Create();
BFMatcher BFMatcher_Create();
BFMatcher BFMatcher_CreateWithParams(int normType, bool crossCheck);
void BFMatcher_Close(BFMatcher b);
struct DMatches BFMatcher_Match(BFMatcher b, Mat query, Mat train);
struct MultiDMatches BFMatcher_KnnMatch(BFMatcher b, Mat query, Mat train, int k);

FlannBasedMatcher FlannBasedMatcher_Create();
Expand Down
5 changes: 5 additions & 0 deletions features2d_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,11 @@ func TestBFMatcher(t *testing.T) {
}
}

matches := bf.Match(desc1, desc2)
if len(matches) != 890 {
t.Errorf("Matches was excepted to have 890 elements, but it has %d", len(matches))
}

bfParams := NewBFMatcherWithParams(NormHamming, false)
defer bfParams.Close()

Expand Down

0 comments on commit e30bef5

Please sign in to comment.