Skip to content

Commit d2469ed

Browse files
committed
Merge pull request opencv#17772 from mshabunin:fix-match-umat-mask
2 parents 5bc6b6f + 4dd9a36 commit d2469ed

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

modules/features2d/src/matchers.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -622,15 +622,20 @@ void DescriptorMatcher::checkMasks( InputArrayOfArrays _masks, int queryDescript
622622
if( isMaskSupported() && !masks.empty() )
623623
{
624624
// Check masks
625-
size_t imageCount = std::max(trainDescCollection.size(), utrainDescCollection.size() );
625+
const size_t imageCount = std::max(trainDescCollection.size(), utrainDescCollection.size() );
626626
CV_Assert( masks.size() == imageCount );
627627
for( size_t i = 0; i < imageCount; i++ )
628628
{
629-
if( !masks[i].empty() && (!trainDescCollection[i].empty() || !utrainDescCollection[i].empty() ) )
629+
if (masks[i].empty())
630+
continue;
631+
const bool hasTrainDesc = !trainDescCollection.empty() && !trainDescCollection[i].empty();
632+
const bool hasUTrainDesc = !utrainDescCollection.empty() && !utrainDescCollection[i].empty();
633+
if (hasTrainDesc || hasUTrainDesc)
630634
{
631-
int rows = trainDescCollection[i].empty() ? utrainDescCollection[i].rows : trainDescCollection[i].rows;
632-
CV_Assert( masks[i].rows == queryDescriptorsCount &&
633-
masks[i].cols == rows && masks[i].type() == CV_8UC1);
635+
const int rows = hasTrainDesc ? trainDescCollection[i].rows : utrainDescCollection[i].rows;
636+
CV_Assert(masks[i].type() == CV_8UC1
637+
&& masks[i].rows == queryDescriptorsCount
638+
&& masks[i].cols == rows);
634639
}
635640
}
636641
}

modules/features2d/test/test_matchers_algorithmic.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,6 @@ TEST(Features2d_DMatch, issue_11855)
565565
1, 1, 1);
566566
Mat targets = (Mat_<uchar>(2, 3) << 1, 1, 1,
567567
0, 0, 0);
568-
569568
Ptr<BFMatcher> bf = BFMatcher::create(NORM_HAMMING, true);
570569
vector<vector<DMatch> > match;
571570
bf->knnMatch(sources, targets, match, 1, noArray(), true);
@@ -577,4 +576,18 @@ TEST(Features2d_DMatch, issue_11855)
577576
EXPECT_EQ(0.0f, match[0][0].distance);
578577
}
579578

579+
TEST(Features2d_DMatch, issue_17771)
580+
{
581+
Mat sources = (Mat_<uchar>(2, 3) << 1, 1, 0,
582+
1, 1, 1);
583+
Mat targets = (Mat_<uchar>(2, 3) << 1, 1, 1,
584+
0, 0, 0);
585+
UMat usources = sources.getUMat(ACCESS_READ);
586+
UMat utargets = targets.getUMat(ACCESS_READ);
587+
vector<vector<DMatch> > match;
588+
Ptr<BFMatcher> ubf = BFMatcher::create(NORM_HAMMING);
589+
Mat mask = (Mat_<uchar>(2, 2) << 1, 0, 0, 1);
590+
EXPECT_NO_THROW(ubf->knnMatch(usources, utargets, match, 1, mask, true));
591+
}
592+
580593
}} // namespace

0 commit comments

Comments
 (0)