From ae8c90301ff93f64fd6d5a0f0399b51e70996eb0 Mon Sep 17 00:00:00 2001 From: Alexander Smorkalov Date: Wed, 17 May 2023 11:02:01 +0300 Subject: [PATCH] Fixed mask handling in AffineFeature. --- modules/features2d/src/affine_feature.cpp | 4 +++- .../features2d/test/test_affine_feature.cpp | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/modules/features2d/src/affine_feature.cpp b/modules/features2d/src/affine_feature.cpp index 40e03e92ba..5d19d6c6c3 100644 --- a/modules/features2d/src/affine_feature.cpp +++ b/modules/features2d/src/affine_feature.cpp @@ -243,7 +243,7 @@ private: else mask0 = mask; pose = Matx23f(1,0,0, - 0,1,0); + 0,1,0); if( phi == 0 ) image.copyTo(rotImage); @@ -276,6 +276,8 @@ private: } if( phi != 0 || tilt != 1 ) warpAffine(mask0, warpedMask, pose, warpedImage.size(), INTER_NEAREST); + else + warpedMask = mask0; } diff --git a/modules/features2d/test/test_affine_feature.cpp b/modules/features2d/test/test_affine_feature.cpp index f40f21ed8d..75885bd751 100644 --- a/modules/features2d/test/test_affine_feature.cpp +++ b/modules/features2d/test/test_affine_feature.cpp @@ -182,4 +182,26 @@ TEST(Features2d_AFFINE_FEATURE, regression) #endif } +TEST(Features2d_AFFINE_FEATURE, mask) +{ + Mat gray = imread(cvtest::findDataFile("features2d/tsukuba.png"), IMREAD_GRAYSCALE); + ASSERT_FALSE(gray.empty()) << "features2d/tsukuba.png image was not found in test data!"; + + // small tilt range to limit internal mask warping + Ptr ext = AffineFeature::create(SIFT::create(), 1, 0); + Mat mask = Mat::zeros(gray.size(), CV_8UC1); + mask(Rect(50, 50, mask.cols-100, mask.rows-100)).setTo(255); + + // calc and compare keypoints + vector calcKeypoints; + ext->detectAndCompute(gray, mask, calcKeypoints, noArray(), false); + + // added expanded test range to cover sub-pixel coordinates for features on mask border + for( size_t i = 0; i < calcKeypoints.size(); i++ ) + { + ASSERT_TRUE((calcKeypoints[i].pt.x >= 50-1) && (calcKeypoints[i].pt.x <= mask.cols-50+1)); + ASSERT_TRUE((calcKeypoints[i].pt.y >= 50-1) && (calcKeypoints[i].pt.y <= mask.rows-50+1)); + } +} + }} // namespace