diff --git a/modules/features2d/src/matchers.cpp b/modules/features2d/src/matchers.cpp index d39afe1ade..ee6e15bb31 100644 --- a/modules/features2d/src/matchers.cpp +++ b/modules/features2d/src/matchers.cpp @@ -622,15 +622,20 @@ void DescriptorMatcher::checkMasks( InputArrayOfArrays _masks, int queryDescript if( isMaskSupported() && !masks.empty() ) { // Check masks - size_t imageCount = std::max(trainDescCollection.size(), utrainDescCollection.size() ); + const size_t imageCount = std::max(trainDescCollection.size(), utrainDescCollection.size() ); CV_Assert( masks.size() == imageCount ); for( size_t i = 0; i < imageCount; i++ ) { - if( !masks[i].empty() && (!trainDescCollection[i].empty() || !utrainDescCollection[i].empty() ) ) + if (masks[i].empty()) + continue; + const bool hasTrainDesc = !trainDescCollection.empty() && !trainDescCollection[i].empty(); + const bool hasUTrainDesc = !utrainDescCollection.empty() && !utrainDescCollection[i].empty(); + if (hasTrainDesc || hasUTrainDesc) { - int rows = trainDescCollection[i].empty() ? utrainDescCollection[i].rows : trainDescCollection[i].rows; - CV_Assert( masks[i].rows == queryDescriptorsCount && - masks[i].cols == rows && masks[i].type() == CV_8UC1); + const int rows = hasTrainDesc ? trainDescCollection[i].rows : utrainDescCollection[i].rows; + CV_Assert(masks[i].type() == CV_8UC1 + && masks[i].rows == queryDescriptorsCount + && masks[i].cols == rows); } } } diff --git a/modules/features2d/test/test_matchers_algorithmic.cpp b/modules/features2d/test/test_matchers_algorithmic.cpp index a7116e9bc3..01d08ffe02 100644 --- a/modules/features2d/test/test_matchers_algorithmic.cpp +++ b/modules/features2d/test/test_matchers_algorithmic.cpp @@ -565,7 +565,6 @@ TEST(Features2d_DMatch, issue_11855) 1, 1, 1); Mat targets = (Mat_(2, 3) << 1, 1, 1, 0, 0, 0); - Ptr bf = BFMatcher::create(NORM_HAMMING, true); vector > match; bf->knnMatch(sources, targets, match, 1, noArray(), true); @@ -577,4 +576,18 @@ TEST(Features2d_DMatch, issue_11855) EXPECT_EQ(0.0f, match[0][0].distance); } +TEST(Features2d_DMatch, issue_17771) +{ + Mat sources = (Mat_(2, 3) << 1, 1, 0, + 1, 1, 1); + Mat targets = (Mat_(2, 3) << 1, 1, 1, + 0, 0, 0); + UMat usources = sources.getUMat(ACCESS_READ); + UMat utargets = targets.getUMat(ACCESS_READ); + vector > match; + Ptr ubf = BFMatcher::create(NORM_HAMMING); + Mat mask = (Mat_(2, 2) << 1, 0, 0, 1); + EXPECT_NO_THROW(ubf->knnMatch(usources, utargets, match, 1, mask, true)); +} + }} // namespace