From cb286a66be9c1807c2aa1d3bf4cfd491d7105762 Mon Sep 17 00:00:00 2001 From: tv3141 Date: Wed, 10 Nov 2021 20:15:38 +0100 Subject: [PATCH] Merge pull request #21030 from tv3141:fix_seg_fault_houghlinespointset Fix seg fault houghlinespointset * Clarify parameter doc for HoughLinesPointSet * Fix seg fault. * Add regression test. * Fix latex typo --- modules/imgproc/include/opencv2/imgproc.hpp | 10 +++---- modules/imgproc/src/hough.cpp | 4 ++- modules/imgproc/test/test_houghlines.cpp | 30 +++++++++++++++++++++ 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/modules/imgproc/include/opencv2/imgproc.hpp b/modules/imgproc/include/opencv2/imgproc.hpp index 8db6cc5d8d..c669f2cdef 100644 --- a/modules/imgproc/include/opencv2/imgproc.hpp +++ b/modules/imgproc/include/opencv2/imgproc.hpp @@ -2086,12 +2086,12 @@ The function finds lines in a set of points using a modification of the Hough tr @param point Input vector of points. Each vector must be encoded as a Point vector \f$(x,y)\f$. Type must be CV_32FC2 or CV_32SC2. @param lines Output vector of found lines. Each vector is encoded as a vector \f$(votes, rho, theta)\f$. The larger the value of 'votes', the higher the reliability of the Hough line. -@param lines_max Max count of hough lines. +@param lines_max Max count of Hough lines. @param threshold Accumulator threshold parameter. Only those lines are returned that get enough -votes ( \f$>\texttt{threshold}\f$ ) -@param min_rho Minimum Distance value of the accumulator in pixels. -@param max_rho Maximum Distance value of the accumulator in pixels. -@param rho_step Distance resolution of the accumulator in pixels. +votes ( \f$>\texttt{threshold}\f$ ). +@param min_rho Minimum value for \f$\rho\f$ for the accumulator (Note: \f$\rho\f$ can be negative. The absolute value \f$|\rho|\f$ is the distance of a line to the origin.). +@param max_rho Maximum value for \f$\rho\f$ for the accumulator. +@param rho_step Distance resolution of the accumulator. @param min_theta Minimum angle value of the accumulator in radians. @param max_theta Maximum angle value of the accumulator in radians. @param theta_step Angle resolution of the accumulator in radians. diff --git a/modules/imgproc/src/hough.cpp b/modules/imgproc/src/hough.cpp index b48b7ea137..9ed6cc5c57 100644 --- a/modules/imgproc/src/hough.cpp +++ b/modules/imgproc/src/hough.cpp @@ -975,7 +975,9 @@ void HoughLinesPointSet( InputArray _point, OutputArray _lines, int lines_max, i for(int n = 0; n < numangle; n++ ) { int r = cvRound( point.at(i).x * tabCos[n] + point.at(i).y * tabSin[n] - irho_min); - accum[(n+1) * (numrho+2) + r+1]++; + if ( r >= 0 && r <= numrho) { + accum[(n+1) * (numrho+2) + r+1]++; + } } // stage 2. find local maximums diff --git a/modules/imgproc/test/test_houghlines.cpp b/modules/imgproc/test/test_houghlines.cpp index fca0449b91..e90891274a 100644 --- a/modules/imgproc/test/test_houghlines.cpp +++ b/modules/imgproc/test/test_houghlines.cpp @@ -299,6 +299,36 @@ TEST_P(HoughLinesPointSetTest, regression) run_test(); } +TEST(HoughLinesPointSet, regression_21029) +{ + std::vector points; + points.push_back(Point2f(100, 100)); + points.push_back(Point2f(1000, 1000)); + points.push_back(Point2f(10000, 10000)); + points.push_back(Point2f(100000, 100000)); + + double rhoMin = 0; + double rhoMax = 10; + double rhoStep = 0.1; + + double thetaMin = 85 * CV_PI / 180.0; + double thetaMax = 95 * CV_PI / 180.0; + double thetaStep = 1 * CV_PI / 180.0; + + int lines_max = 5; + int threshold = 100; + + Mat lines; + + HoughLinesPointSet(points, lines, + lines_max, threshold, + rhoMin, rhoMax, rhoStep, + thetaMin, thetaMax, thetaStep + ); + + EXPECT_TRUE(lines.empty()); +} + INSTANTIATE_TEST_CASE_P( ImgProc, StandartHoughLinesTest, testing::Combine(testing::Values( "shared/pic5.png", "../stitching/a1.png" ), testing::Values( 1, 10 ), testing::Values( 0.05, 0.1 ), -- GitLab