mirror of
https://github.com/opencv/opencv.git
synced 2026-07-31 00:03:03 +04:00
Fix angle discretization in Hough transforms
In some situations the last value was missing from the discrete theta values. Now, the last value is chosen such that it is close to the user-provided maximum theta, while the distance to pi remains always at least theta_step/2. This should avoid duplicate detections. A better way would probably be to use max_theta as is and adjust the resolution (theta_step) instead, such that the discretization would always be uniform (in a circular sense) when full angle range is used.
This commit is contained in:
@@ -68,6 +68,18 @@ struct hough_cmp_gt
|
||||
const int* aux;
|
||||
};
|
||||
|
||||
static inline int
|
||||
computeNumangle( double min_theta, double max_theta, double theta_step )
|
||||
{
|
||||
int numangle = cvFloor((max_theta - min_theta) / theta_step) + 1;
|
||||
// If the distance between the first angle and the last angle is
|
||||
// approximately equal to pi, then the last angle will be removed
|
||||
// in order to prevent a line to be detected twice.
|
||||
if ( numangle > 1 && fabs(CV_PI - (numangle-1)*theta_step) < theta_step/2 )
|
||||
--numangle;
|
||||
return numangle;
|
||||
}
|
||||
|
||||
static void
|
||||
createTrigTable( int numangle, double min_theta, double theta_step,
|
||||
float irho, float *tabSin, float *tabCos )
|
||||
@@ -130,7 +142,7 @@ HoughLinesStandard( InputArray src, OutputArray lines, int type,
|
||||
|
||||
CV_CheckGE(max_theta, min_theta, "max_theta must be greater than min_theta");
|
||||
|
||||
int numangle = cvRound((max_theta - min_theta) / theta);
|
||||
int numangle = computeNumangle(min_theta, max_theta, theta);
|
||||
int numrho = cvRound(((max_rho - min_rho) + 1) / rho);
|
||||
|
||||
#if defined HAVE_IPP && IPP_VERSION_X100 >= 810 && !IPP_DISABLE_HOUGH
|
||||
@@ -475,7 +487,7 @@ HoughLinesProbabilistic( Mat& image,
|
||||
int width = image.cols;
|
||||
int height = image.rows;
|
||||
|
||||
int numangle = cvRound(CV_PI / theta);
|
||||
int numangle = computeNumangle(0.0, CV_PI, theta);
|
||||
int numrho = cvRound(((width + height) * 2 + 1) / rho);
|
||||
|
||||
#if defined HAVE_IPP && IPP_VERSION_X100 >= 810 && !IPP_DISABLE_HOUGH
|
||||
@@ -792,7 +804,7 @@ static bool ocl_HoughLines(InputArray _src, OutputArray _lines, double rho, doub
|
||||
}
|
||||
|
||||
UMat src = _src.getUMat();
|
||||
int numangle = cvRound((max_theta - min_theta) / theta);
|
||||
int numangle = computeNumangle(min_theta, max_theta, theta);
|
||||
int numrho = cvRound(((src.cols + src.rows) * 2 + 1) / rho);
|
||||
|
||||
UMat pointsList;
|
||||
@@ -846,7 +858,7 @@ static bool ocl_HoughLinesP(InputArray _src, OutputArray _lines, double rho, dou
|
||||
}
|
||||
|
||||
UMat src = _src.getUMat();
|
||||
int numangle = cvRound(CV_PI / theta);
|
||||
int numangle = computeNumangle(0.0, CV_PI, theta);
|
||||
int numrho = cvRound(((src.cols + src.rows) * 2 + 1) / rho);
|
||||
|
||||
UMat pointsList;
|
||||
@@ -956,7 +968,7 @@ void HoughLinesPointSet( InputArray _point, OutputArray _lines, int lines_max, i
|
||||
int i;
|
||||
float irho = 1 / (float)rho_step;
|
||||
float irho_min = ((float)min_rho * irho);
|
||||
int numangle = cvRound((max_theta - min_theta) / theta_step);
|
||||
int numangle = computeNumangle(min_theta, max_theta, theta_step);
|
||||
int numrho = cvRound((max_rho - min_rho + 1) / rho_step);
|
||||
|
||||
Mat _accum = Mat::zeros( (numangle+2), (numrho+2), CV_32SC1 );
|
||||
|
||||
Reference in New Issue
Block a user