From e30bef5dcdf0e788cb2cf89240ec78871afe71f7 Mon Sep 17 00:00:00 2001 From: Mykhailo Bilyi Date: Thu, 7 Sep 2023 13:36:10 +0200 Subject: [PATCH] Add Match method for BFMatcher --- features2d.cpp | 13 +++++++++++++ features2d.go | 12 ++++++++++++ features2d.h | 1 + features2d_test.go | 5 +++++ 4 files changed, 31 insertions(+) diff --git a/features2d.cpp b/features2d.cpp index f7b18e30..0ccce5f8 100644 --- a/features2d.cpp +++ b/features2d.cpp @@ -380,6 +380,19 @@ void BFMatcher_Close(BFMatcher b) { delete b; } +struct DMatches BFMatcher_Match(BFMatcher b, Mat query, Mat train) { + std::vector 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 > matches; (*b)->knnMatch(*query, *train, matches, k); diff --git a/features2d.go b/features2d.go index 162b604f..4a0fffd7 100644 --- a/features2d.go +++ b/features2d.go @@ -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: diff --git a/features2d.h b/features2d.h index b6a9c4fc..4042a0e3 100644 --- a/features2d.h +++ b/features2d.h @@ -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(); diff --git a/features2d_test.go b/features2d_test.go index bc16ed25..cc8e8d69 100644 --- a/features2d_test.go +++ b/features2d_test.go @@ -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()