Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork56.4k
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
Uh oh!
There was an error while loading.Please reload this page.
Feature: weighted hough#21407
Changes fromall commits
1864bbb472d838ad5ecd3c73ea7625d52a86586140e9ca863bb147e901c378f951221b5aa09c3d2ecd6a691aee49d508c0651ade0fc68b8582cffa64ec3ffe2cde5f90f7c990712a418c27c57e96137fec29b0bb1a2b034File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -83,7 +83,7 @@ Arranging the terms: \f$r = x \cos \theta + y \sin \theta\f$ | ||
| ### Standard and Probabilistic Hough Line Transform | ||
| OpenCV implementsthree kind of Hough Line Transforms: | ||
| a. **The Standard Hough Transform** | ||
| @@ -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** | ||
asmorkalov marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| - 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*. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
asmorkalov marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| // 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 ), | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -15,22 +15,27 @@ using namespace std; | ||
| /// Global variables | ||
| /** General variables */ | ||
| Mat src,canny_edge, sobel_edge; | ||
| Mat src_gray; | ||
| 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 | ||
| @@ -53,22 +58,29 @@ int main( int argc, char** argv ) | ||
| /// Pass the image to gray | ||
| cvtColor( src, src_gray, COLOR_RGB2GRAY ); | ||
| /// 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); | ||
| namedWindow( probabilistic_name, WINDOW_AUTOSIZE ); | ||
| 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; | ||
| } | ||
| @@ -90,10 +102,10 @@ void help() | ||
| void Standard_Hough( int, void* ) | ||
| { | ||
| vector<Vec2f> s_lines; | ||
| cvtColor(canny_edge, standard_hough, COLOR_GRAY2BGR ); | ||
| /// 1. Use Standard Hough Transform | ||
| 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++ ) | ||
| @@ -117,10 +129,10 @@ void Standard_Hough( int, void* ) | ||
| void Probabilistic_Hough( int, void* ) | ||
| { | ||
| vector<Vec4i> p_lines; | ||
| cvtColor(canny_edge, probabilistic_hough, COLOR_GRAY2BGR ); | ||
| /// 2. Use Probabilistic Hough Transform | ||
| 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++ ) | ||
| @@ -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 ); | ||
asmorkalov marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| } | ||
| imshow( weighted_name, weighted_hough ); | ||
| } | ||
Uh oh!
There was an error while loading.Please reload this page.