HoughLines_Demo.cpp 5.0 KB

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