1 /**
2  * @file HoughLines_Demo.cpp
3  * @brief Demo code for Hough Transform
4  * @author OpenCV team
5  */
6 
7 #include "opencv2/imgcodecs.hpp"
8 #include "opencv2/highgui/highgui.hpp"
9 #include "opencv2/imgproc/imgproc.hpp"
10 #include <iostream>
11 #include <stdio.h>
12 
13 using namespace cv;
14 using namespace std;
15 
16 /// Global variables
17 
18 /** General variables */
19 Mat src, edges;
20 Mat src_gray;
21 Mat standard_hough, probabilistic_hough;
22 int min_threshold = 50;
23 int max_trackbar = 150;
24 
25 const char* standard_name = "Standard Hough Lines Demo";
26 const char* probabilistic_name = "Probabilistic Hough Lines Demo";
27 
28 int s_trackbar = max_trackbar;
29 int p_trackbar = max_trackbar;
30 
31 /// Function Headers
32 void help();
33 void Standard_Hough( int, void* );
34 void Probabilistic_Hough( int, void* );
35 
36 /**
37  * @function main
38  */
main(int,char ** argv)39 int main( int, char** argv )
40 {
41    /// Read the image
42    src = imread( argv[1], 1 );
43 
44    if( src.empty() )
45      { help();
46        return -1;
47      }
48 
49    /// Pass the image to gray
50    cvtColor( src, src_gray, COLOR_RGB2GRAY );
51 
52    /// Apply Canny edge detector
53    Canny( src_gray, edges, 50, 200, 3 );
54 
55    /// Create Trackbars for Thresholds
56    char thresh_label[50];
57    sprintf( thresh_label, "Thres: %d + input", min_threshold );
58 
59    namedWindow( standard_name, WINDOW_AUTOSIZE );
60    createTrackbar( thresh_label, standard_name, &s_trackbar, max_trackbar, Standard_Hough);
61 
62    namedWindow( probabilistic_name, WINDOW_AUTOSIZE );
63    createTrackbar( thresh_label, probabilistic_name, &p_trackbar, max_trackbar, Probabilistic_Hough);
64 
65    /// Initialize
66    Standard_Hough(0, 0);
67    Probabilistic_Hough(0, 0);
68    waitKey(0);
69    return 0;
70 }
71 
72 /**
73  * @function help
74  * @brief Indications of how to run this program and why is it for
75  */
help()76 void help()
77 {
78   printf("\t Hough Transform to detect lines \n ");
79   printf("\t---------------------------------\n ");
80   printf(" Usage: ./HoughLines_Demo <image_name> \n");
81 }
82 
83 /**
84  * @function Standard_Hough
85  */
Standard_Hough(int,void *)86 void Standard_Hough( int, void* )
87 {
88   vector<Vec2f> s_lines;
89   cvtColor( edges, standard_hough, COLOR_GRAY2BGR );
90 
91   /// 1. Use Standard Hough Transform
92   HoughLines( edges, s_lines, 1, CV_PI/180, min_threshold + s_trackbar, 0, 0 );
93 
94   /// Show the result
95   for( size_t i = 0; i < s_lines.size(); i++ )
96      {
97       float r = s_lines[i][0], t = s_lines[i][1];
98       double cos_t = cos(t), sin_t = sin(t);
99       double x0 = r*cos_t, y0 = r*sin_t;
100       double alpha = 1000;
101 
102        Point pt1( cvRound(x0 + alpha*(-sin_t)), cvRound(y0 + alpha*cos_t) );
103        Point pt2( cvRound(x0 - alpha*(-sin_t)), cvRound(y0 - alpha*cos_t) );
104        line( standard_hough, pt1, pt2, Scalar(255,0,0), 3, LINE_AA);
105      }
106 
107    imshow( standard_name, standard_hough );
108 }
109 
110 /**
111  * @function Probabilistic_Hough
112  */
Probabilistic_Hough(int,void *)113 void Probabilistic_Hough( int, void* )
114 {
115   vector<Vec4i> p_lines;
116   cvtColor( edges, probabilistic_hough, COLOR_GRAY2BGR );
117 
118   /// 2. Use Probabilistic Hough Transform
119   HoughLinesP( edges, p_lines, 1, CV_PI/180, min_threshold + p_trackbar, 30, 10 );
120 
121   /// Show the result
122   for( size_t i = 0; i < p_lines.size(); i++ )
123      {
124        Vec4i l = p_lines[i];
125        line( probabilistic_hough, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(255,0,0), 3, LINE_AA);
126      }
127 
128    imshow( probabilistic_name, probabilistic_hough );
129 }
130