From 0ea454301f64e813b6e6470be23fb550324a17c9 Mon Sep 17 00:00:00 2001 From: Scott Breyfogle Date: Fri, 10 Jan 2014 16:42:25 -0800 Subject: [PATCH] Added optional constraints to non-probablistic hough lines functions --- modules/imgproc/doc/feature_detection.rst | 10 ++++-- modules/imgproc/include/opencv2/imgproc.hpp | 3 +- .../include/opencv2/imgproc/imgproc_c.h | 3 +- modules/imgproc/src/hough.cpp | 31 ++++++++++++------- 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/modules/imgproc/doc/feature_detection.rst b/modules/imgproc/doc/feature_detection.rst index d92d8d4659..023028823b 100644 --- a/modules/imgproc/doc/feature_detection.rst +++ b/modules/imgproc/doc/feature_detection.rst @@ -358,11 +358,11 @@ HoughLines ---------- Finds lines in a binary image using the standard Hough transform. -.. ocv:function:: void HoughLines( InputArray image, OutputArray lines, double rho, double theta, int threshold, double srn=0, double stn=0 ) +.. ocv:function:: void HoughLines( InputArray image, OutputArray lines, double rho, double theta, int threshold, double srn=0, double stn=0, double min_theta=0, double max_theta=CV_PI ) -.. ocv:pyfunction:: cv2.HoughLines(image, rho, theta, threshold[, lines[, srn[, stn]]]) -> lines +.. ocv:pyfunction:: cv2.HoughLines(image, rho, theta, threshold[, lines[, srn[, stn[, min_theta[, max_theta]]]]]) -> lines -.. ocv:cfunction:: CvSeq* cvHoughLines2( CvArr* image, void* line_storage, int method, double rho, double theta, int threshold, double param1=0, double param2=0 ) +.. ocv:cfunction:: CvSeq* cvHoughLines2( CvArr* image, void* line_storage, int method, double rho, double theta, int threshold, double param1=0, double param2=0, double min_theta=0, double max_theta=CV_PI ) :param image: 8-bit, single-channel binary source image. The image may be modified by the function. @@ -378,6 +378,10 @@ Finds lines in a binary image using the standard Hough transform. :param stn: For the multi-scale Hough transform, it is a divisor for the distance resolution ``theta``. + :param min_theta: For standard and multi-scale Hough transform, minimum angle to check for lines. Must fall between 0 and max_theta. + + :param max_theta: For standard and multi-scale Hough transform, maximum angle to check for lines. Must fall between min_theta and CV_PI. + :param method: One of the following Hough transform variants: * **CV_HOUGH_STANDARD** classical or standard Hough transform. Every line is represented by two floating-point numbers :math:`(\rho, \theta)` , where :math:`\rho` is a distance between (0,0) point and the line, and :math:`\theta` is the angle between x-axis and the normal to the line. Thus, the matrix must be (the created sequence will be) of ``CV_32FC2`` type diff --git a/modules/imgproc/include/opencv2/imgproc.hpp b/modules/imgproc/include/opencv2/imgproc.hpp index 5a9450bf22..3c66873eb3 100644 --- a/modules/imgproc/include/opencv2/imgproc.hpp +++ b/modules/imgproc/include/opencv2/imgproc.hpp @@ -1151,7 +1151,8 @@ CV_EXPORTS_W void goodFeaturesToTrack( InputArray image, OutputArray corners, //! finds lines in the black-n-white image using the standard or pyramid Hough transform CV_EXPORTS_W void HoughLines( InputArray image, OutputArray lines, double rho, double theta, int threshold, - double srn = 0, double stn = 0 ); + double srn = 0, double stn = 0, + double min_theta = 0, double max_theta = CV_PI ); //! finds line segments in the black-n-white image using probabilistic Hough transform CV_EXPORTS_W void HoughLinesP( InputArray image, OutputArray lines, diff --git a/modules/imgproc/include/opencv2/imgproc/imgproc_c.h b/modules/imgproc/include/opencv2/imgproc/imgproc_c.h index 124f7f24c2..168a5cfd23 100644 --- a/modules/imgproc/include/opencv2/imgproc/imgproc_c.h +++ b/modules/imgproc/include/opencv2/imgproc/imgproc_c.h @@ -601,7 +601,8 @@ CVAPI(void) cvGoodFeaturesToTrack( const CvArr* image, CvArr* eig_image, param1 ~ srn, param2 ~ stn - for multi-scale */ CVAPI(CvSeq*) cvHoughLines2( CvArr* image, void* line_storage, int method, double rho, double theta, int threshold, - double param1 CV_DEFAULT(0), double param2 CV_DEFAULT(0)); + double param1 CV_DEFAULT(0), double param2 CV_DEFAULT(0), + double min_theta CV_DEFAULT(0), double max_theta CV_DEFAULT(CV_PI)); /* Finds circles in the image */ CVAPI(CvSeq*) cvHoughCircles( CvArr* image, void* circle_storage, diff --git a/modules/imgproc/src/hough.cpp b/modules/imgproc/src/hough.cpp index 9c8eaca8f5..6cbc9bcc6d 100644 --- a/modules/imgproc/src/hough.cpp +++ b/modules/imgproc/src/hough.cpp @@ -75,7 +75,8 @@ Functions return the actual number of found lines. */ static void HoughLinesStandard( const Mat& img, float rho, float theta, - int threshold, std::vector& lines, int linesMax ) + int threshold, std::vector& lines, int linesMax, + double min_theta, double max_theta ) { int i, j; float irho = 1 / rho; @@ -87,7 +88,13 @@ HoughLinesStandard( const Mat& img, float rho, float theta, int width = img.cols; int height = img.rows; - int numangle = cvRound(CV_PI / theta); + if (max_theta < 0 || max_theta > CV_PI ) { + CV_Error( CV_StsBadArg, "max_theta must fall between 0 and pi" ); + } + if (min_theta < 0 || min_theta > max_theta ) { + CV_Error( CV_StsBadArg, "min_theta must fall between 0 and max_theta" ); + } + int numangle = cvRound((max_theta - min_theta) / theta); int numrho = cvRound(((width + height) * 2 + 1) / rho); AutoBuffer _accum((numangle+2) * (numrho+2)); @@ -99,7 +106,7 @@ HoughLinesStandard( const Mat& img, float rho, float theta, memset( accum, 0, sizeof(accum[0]) * (numangle+2) * (numrho+2) ); - float ang = 0; + float ang = min_theta; for(int n = 0; n < numangle; ang += theta, n++ ) { tabSin[n] = (float)(sin((double)ang) * irho); @@ -166,7 +173,8 @@ static void HoughLinesSDiv( const Mat& img, float rho, float theta, int threshold, int srn, int stn, - std::vector& lines, int linesMax ) + std::vector& lines, int linesMax, + double min_theta, double max_theta ) { #define _POINT(row, column)\ (image_src[(row)*step+(column)]) @@ -293,7 +301,7 @@ HoughLinesSDiv( const Mat& img, if( count * 100 > rn * tn ) { - HoughLinesStandard( img, rho, theta, threshold, lines, linesMax ); + HoughLinesStandard( img, rho, theta, threshold, lines, linesMax, min_theta, max_theta ); return; } @@ -601,15 +609,15 @@ HoughLinesProbabilistic( Mat& image, void cv::HoughLines( InputArray _image, OutputArray _lines, double rho, double theta, int threshold, - double srn, double stn ) + double srn, double stn, double min_theta, double max_theta ) { Mat image = _image.getMat(); std::vector lines; if( srn == 0 && stn == 0 ) - HoughLinesStandard(image, (float)rho, (float)theta, threshold, lines, INT_MAX); + HoughLinesStandard(image, (float)rho, (float)theta, threshold, lines, INT_MAX, min_theta, max_theta ); else - HoughLinesSDiv(image, (float)rho, (float)theta, threshold, cvRound(srn), cvRound(stn), lines, INT_MAX); + HoughLinesSDiv(image, (float)rho, (float)theta, threshold, cvRound(srn), cvRound(stn), lines, INT_MAX, min_theta, max_theta); Mat(lines).copyTo(_lines); } @@ -631,7 +639,8 @@ void cv::HoughLinesP(InputArray _image, OutputArray _lines, CV_IMPL CvSeq* cvHoughLines2( CvArr* src_image, void* lineStorage, int method, double rho, double theta, int threshold, - double param1, double param2 ) + double param1, double param2, + double min_theta, double max_theta ) { cv::Mat image = cv::cvarrToMat(src_image); std::vector l2; @@ -694,11 +703,11 @@ cvHoughLines2( CvArr* src_image, void* lineStorage, int method, { case CV_HOUGH_STANDARD: HoughLinesStandard( image, (float)rho, - (float)theta, threshold, l2, linesMax ); + (float)theta, threshold, l2, linesMax, min_theta, max_theta ); break; case CV_HOUGH_MULTI_SCALE: HoughLinesSDiv( image, (float)rho, (float)theta, - threshold, iparam1, iparam2, l2, linesMax ); + threshold, iparam1, iparam2, l2, linesMax, min_theta, max_theta ); break; case CV_HOUGH_PROBABILISTIC: HoughLinesProbabilistic( image, (float)rho, (float)theta,