mirror of
https://github.com/opencv/opencv.git
synced 2026-07-21 19:33:03 +04:00
features2d: improve detector parameter validation
This commit is contained in:
@@ -8052,6 +8052,9 @@ void AGAST(InputArray _img, std::vector<KeyPoint>& keypoints, int threshold, boo
|
||||
case AgastFeatureDetector::OAST_9_16:
|
||||
OAST_9_16(_img, kpts, threshold);
|
||||
break;
|
||||
default:
|
||||
CV_Error_(Error::StsBadArg,
|
||||
("Unknown AgastFeatureDetector detector type: %d", static_cast<int>(type)));
|
||||
}
|
||||
|
||||
cv::Mat img = _img.getMat();
|
||||
|
||||
@@ -104,6 +104,8 @@ public:
|
||||
}
|
||||
virtual void setPatternScale(float _patternScale) CV_OVERRIDE
|
||||
{
|
||||
CV_CheckGT(_patternScale, 0.f, "patternScale must be positive");
|
||||
|
||||
patternScale = _patternScale;
|
||||
std::vector<float> rList;
|
||||
std::vector<int> nList;
|
||||
|
||||
@@ -441,6 +441,14 @@ void FAST(InputArray _img, std::vector<KeyPoint>& keypoints, int threshold, bool
|
||||
{
|
||||
CV_INSTRUMENT_REGION();
|
||||
|
||||
if (type != FastFeatureDetector::TYPE_5_8 &&
|
||||
type != FastFeatureDetector::TYPE_7_12 &&
|
||||
type != FastFeatureDetector::TYPE_9_16)
|
||||
{
|
||||
CV_Error_(Error::StsBadArg,
|
||||
("Unknown FastFeatureDetector detector type: %d", static_cast<int>(type)));
|
||||
}
|
||||
|
||||
const size_t max_fast_features = std::max(_img.total()/100, size_t(1000)); // Simple heuristic that depends on resolution.
|
||||
|
||||
CV_OCL_RUN(_img.isUMat() && type == FastFeatureDetector::TYPE_9_16,
|
||||
@@ -549,7 +557,7 @@ public:
|
||||
else if(prop == FAST_N)
|
||||
type = static_cast<FastFeatureDetector::DetectorType>(cvRound(value));
|
||||
else
|
||||
CV_Error(Error::StsBadArg, "");
|
||||
CV_Error_(Error::StsBadArg, ("Unknown FastFeatureDetector property: %d", prop));
|
||||
}
|
||||
|
||||
double get(int prop) const
|
||||
@@ -560,7 +568,7 @@ public:
|
||||
return nonmaxSuppression;
|
||||
if(prop == FAST_N)
|
||||
return static_cast<int>(type);
|
||||
CV_Error(Error::StsBadArg, "");
|
||||
CV_Error_(Error::StsBadArg, ("Unknown FastFeatureDetector property: %d", prop));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -1266,7 +1266,25 @@ void ORB_Impl::detectAndCompute( InputArray _image, InputArray _mask,
|
||||
Ptr<ORB> ORB::create(int nfeatures, float scaleFactor, int nlevels, int edgeThreshold,
|
||||
int firstLevel, int wta_k, ORB::ScoreType scoreType, int patchSize, int fastThreshold)
|
||||
{
|
||||
CV_Assert(firstLevel >= 0);
|
||||
CV_CheckGE(nfeatures, 0, "nfeatures must be non-negative");
|
||||
CV_CheckGT(scaleFactor, 1.f, "scaleFactor must be greater than 1");
|
||||
CV_CheckGT(nlevels, 0, "nlevels must be positive");
|
||||
CV_CheckGE(edgeThreshold, 0, "edgeThreshold must be non-negative");
|
||||
CV_CheckGE(firstLevel, 0, "firstLevel must be non-negative");
|
||||
CV_CheckGE(patchSize, 2, "patchSize must be at least 2");
|
||||
|
||||
if (wta_k != 2 && wta_k != 3 && wta_k != 4)
|
||||
{
|
||||
CV_Error_(Error::StsBadArg,
|
||||
("wta_k must be 2, 3, or 4, but got %d", wta_k));
|
||||
}
|
||||
|
||||
if (scoreType != ORB::HARRIS_SCORE && scoreType != ORB::FAST_SCORE)
|
||||
{
|
||||
CV_Error_(Error::StsBadArg,
|
||||
("Unknown ORB score type: %d", static_cast<int>(scoreType)));
|
||||
}
|
||||
|
||||
return makePtr<ORB_Impl>(nfeatures, scaleFactor, nlevels, edgeThreshold,
|
||||
firstLevel, wta_k, scoreType, patchSize, fastThreshold);
|
||||
}
|
||||
|
||||
@@ -135,4 +135,14 @@ void CV_AgastTest::run( int )
|
||||
|
||||
TEST(Features2d_AGAST, regression) { CV_AgastTest test; test.safe_run(); }
|
||||
|
||||
TEST(Features2d_AGAST, invalidDetectorType)
|
||||
{
|
||||
Mat img = Mat::zeros(16, 16, CV_8UC1);
|
||||
vector<KeyPoint> keypoints;
|
||||
|
||||
EXPECT_THROW(AGAST(img, keypoints, 10, true,
|
||||
static_cast<AgastFeatureDetector::DetectorType>(-1)),
|
||||
cv::Exception);
|
||||
}
|
||||
|
||||
}} // namespace
|
||||
|
||||
@@ -105,4 +105,10 @@ void CV_BRISKTest::run( int )
|
||||
|
||||
TEST(Features2d_BRISK, regression) { CV_BRISKTest test; test.safe_run(); }
|
||||
|
||||
TEST(Features2d_BRISK, invalidCreateParameters)
|
||||
{
|
||||
EXPECT_THROW(BRISK::create(30, 3, 0.0f), cv::Exception);
|
||||
EXPECT_THROW(BRISK::create(30, 3, -1.0f), cv::Exception);
|
||||
}
|
||||
|
||||
}} // namespace
|
||||
|
||||
@@ -165,4 +165,14 @@ TEST(Features2d_FAST, noNMS)
|
||||
ASSERT_EQ( 0, cvtest::norm(gt_kps, kps, NORM_L2));
|
||||
}
|
||||
|
||||
TEST(Features2d_FAST, invalidDetectorType)
|
||||
{
|
||||
Mat img = Mat::zeros(16, 16, CV_8UC1);
|
||||
vector<KeyPoint> keypoints;
|
||||
|
||||
EXPECT_THROW(FAST(img, keypoints, 10, true,
|
||||
static_cast<FastFeatureDetector::DetectorType>(-1)),
|
||||
cv::Exception);
|
||||
}
|
||||
|
||||
}} // namespace
|
||||
|
||||
@@ -195,4 +195,15 @@ TEST(Features2D_ORB, MaskValue)
|
||||
ASSERT_EQ(countNonZero(diff), 0);
|
||||
}
|
||||
|
||||
TEST(Features2D_ORB, invalidCreateParameters)
|
||||
{
|
||||
EXPECT_THROW(ORB::create(500, 1.0f), cv::Exception);
|
||||
EXPECT_THROW(ORB::create(500, 1.2f, 0), cv::Exception);
|
||||
EXPECT_THROW(ORB::create(500, 1.2f, 8, 31, 0, 5), cv::Exception);
|
||||
EXPECT_THROW(ORB::create(500, 1.2f, 8, 31, 0, 2,
|
||||
static_cast<ORB::ScoreType>(-1)), cv::Exception);
|
||||
EXPECT_THROW(ORB::create(500, 1.2f, 8, 31, 0, 2,
|
||||
ORB::HARRIS_SCORE, 1), cv::Exception);
|
||||
}
|
||||
|
||||
}} // namespace
|
||||
|
||||
@@ -10,7 +10,7 @@ using namespace cv;
|
||||
static void help(char** argv)
|
||||
{
|
||||
|
||||
printf("\nShow off image morphology: erosion, dialation, open and close\n"
|
||||
printf("\nShow off image morphology: erosion, dilation, opening and closing\n"
|
||||
"Call:\n %s [image]\n"
|
||||
"This program also shows use of rect, ellipse, cross and diamond kernels\n\n", argv[0]);
|
||||
printf( "Hot keys: \n"
|
||||
|
||||
Reference in New Issue
Block a user