mirror of
https://github.com/opencv/opencv.git
synced 2026-09-25 04:09:57 +03:00
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
195 lines
6.9 KiB
C++
195 lines
6.9 KiB
C++
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
//
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
//
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
// If you do not agree to this license, do not download, install,
|
|
// copy or use the software.
|
|
//
|
|
//
|
|
// Intel License Agreement
|
|
// For Open Source Computer Vision Library
|
|
//
|
|
// Copyright (C) 2000, Intel Corporation, all rights reserved.
|
|
// Third party copyrights are property of their respective owners.
|
|
//
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
// are permitted provided that the following conditions are met:
|
|
//
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
// this list of conditions and the following disclaimer.
|
|
//
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
// and/or other materials provided with the distribution.
|
|
//
|
|
// * The name of Intel Corporation may not be used to endorse or promote products
|
|
// derived from this software without specific prior written permission.
|
|
//
|
|
// This software is provided by the copyright holders and contributors "as is" and
|
|
// any express or implied warranties, including, but not limited to, the implied
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
//
|
|
//M*/
|
|
|
|
#include "test_precomp.hpp"
|
|
|
|
namespace opencv_test { namespace {
|
|
|
|
const string FEATURES2D_DIR = "features2d";
|
|
const string IMAGE_FILENAME = "tsukuba.png";
|
|
|
|
/****************************************************************************************\
|
|
* Test for KeyPoint *
|
|
\****************************************************************************************/
|
|
|
|
class CV_FeatureDetectorKeypointsTest : public cvtest::BaseTest
|
|
{
|
|
public:
|
|
CV_FeatureDetectorKeypointsTest(const Ptr<FeatureDetector>& _detector) :
|
|
detector(_detector) {}
|
|
|
|
protected:
|
|
virtual void run(int)
|
|
{
|
|
CV_Assert(detector);
|
|
string imgFilename = string(ts->get_data_path()) + FEATURES2D_DIR + "/" + IMAGE_FILENAME;
|
|
|
|
// Read the test image.
|
|
Mat image = imread(imgFilename);
|
|
if(image.empty())
|
|
{
|
|
ts->printf(cvtest::TS::LOG, "Image %s can not be read.\n", imgFilename.c_str());
|
|
ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
|
|
return;
|
|
}
|
|
|
|
vector<KeyPoint> keypoints;
|
|
detector->detect(image, keypoints);
|
|
|
|
if(keypoints.empty())
|
|
{
|
|
ts->printf(cvtest::TS::LOG, "Detector can't find keypoints in image.\n");
|
|
ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
|
|
return;
|
|
}
|
|
|
|
Rect r(0, 0, image.cols, image.rows);
|
|
for(size_t i = 0; i < keypoints.size(); i++)
|
|
{
|
|
const KeyPoint& kp = keypoints[i];
|
|
|
|
if(!r.contains(kp.pt))
|
|
{
|
|
ts->printf(cvtest::TS::LOG, "KeyPoint::pt is out of image (x=%f, y=%f).\n", kp.pt.x, kp.pt.y);
|
|
ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
|
|
return;
|
|
}
|
|
|
|
if(kp.size <= 0.f)
|
|
{
|
|
ts->printf(cvtest::TS::LOG, "KeyPoint::size is not positive (%f).\n", kp.size);
|
|
ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
|
|
return;
|
|
}
|
|
|
|
if((kp.angle < 0.f && kp.angle != -1.f) || kp.angle >= 360.f)
|
|
{
|
|
ts->printf(cvtest::TS::LOG, "KeyPoint::angle is out of range [0, 360). It's %f.\n", kp.angle);
|
|
ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
|
|
return;
|
|
}
|
|
}
|
|
ts->set_failed_test_info(cvtest::TS::OK);
|
|
}
|
|
|
|
Ptr<FeatureDetector> detector;
|
|
};
|
|
|
|
|
|
// Registration of tests
|
|
TEST(Features2d_Detector_Keypoints_FAST, validation)
|
|
{
|
|
CV_FeatureDetectorKeypointsTest test(FastFeatureDetector::create());
|
|
test.safe_run();
|
|
}
|
|
|
|
TEST(Features2d_Detector_Keypoints_HARRIS, validation)
|
|
{
|
|
|
|
CV_FeatureDetectorKeypointsTest test(GFTTDetector::create(1000, 0.01, 1, 3, 3, true, 0.04));
|
|
test.safe_run();
|
|
}
|
|
|
|
TEST(Features2d_Detector_Keypoints_GFTT, validation)
|
|
{
|
|
Ptr<GFTTDetector> gftt = GFTTDetector::create();
|
|
gftt->setHarrisDetector(true);
|
|
CV_FeatureDetectorKeypointsTest test(gftt);
|
|
test.safe_run();
|
|
}
|
|
|
|
TEST(Features2d_Detector_Keypoints_MSER, validation)
|
|
{
|
|
CV_FeatureDetectorKeypointsTest test(MSER::create());
|
|
test.safe_run();
|
|
}
|
|
|
|
TEST(Features2d_Detector_Keypoints_ORB, validation)
|
|
{
|
|
CV_FeatureDetectorKeypointsTest test(ORB::create());
|
|
test.safe_run();
|
|
}
|
|
|
|
TEST(Features2d_Detector_Keypoints_SIFT, validation)
|
|
{
|
|
CV_FeatureDetectorKeypointsTest test(SIFT::create());
|
|
test.safe_run();
|
|
}
|
|
|
|
|
|
// 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
|