Skip to content

Commit 3503a36

Browse files
committed
Merge pull request opencv#18444 from aitikgupta:check-minimum-points
2 parents 2ea7269 + cbf978d commit 3503a36

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

modules/calib3d/src/fundam.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,9 @@ cv::Mat cv::findHomography( InputArray _points1, InputArray _points2,
374374
return Mat();
375375
convertPointsFromHomogeneous(p, p);
376376
}
377+
// Need at least 4 point correspondences to calculate Homography
378+
if( npoints < 4 )
379+
CV_Error(Error::StsVecLengthErr , "The input arrays should have at least 4 corresponding point sets to calculate Homography");
377380
p.reshape(2, npoints).convertTo(m, CV_32F);
378381
}
379382

modules/calib3d/test/test_homography.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ namespace opencv_test { namespace {
6363
#define MESSAGE_RANSAC_DIFF "Reprojection error for current pair of points more than required."
6464

6565
#define MAX_COUNT_OF_POINTS 303
66+
#define MIN_COUNT_OF_POINTS 4
6667
#define COUNT_NORM_TYPES 3
6768
#define METHODS_COUNT 4
6869

@@ -249,7 +250,7 @@ void CV_HomographyTest::print_information_8(int _method, int j, int N, int k, in
249250

250251
void CV_HomographyTest::run(int)
251252
{
252-
for (int N = 4; N <= MAX_COUNT_OF_POINTS; ++N)
253+
for (int N = MIN_COUNT_OF_POINTS; N <= MAX_COUNT_OF_POINTS; ++N)
253254
{
254255
RNG& rng = ts->get_rng();
255256

@@ -711,4 +712,27 @@ TEST(Calib3d_Homography, fromImages)
711712
ASSERT_GE(ninliers1, 80);
712713
}
713714

715+
TEST(Calib3d_Homography, minPoints)
716+
{
717+
float pt1data[] =
718+
{
719+
2.80073029e+002f, 2.39591217e+002f, 2.21912201e+002f, 2.59783997e+002f
720+
};
721+
722+
float pt2data[] =
723+
{
724+
1.84072723e+002f, 1.43591202e+002f, 1.25912483e+002f, 1.63783859e+002f
725+
};
726+
727+
int npoints = (int)(sizeof(pt1data)/sizeof(pt1data[0])/2);
728+
printf("npoints = %d\n", npoints); // npoints = 2
729+
730+
Mat p1(1, npoints, CV_32FC2, pt1data);
731+
Mat p2(1, npoints, CV_32FC2, pt2data);
732+
Mat mask;
733+
734+
// findHomography should raise an error since npoints < MIN_COUNT_OF_POINTS
735+
EXPECT_THROW(findHomography(p1, p2, RANSAC, 0.01, mask), cv::Exception);
736+
}
737+
714738
}} // namespace

0 commit comments

Comments
 (0)