Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Feature: weighted hough#21407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
25 commits
Select commitHold shift + click to select a range
1864bbb
implement weightedHough using edge value in HoughLines().
MasahiroOgawaDec 9, 2021
472d838
revise original update for cv_assert.
MasahiroOgawaDec 21, 2021
ad5ecd3
add sample code to test weighted Hough.
MasahiroOgawaJan 6, 2022
c73ea76
Merge remote-tracking branch 'upstream/4.x' into feature/weighted_hough
MasahiroOgawaJan 6, 2022
25d52a8
update doc.
MasahiroOgawaJan 7, 2022
6586140
update weighted hough doc.
MasahiroOgawaJan 7, 2022
e9ca863
revise weighted hough to avoid if() in for.
MasahiroOgawaJan 11, 2022
bb147e9
Merge remote-tracking branch 'origin/4.x' into feature/weighted_hough
MasahiroOgawaNov 28, 2022
01c378f
fix HoughLines_Demo.cpp buid issue.
MasahiroOgawaDec 2, 2022
951221b
add unit test for weighte Hough.
MasahiroOgawaDec 20, 2022
5aa09c3
add unit test for weighte Hough.
MasahiroOgawaDec 20, 2022
d2ecd6a
Merge branch 'feature/weighted_hough' of github.com:sensyn-robotics/s…
MasahiroOgawaDec 20, 2022
691aee4
revise based on the asmorkalov comments.
MasahiroOgawaDec 20, 2022
9d508c0
Merge branch 'mas/tmp_weightedhough' into feature/weighted_hough
MasahiroOgawaDec 20, 2022
651ade0
Merge branch '4.x' into feature/weighted_hough
MasahiroOgawaSep 12, 2023
fc68b85
Merge branch '4.x' into feature/weighted_hough
MasahiroOgawaOct 11, 2023
82cffa6
Merge remote-tracking branch 'origin/4.x' into feature/weighted_hough
MasahiroOgawaDec 12, 2024
4ec3ffe
fix the variables order in **_EQ, and **_NEAR of weighted hough test …
MasahiroOgawaDec 12, 2024
2cde5f9
fix for weighted diagonal test too; change order in *_EQ() variable o…
MasahiroOgawaDec 12, 2024
0f7c990
fix weighted hough unit test. as it will success on weighted hough, b…
MasahiroOgawaDec 12, 2024
712a418
Merge remote-tracking branch 'origin/4.x' into feature/weighted_hough
MasahiroOgawaDec 16, 2024
c27c57e
fix unit test to success for current weighted hough, but fail for nor…
MasahiroOgawaDec 16, 2024
96137fe
fix warning: comparison of integer expressions of different signednes…
MasahiroOgawaDec 20, 2024
c29b0bb
fix CI build error.
MasahiroOgawaJan 6, 2025
1a2b034
remove white space based on CI buildAndTest error.
MasahiroOgawaJan 6, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -83,7 +83,7 @@ Arranging the terms: \f$r = x \cos \theta + y \sin \theta\f$

### Standard and Probabilistic Hough Line Transform

OpenCV implementstwo kind of Hough Line Transforms:
OpenCV implementsthree kind of Hough Line Transforms:

a. **The Standard Hough Transform**

Expand All@@ -97,6 +97,12 @@ b. **The Probabilistic Hough Line Transform**
of the detected lines \f$(x_{0}, y_{0}, x_{1}, y_{1})\f$
- In OpenCV it is implemented with the function **HoughLinesP()**

c. **The Weighted Hough Transform**

- Uses edge intensity instead binary 0 or 1 values in standard Hough transform.
- In OpenCV it is implemented with the function **HoughLines()** with use_edgeval=true.
- See the example in samples/cpp/tutorial_code/ImgTrans/HoughLines_Demo.cpp.

### What does this program do?
- Loads an image
- Applies a *Standard Hough Line Transform* and a *Probabilistic Line Transform*.
Expand Down
4 changes: 3 additions & 1 deletionmodules/imgproc/include/opencv2/imgproc.hpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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.

Expand Down
42 changes: 28 additions & 14 deletionsmodules/imgproc/src/hough.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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");

Expand DownExpand Up@@ -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 );
Expand DownExpand Up@@ -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();

Expand All@@ -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);
}
Expand Down
47 changes: 47 additions & 0 deletionsmodules/imgproc/test/test_houghlines.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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 ),
Expand Down
69 changes: 58 additions & 11 deletionssamples/cpp/tutorial_code/ImgTrans/HoughLines_Demo.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,22 +15,27 @@ using namespace std;
/// Global variables

/** General variables */
Mat src,edges;
Mat src,canny_edge, sobel_edge;
Mat src_gray;
Mat standard_hough, probabilistic_hough;
Mat standard_hough, probabilistic_hough, weighted_hough;
int min_threshold = 50;
int max_trackbar = 150;
int weightedhough_max_trackbar = 100000;

const char* standard_name = "Standard Hough Lines Demo";
const char* probabilistic_name = "Probabilistic Hough Lines Demo";
const char* weighted_name = "Weighted Hough Lines Demo";

int s_trackbar = max_trackbar;
int p_trackbar = max_trackbar;
int e_trackbar = 60;
int w_trackbar = 60000;

/// Function Headers
void help();
void Standard_Hough( int, void* );
void Probabilistic_Hough( int, void* );
void Weighted_Hough( int, void* );

/**
* @function main
Expand All@@ -53,22 +58,29 @@ int main( int argc, char** argv )
/// Pass the image to gray
cvtColor( src, src_gray, COLOR_RGB2GRAY );

/// Apply Canny edge detector
Canny( src_gray, edges, 50, 200, 3 );
/// Apply Canny/Sobel edge detector
Canny( src_gray, canny_edge, 50, 200, 3 );
Sobel( src_gray, sobel_edge, CV_16S, 1, 0 ); // dx(order of the derivative x)=1,dy=0

/// Create Trackbars for Thresholds
char thresh_label[50];
snprintf( thresh_label, sizeof(thresh_label), "Thres: %d + input", min_threshold );

namedWindow( standard_name, WINDOW_AUTOSIZE );
createTrackbar( thresh_label, standard_name, &s_trackbar, max_trackbar, Standard_Hough);
createTrackbar( thresh_label, standard_name, &s_trackbar, max_trackbar, Standard_Hough);

namedWindow( probabilistic_name, WINDOW_AUTOSIZE );
createTrackbar( thresh_label, probabilistic_name, &p_trackbar, max_trackbar, Probabilistic_Hough);
createTrackbar( thresh_label, probabilistic_name, &p_trackbar, max_trackbar, Probabilistic_Hough );

char edge_thresh_label[50];
sprintf( edge_thresh_label, "Edge Thres: input" );
namedWindow( weighted_name, WINDOW_AUTOSIZE);
createTrackbar( edge_thresh_label, weighted_name, &e_trackbar, max_trackbar, Weighted_Hough);
createTrackbar( thresh_label, weighted_name, &w_trackbar, weightedhough_max_trackbar, Weighted_Hough);

/// Initialize
Standard_Hough(0, 0);
Probabilistic_Hough(0, 0);
Weighted_Hough(0, 0);
waitKey(0);
return 0;
}
Expand All@@ -90,10 +102,10 @@ void help()
void Standard_Hough( int, void* )
{
vector<Vec2f> s_lines;
cvtColor(edges, standard_hough, COLOR_GRAY2BGR );
cvtColor(canny_edge, standard_hough, COLOR_GRAY2BGR );

/// 1. Use Standard Hough Transform
HoughLines(edges, s_lines, 1, CV_PI/180, min_threshold + s_trackbar, 0, 0 );
HoughLines(canny_edge, s_lines, 1, CV_PI/180, min_threshold + s_trackbar, 0, 0 );

/// Show the result
for( size_t i = 0; i < s_lines.size(); i++ )
Expand All@@ -117,10 +129,10 @@ void Standard_Hough( int, void* )
void Probabilistic_Hough( int, void* )
{
vector<Vec4i> p_lines;
cvtColor(edges, probabilistic_hough, COLOR_GRAY2BGR );
cvtColor(canny_edge, probabilistic_hough, COLOR_GRAY2BGR );

/// 2. Use Probabilistic Hough Transform
HoughLinesP(edges, p_lines, 1, CV_PI/180, min_threshold + p_trackbar, 30, 10 );
HoughLinesP(canny_edge, p_lines, 1, CV_PI/180, min_threshold + p_trackbar, 30, 10 );

/// Show the result
for( size_t i = 0; i < p_lines.size(); i++ )
Expand All@@ -131,3 +143,38 @@ void Probabilistic_Hough( int, void* )

imshow( probabilistic_name, probabilistic_hough );
}

/**
* @function Weighted_Hough
* This can detect lines based on the edge intensities.
*/
void Weighted_Hough( int, void* )
{
vector<Vec2f> s_lines;

/// prepare
Mat edge_img;
convertScaleAbs(sobel_edge, edge_img );
// use same threshold for edge with Hough.
threshold( edge_img, edge_img, e_trackbar, 255, cv::THRESH_TOZERO);
cvtColor( edge_img, weighted_hough, COLOR_GRAY2BGR );

/// 3. Use Weighted Hough Transform
const bool use_edgeval{true};
HoughLines( edge_img, s_lines, 1, CV_PI/180, min_threshold + w_trackbar, 0, 0, 0, CV_PI, use_edgeval);

/// Show the result
for( size_t i = 0; i < s_lines.size(); i++ )
{
float r = s_lines[i][0], t = s_lines[i][1];
double cos_t = cos(t), sin_t = sin(t);
double x0 = r*cos_t, y0 = r*sin_t;
double alpha = 1000;

Point pt1( cvRound(x0 + alpha*(-sin_t)), cvRound(y0 + alpha*cos_t) );
Point pt2( cvRound(x0 - alpha*(-sin_t)), cvRound(y0 - alpha*cos_t) );
line( weighted_hough, pt1, pt2, Scalar(255,0,0), 3, LINE_AA );
}

imshow( weighted_name, weighted_hough );
}
Loading

[8]ページ先頭

©2009-2025 Movatter.jp