Skip to content

Commit 48b5ded

Browse files
authored
Merge pull request #3632 from troelsy:4.x
Fix a bug in knnMatchConvert when a feature couldn't be matched #3632 After I started using a mask with `knnMatchAsync`, I found that the result from `knnMatchConvert` would be clipped at random. Investigating the issue, I found that `knnMatchAsync` will initialize all `trainIdx` to `-1`, which will be overwritten by the CUDA kernel. A mask can be used to prevent certain features from being matched and this will prevent the CUDA kernel from setting the match distance. `knnMatchConvert` is not properly incrementing the pointers when `trainIdx == -1`, so an unmatched feature will get it stuck at `if (trainIdx == -1)`. Eventually the outer for-loop finishes and returns a vector with the matches up until the first missing match distance. My solution is to increment the counters the same way as a successful iteration would. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake
1 parent c950656 commit 48b5ded

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

modules/cudafeatures2d/src/brute_force_matcher.cpp

+7-9
Original file line numberDiff line numberDiff line change
@@ -791,15 +791,13 @@ namespace
791791
for (int i = 0; i < k; ++i)
792792
{
793793
const int trainIdx = *trainIdxPtr;
794-
if (trainIdx == -1)
795-
continue;
796-
797-
const int imgIdx = imgIdxPtr ? *imgIdxPtr : 0;
798-
const float distance = *distancePtr;
799-
800-
DMatch m(queryIdx, trainIdx, imgIdx, distance);
801-
802-
curMatches.push_back(m);
794+
if (trainIdx != -1)
795+
{
796+
const int imgIdx = imgIdxPtr ? *imgIdxPtr : 0;
797+
const float distance = *distancePtr;
798+
DMatch m(queryIdx, trainIdx, imgIdx, distance);
799+
curMatches.push_back(m);
800+
}
803801

804802
++trainIdxPtr;
805803
++distancePtr;

0 commit comments

Comments
 (0)