From 2bf4f5c151eef7e786f5903eff4a010e800bf288 Mon Sep 17 00:00:00 2001 From: Skreg <85214856+shyama7004@users.noreply.github.com> Date: Tue, 9 Dec 2025 17:01:53 +0530 Subject: [PATCH] Merge pull request #26366 from shyama7004:fix-orb.cpp Fix ORB inconsistency for masks with values 255 and 1. #26366 ### Pull Request Readiness Checklist The PR fixes : [25974](https://github.com/opencv/opencv/issues/25974) 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 - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake --- modules/features2d/src/orb.cpp | 8 ++++++-- modules/features2d/test/test_orb.cpp | 27 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/modules/features2d/src/orb.cpp b/modules/features2d/src/orb.cpp index b0d32002a1..f970818377 100644 --- a/modules/features2d/src/orb.cpp +++ b/modules/features2d/src/orb.cpp @@ -1036,7 +1036,11 @@ void ORB_Impl::detectAndCompute( InputArray _image, InputArray _mask, bool useOCL = false; #endif - Mat image = _image.getMat(), mask = _mask.getMat(); + Mat image = _image.getMat(), mask; + if (!_mask.empty()) + { + threshold(_mask, mask, 0, 255, THRESH_BINARY); + } if( image.type() != CV_8UC1 ) cvtColor(_image, image, COLOR_BGR2GRAY); @@ -1134,7 +1138,7 @@ void ORB_Impl::detectAndCompute( InputArray _image, InputArray _mask, } copyMakeBorder(currImg, extImg, border, border, border, border, - BORDER_REFLECT_101+BORDER_ISOLATED); + BORDER_REFLECT_101 + BORDER_ISOLATED); if (!mask.empty()) copyMakeBorder(currMask, extMask, border, border, border, border, BORDER_CONSTANT+BORDER_ISOLATED); diff --git a/modules/features2d/test/test_orb.cpp b/modules/features2d/test/test_orb.cpp index 89e2f7d78b..92fe8e9838 100644 --- a/modules/features2d/test/test_orb.cpp +++ b/modules/features2d/test/test_orb.cpp @@ -168,4 +168,31 @@ BIGDATA_TEST(Features2D_ORB, regression_opencv_python_537) // memory usage: ~3 ASSERT_NO_THROW(orbPtr->detectAndCompute(img, noArray(), kps, fv)); } +TEST(Features2D_ORB, MaskValue) +{ + Mat gray = imread(cvtest::findDataFile("features2d/tsukuba.png"), IMREAD_GRAYSCALE); + ASSERT_FALSE(gray.empty()); + + cv::Rect roi(gray.cols/4, gray.rows/4, gray.cols/2, gray.rows/2); + + Mat mask255 = Mat::zeros(gray.size(), CV_8UC1); + Mat mask1 = Mat::zeros(gray.size(), CV_8UC1); + mask255(roi).setTo(255); + mask1(roi).setTo(1); + + Ptr orb = cv::ORB::create(); + + vector keypoints_mask255, keypoints_mask1; + Mat descriptors_mask255, descriptors_mask1; + + orb->detectAndCompute(gray, mask255, keypoints_mask255, descriptors_mask255, false); + orb->detectAndCompute(gray, mask1, keypoints_mask1, descriptors_mask1, false); + + ASSERT_EQ(keypoints_mask255.size(), keypoints_mask1.size()) + << "Number of keypoints differs between mask values 255 and 1"; + + Mat diff = descriptors_mask255 != descriptors_mask1; + ASSERT_EQ(countNonZero(diff), 0); +} + }} // namespace