HoughLines_Demo.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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, edges;
  15. Mat src_gray;
  16. Mat standard_hough, probabilistic_hough;
  17. int min_threshold = 50;
  18. int max_trackbar = 150;
  19. const char* standard_name = "Standard Hough Lines Demo";
  20. const char* probabilistic_name = "Probabilistic Hough Lines Demo";
  21. int s_trackbar = max_trackbar;
  22. int p_trackbar = max_trackbar;
  23. /// Function Headers
  24. void help();
  25. void Standard_Hough( int, void* );
  26. void Probabilistic_Hough( int, void* );
  27. /**
  28. * @function main
  29. */
  30. int main( int argc, char** argv )
  31. {
  32. // Read the image
  33. String imageName("building.jpg"); // by default
  34. if (argc > 1)
  35. {
  36. imageName = argv[1];
  37. }
  38. src = imread( samples::findFile( imageName ), IMREAD_COLOR );
  39. if( src.empty() )
  40. { help();
  41. return -1;
  42. }
  43. /// Pass the image to gray
  44. cvtColor( src, src_gray, COLOR_RGB2GRAY );
  45. /// Apply Canny edge detector
  46. Canny( src_gray, edges, 50, 200, 3 );
  47. /// Create Trackbars for Thresholds
  48. char thresh_label[50];
  49. snprintf( thresh_label, sizeof(thresh_label), "Thres: %d + input", min_threshold );
  50. namedWindow( standard_name, WINDOW_AUTOSIZE );
  51. createTrackbar( thresh_label, standard_name, &s_trackbar, max_trackbar, Standard_Hough);
  52. namedWindow( probabilistic_name, WINDOW_AUTOSIZE );
  53. createTrackbar( thresh_label, probabilistic_name, &p_trackbar, max_trackbar, Probabilistic_Hough);
  54. /// Initialize
  55. Standard_Hough(0, 0);
  56. Probabilistic_Hough(0, 0);
  57. waitKey(0);
  58. return 0;
  59. }
  60. /**
  61. * @function help
  62. * @brief Indications of how to run this program and why is it for
  63. */
  64. void help()
  65. {
  66. printf("\t Hough Transform to detect lines \n ");
  67. printf("\t---------------------------------\n ");
  68. printf(" Usage: ./HoughLines_Demo <image_name> \n");
  69. }
  70. /**
  71. * @function Standard_Hough
  72. */
  73. void Standard_Hough( int, void* )
  74. {
  75. vector<Vec2f> s_lines;
  76. cvtColor( edges, standard_hough, COLOR_GRAY2BGR );
  77. /// 1. Use Standard Hough Transform
  78. HoughLines( edges, s_lines, 1, CV_PI/180, min_threshold + s_trackbar, 0, 0 );
  79. /// Show the result
  80. for( size_t i = 0; i < s_lines.size(); i++ )
  81. {
  82. float r = s_lines[i][0], t = s_lines[i][1];
  83. double cos_t = cos(t), sin_t = sin(t);
  84. double x0 = r*cos_t, y0 = r*sin_t;
  85. double alpha = 1000;
  86. Point pt1( cvRound(x0 + alpha*(-sin_t)), cvRound(y0 + alpha*cos_t) );
  87. Point pt2( cvRound(x0 - alpha*(-sin_t)), cvRound(y0 - alpha*cos_t) );
  88. line( standard_hough, pt1, pt2, Scalar(255,0,0), 3, LINE_AA);
  89. }
  90. imshow( standard_name, standard_hough );
  91. }
  92. /**
  93. * @function Probabilistic_Hough
  94. */
  95. void Probabilistic_Hough( int, void* )
  96. {
  97. vector<Vec4i> p_lines;
  98. cvtColor( edges, probabilistic_hough, COLOR_GRAY2BGR );
  99. /// 2. Use Probabilistic Hough Transform
  100. HoughLinesP( edges, p_lines, 1, CV_PI/180, min_threshold + p_trackbar, 30, 10 );
  101. /// Show the result
  102. for( size_t i = 0; i < p_lines.size(); i++ )
  103. {
  104. Vec4i l = p_lines[i];
  105. line( probabilistic_hough, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(255,0,0), 3, LINE_AA);
  106. }
  107. imshow( probabilistic_name, probabilistic_hough );
  108. }