Repository navigation
features: accept CV_Bool masks in SIFT detect (#25895) - #29679
Conversation
cv::Mat_<bool>::depth() returns CV_Bool in 5.0, where it returned CV_8U in 4.x, so SIFT::detect with a boolean mask now fails: (-5:Bad argument) mask has incorrect type (!=CV_8UC1) Allow CV_BoolC1 in that check. No conversion is needed: the mask reaches KeyPointsFilter::runByPixelsMask, whose MaskPredicate reads it with Mat::at<uchar>, and Mat::at only asserts that the element size matches. CV_Bool is one byte like CV_8U, so the existing read is already correct. FAST and SimpleBlobDetector have no explicit mask type check and route through the same predicate, so they already accepted boolean masks. The test covers all three to keep that behaviour pinned. Added a regression test comparing a Mat_<bool> mask against the equivalent CV_8UC1 mask and requiring the same keypoints from each detector.
|
@asmorkalov Both points addressed in 9a23bfe. The test now reads features2d/tsukuba.png from opencv_extra, the same input the rest of test_keypoints.cpp already uses, with the mask covering the centre half of the image. The redundant comment above the test is gone. Switching to a real image turned up something worth mentioning: your reliability concern was well founded. Once I added I briefly tried ORB as a third detector and it throws Rebuilt and reran: the test passes, and the negative test is reconfirmed, without the sift.dispatch.cpp change it fails with |
Address review feedback: the synthetic test image was too small and had too few features to exercise SIFT reliably, so switch to features2d/tsukuba.png from opencv_extra, the input the rest of this file already uses. Also drop the redundant comment above the test and assert that the reference CV_8U mask actually yields keypoints, so the bool-vs-uchar comparison cannot pass vacuously. SimpleBlobDetector finds nothing on tsukuba, so it is removed from the detector list.
04f4f7d to
9a23bfe
Compare
|
Please do not touch ORB for now. It builds pyramid of masks and does some other manipulations that are not supported for bool type yet. |
|
@asmorkalov Understood, I will leave ORB alone and will not open the follow-up I mentioned. To be clear about the current state of this PR: ORB is already out of it. I only tried it briefly as a third detector while switching to tsukuba, saw the throw, and removed it again before pushing, so 9a23bfe tests FAST and SIFT only. Nothing to revert here. Thanks for the detail about the mask pyramid, that is useful. It explains that the cv::threshold throw I hit is only the first symptom rather than the whole story, so a type gate relax would not have been anywhere near sufficient. Good to know before spending time on it. Thanks for the review and the approval. |
Address review feedback: turn the loop over detectors into a value parameterized test, one INSTANTIATE_TEST_CASE_P per detector, following test_descriptors_invariance.cpp. The detector name now shows up in the test case name, each detector runs as an independent iteration, and adding another one is a single line. The parameter is a factory function rather than a Ptr, for the reason already noted in test_descriptors_invariance.impl.hpp: parameters live for the whole test program run.
|
@asmorkalov , sorry for the delay, but I have done the fix for which you provided the friendly reminder earlier today. |
|
Build warnings: |
Fixes the -Wmaybe-uninitialized warnings GCC 13 reports on test_keypoints.cpp. The warning comes from std::function's copy constructor reading _M_functor out of the temporary that testing::Values builds, and gtest copies the parameter several times on the way into ParamGenerator. The factories are captureless lambdas, so a plain function pointer holds them. It is trivially copyable, there is no type-erased buffer to warn about, and the test case line now prints the factory address instead of a 64 byte hex dump of the std::function object.
Problem
cv::Mat_<bool>::depth()returnsCV_Boolin 5.0, where it returnedCV_8Uin 4.x.SIFT_Impl::detectAndComputegates the mask atsift.dispatch.cpp:980, so a boolean detection mask now fails with:Fix
Allow
CV_BoolC1in that check, and update the message accordingly.No conversion is needed. The mask is consumed by
KeyPointsFilter::runByPixelsMask, whoseMaskPredicatereads it withMat::at<uchar>, andMat::atonly asserts that the element size matches (CV_ELEM_SIZE1(traits::Depth<_Tp>::value) == elemSize1()).CV_Boolis one byte likeCV_8U, so the existing read is already correct for a boolean mask.That is also why
FastFeatureDetectorandSimpleBlobDetector, 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
goodFeaturesToTrackalready uses in this module (featureselect.cpp:301acceptsCV_8UC1 || CV_BoolC1).Test
Features2d_Detector_Keypoints_BoolMask.matches_uchar_maskbuilds a synthetic image with circles and rectangles, runs FAST, SIFT and SimpleBlobDetector with aMat_<bool>mask and with the equivalentCV_8UC1mask, 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
Patch to opencv_extra has the same branch name.