Merge pull request #29679 from Neal006/features:keypoint-bool-mask

features: accept CV_Bool masks in SIFT detect (#25895) - #29679

### Problem

`cv::Mat_<bool>::depth()` returns `CV_Bool` in 5.0, where it returned `CV_8U` in 4.x. `SIFT_Impl::detectAndCompute` gates the mask at `sift.dispatch.cpp:980`, so a boolean detection mask now fails with:

```
(-5:Bad argument) mask has incorrect type (!=CV_8UC1) in function 'cv::SIFT_Impl::detectAndCompute'
```

### Fix

Allow `CV_BoolC1` in that check, and update the message accordingly.

No conversion is needed. The mask is consumed by `KeyPointsFilter::runByPixelsMask`, whose `MaskPredicate` reads it with `Mat::at<uchar>`, and `Mat::at` only asserts that the element size matches (`CV_ELEM_SIZE1(traits::Depth<_Tp>::value) == elemSize1()`). `CV_Bool` is one byte like `CV_8U`, so the existing read is already correct for a boolean mask.

That is also why `FastFeatureDetector` and `SimpleBlobDetector`, which have no explicit mask type check and route through the same predicate, already accepted boolean masks. The only thing standing in the way was SIFT's own check.

This follows the same approach `goodFeaturesToTrack` already uses in this module (`featureselect.cpp:301` accepts `CV_8UC1 || CV_BoolC1`).

### Test

`Features2d_Detector_Keypoints_BoolMask.matches_uchar_mask` builds a synthetic image with circles and rectangles, runs FAST, SIFT and SimpleBlobDetector with a `Mat_<bool>` mask and with the equivalent `CV_8UC1` mask, and requires the same keypoints from each.

Verified locally on 5.x: the test fails without the change (SIFT throws the error above) and passes with it. FAST and SimpleBlobDetector are included to pin their already-working behaviour against future regressions. No test data needed, the test is synthetic.

Part of #25895.

### Pull Request Readiness Checklist

- [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
This commit is contained in:
Neal Daftary
2026-08-14 10:31:38 +05:30
committed by GitHub
parent 5846edaa83
commit 54b61d806e
2 changed files with 38 additions and 2 deletions

View File

@@ -977,8 +977,8 @@ void SIFT_Impl::detectAndCompute(InputArray _image, InputArray _mask,
if( image.empty() || image.depth() != CV_8U )
CV_Error( Error::StsBadArg, "image is empty or has incorrect depth (!=CV_8U)" );
if( !mask.empty() && mask.type() != CV_8UC1 )
CV_Error( Error::StsBadArg, "mask has incorrect type (!=CV_8UC1)" );
if( !mask.empty() && mask.type() != CV_8UC1 && mask.type() != CV_BoolC1 )
CV_Error( Error::StsBadArg, "mask has incorrect type (!=CV_8UC1 && !=CV_BoolC1)" );
if( useProvidedKeypoints )
{

View File

@@ -155,4 +155,40 @@ TEST(Features2d_Detector_Keypoints_SIFT, validation)
}
// See https://github.com/opencv/opencv/issues/25895
typedef Ptr<FeatureDetector> (*DetectorFactory)();
typedef testing::TestWithParam<DetectorFactory> Features2d_Detector_Keypoints_BoolMask;
TEST_P(Features2d_Detector_Keypoints_BoolMask, matches_uchar_mask)
{
Mat image = imread(cvtest::findDataFile(FEATURES2D_DIR + "/" + IMAGE_FILENAME), IMREAD_GRAYSCALE);
ASSERT_FALSE(image.empty());
const Rect roi(image.cols / 4, image.rows / 4, image.cols / 2, image.rows / 2);
Mat_<bool> mask_bool(image.size(), false);
mask_bool(roi) = true;
ASSERT_EQ(mask_bool.depth(), CV_Bool);
Mat mask_uchar(image.size(), CV_8UC1, Scalar::all(0));
mask_uchar(roi) = 255;
Ptr<FeatureDetector> detector = GetParam()();
std::vector<KeyPoint> kp_bool, kp_uchar;
ASSERT_NO_THROW(detector->detect(image, kp_bool, mask_bool));
detector->detect(image, kp_uchar, mask_uchar);
ASSERT_FALSE(kp_uchar.empty());
ASSERT_EQ(kp_bool.size(), kp_uchar.size());
for (size_t k = 0; k < kp_bool.size(); k++)
EXPECT_EQ(kp_bool[k].pt, kp_uchar[k].pt) << "keypoint " << k;
}
INSTANTIATE_TEST_CASE_P(FAST, Features2d_Detector_Keypoints_BoolMask,
Values([]() -> Ptr<FeatureDetector> { return FastFeatureDetector::create(); }));
INSTANTIATE_TEST_CASE_P(SIFT, Features2d_Detector_Keypoints_BoolMask,
Values([]() -> Ptr<FeatureDetector> { return SIFT::create(); }));
}} // namespace