mirror of
https://github.com/opencv/opencv.git
synced 2026-07-31 08:13:04 +04:00
Merge pull request #21407 from sensyn-robotics:feature/weighted_hough
Feature: weighted Hough Transform #21407 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or other license that is incompatible with OpenCV - [x] The PR is proposed to proper branch - [x] There is reference to original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
@@ -2165,11 +2165,13 @@ Must fall between 0 and max_theta.
|
||||
@param max_theta For standard and multi-scale Hough transform, an upper bound for the angle.
|
||||
Must fall between min_theta and CV_PI. The actual maximum angle in the accumulator may be slightly
|
||||
less than max_theta, depending on the parameters min_theta and theta.
|
||||
@param use_edgeval True if you want to use weighted Hough transform.
|
||||
*/
|
||||
CV_EXPORTS_W 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 );
|
||||
double min_theta = 0, double max_theta = CV_PI,
|
||||
bool use_edgeval = false );
|
||||
|
||||
/** @brief Finds line segments in a binary image using the probabilistic Hough transform.
|
||||
|
||||
|
||||
@@ -120,7 +120,7 @@ static void
|
||||
HoughLinesStandard( InputArray src, OutputArray lines, int type,
|
||||
float rho, float theta,
|
||||
int threshold, int linesMax,
|
||||
double min_theta, double max_theta )
|
||||
double min_theta, double max_theta, bool use_edgeval = false )
|
||||
{
|
||||
CV_CheckType(type, type == CV_32FC2 || type == CV_32FC3, "Internal error");
|
||||
|
||||
@@ -184,17 +184,31 @@ HoughLinesStandard( InputArray src, OutputArray lines, int type,
|
||||
irho, tabSin, tabCos);
|
||||
|
||||
// stage 1. fill accumulator
|
||||
for( i = 0; i < height; i++ )
|
||||
for( j = 0; j < width; j++ )
|
||||
{
|
||||
if( image[i * step + j] != 0 )
|
||||
for(int n = 0; n < numangle; n++ )
|
||||
{
|
||||
int r = cvRound( j * tabCos[n] + i * tabSin[n] );
|
||||
r += (numrho - 1) / 2;
|
||||
accum[(n+1) * (numrho+2) + r+1]++;
|
||||
}
|
||||
}
|
||||
if (use_edgeval) {
|
||||
for( i = 0; i < height; i++ )
|
||||
for( j = 0; j < width; j++ )
|
||||
{
|
||||
if( image[i * step + j] != 0 )
|
||||
for(int n = 0; n < numangle; n++ )
|
||||
{
|
||||
int r = cvRound( j * tabCos[n] + i * tabSin[n] );
|
||||
r += (numrho - 1) / 2;
|
||||
accum[(n + 1) * (numrho + 2) + r + 1] += image[i * step + j];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for( i = 0; i < height; i++ )
|
||||
for( j = 0; j < width; j++ )
|
||||
{
|
||||
if( image[i * step + j] != 0 )
|
||||
for(int n = 0; n < numangle; n++ )
|
||||
{
|
||||
int r = cvRound( j * tabCos[n] + i * tabSin[n] );
|
||||
r += (numrho - 1) / 2;
|
||||
accum[(n + 1) * (numrho + 2) + r + 1]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// stage 2. find local maximums
|
||||
findLocalMaximums( numrho, numangle, threshold, accum, _sort_buf );
|
||||
@@ -907,7 +921,7 @@ static bool ocl_HoughLinesP(InputArray _src, OutputArray _lines, double rho, dou
|
||||
|
||||
void HoughLines( InputArray _image, OutputArray lines,
|
||||
double rho, double theta, int threshold,
|
||||
double srn, double stn, double min_theta, double max_theta )
|
||||
double srn, double stn, double min_theta, double max_theta, bool use_edgeval )
|
||||
{
|
||||
CV_INSTRUMENT_REGION();
|
||||
|
||||
@@ -922,7 +936,7 @@ void HoughLines( InputArray _image, OutputArray lines,
|
||||
ocl_HoughLines(_image, lines, rho, theta, threshold, min_theta, max_theta));
|
||||
|
||||
if( srn == 0 && stn == 0 )
|
||||
HoughLinesStandard(_image, lines, type, (float)rho, (float)theta, threshold, INT_MAX, min_theta, max_theta );
|
||||
HoughLinesStandard(_image, lines, type, (float)rho, (float)theta, threshold, INT_MAX, min_theta, max_theta, use_edgeval );
|
||||
else
|
||||
HoughLinesSDiv(_image, lines, type, (float)rho, (float)theta, threshold, cvRound(srn), cvRound(stn), INT_MAX, min_theta, max_theta);
|
||||
}
|
||||
|
||||
@@ -340,6 +340,53 @@ TEST(HoughLines, regression_21983)
|
||||
EXPECT_NEAR(lines[0][1], 1.57179642, 1e-4);
|
||||
}
|
||||
|
||||
TEST(WeightedHoughLines, horizontal)
|
||||
{
|
||||
Mat img(25, 25, CV_8UC1, Scalar(0));
|
||||
// draw lines. from top to bottom, stronger to weaker.
|
||||
line(img, Point(0, 6), Point(25, 6), Scalar(240));
|
||||
line(img, Point(0, 12), Point(25, 12), Scalar(255));
|
||||
line(img, Point(0, 18), Point(25, 18), Scalar(220));
|
||||
|
||||
// detect lines
|
||||
std::vector<Vec2f> lines;
|
||||
int threshold{220*25-1};
|
||||
bool use_edgeval{true};
|
||||
HoughLines(img, lines, 1, CV_PI/180, threshold, 0, 0, 0.0, CV_PI, use_edgeval);
|
||||
|
||||
// check results
|
||||
ASSERT_EQ(3U, lines.size());
|
||||
// detected lines is assumed sorted from stronger to weaker.
|
||||
EXPECT_EQ(12, lines[0][0]);
|
||||
EXPECT_EQ(6, lines[1][0]);
|
||||
EXPECT_EQ(18, lines[2][0]);
|
||||
EXPECT_NEAR(CV_PI/2, lines[0][1], CV_PI/180 + 1e-6);
|
||||
EXPECT_NEAR(CV_PI/2, lines[1][1], CV_PI/180 + 1e-6);
|
||||
EXPECT_NEAR(CV_PI/2, lines[2][1], CV_PI/180 + 1e-6);
|
||||
}
|
||||
|
||||
TEST(WeightedHoughLines, diagonal)
|
||||
{
|
||||
Mat img(25, 25, CV_8UC1, Scalar(0));
|
||||
// draw lines.
|
||||
line(img, Point(0, 0), Point(25, 25), Scalar(128));
|
||||
line(img, Point(0, 25), Point(25, 0), Scalar(255));
|
||||
|
||||
// detect lines
|
||||
std::vector<Vec2f> lines;
|
||||
int threshold{128*25-1};
|
||||
bool use_edgeval{true};
|
||||
HoughLines(img, lines, 1, CV_PI/180, threshold, 0, 0, 0.0, CV_PI, use_edgeval);
|
||||
|
||||
// check results
|
||||
ASSERT_EQ(2U, lines.size());
|
||||
// detected lines is assumed sorted from stronger to weaker.
|
||||
EXPECT_EQ(18, lines[0][0]); // 25*sqrt(2)/2 = 17.67 ~ 18
|
||||
EXPECT_EQ(0, lines[1][0]);
|
||||
EXPECT_NEAR(CV_PI/4, lines[0][1], CV_PI/180 + 1e-6);
|
||||
EXPECT_NEAR(CV_PI*3/4, lines[1][1], CV_PI/180 + 1e-6);
|
||||
}
|
||||
|
||||
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 ),
|
||||
|
||||
Reference in New Issue
Block a user