Advertisement
jyun14

HoughLines

Nov 24th, 2014
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. /**
  2.  * @file HoughLines_Demo.cpp
  3.  * @brief Demo code for Hough Transform
  4.  * @author OpenCV team
  5.  */
  6.  
  7. #include "opencv2/highgui/highgui.hpp"
  8. #include "opencv2/imgproc/imgproc.hpp"
  9. #include <iostream>
  10. #include <stdio.h>
  11.  
  12. using namespace cv;
  13. using namespace std;
  14.  
  15. /// Global variables
  16.  
  17. /** General variables */
  18. Mat src, edges;
  19. Mat src_gray;
  20. Mat standard_hough, probabilistic_hough;
  21. int min_threshold = 50;
  22. int max_trackbar = 150;
  23.  
  24. const char* standard_name = "Standard Hough Lines Demo";
  25. const char* probabilistic_name = "Probabilistic Hough Lines Demo";
  26.  
  27. int s_trackbar = max_trackbar;
  28. int p_trackbar = max_trackbar;
  29.  
  30. /// Function Headers
  31. void help();
  32. void Standard_Hough( int, void* );
  33. void Probabilistic_Hough( int, void* );
  34.  
  35. /**
  36.  * @function main
  37.  */
  38. int main( int, char** argv )
  39. {
  40.    /// Read the image
  41.    src = imread( argv[1], 1 );
  42.  
  43.    if( src.empty() )
  44.      { help();
  45.        return -1;
  46.      }
  47.  
  48.    /// Pass the image to gray
  49.    cvtColor( src, src_gray, COLOR_RGB2GRAY );
  50.  
  51.    /// Apply Canny edge detector
  52.    Canny( src_gray, edges, 50, 200, 3 );
  53.  
  54.    /// Create Trackbars for Thresholds
  55.    char thresh_label[50];
  56.    sprintf( thresh_label, "Thres: %d + input", min_threshold );
  57.  
  58.    namedWindow( standard_name, WINDOW_AUTOSIZE );
  59.    createTrackbar( thresh_label, standard_name, &s_trackbar, max_trackbar, Standard_Hough);
  60.  
  61.    namedWindow( probabilistic_name, WINDOW_AUTOSIZE );
  62.    createTrackbar( thresh_label, probabilistic_name, &p_trackbar, max_trackbar, Probabilistic_Hough);
  63.  
  64.    /// Initialize
  65.    Standard_Hough(0, 0);
  66.    Probabilistic_Hough(0, 0);
  67.    waitKey(0);
  68.    return 0;
  69. }
  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. /**
  83.  * @function Standard_Hough
  84.  */
  85. void Standard_Hough( int, void* )
  86. {
  87.   vector<Vec2f> s_lines;
  88.   cvtColor( edges, standard_hough, CV_GRAY2BGR );
  89.  
  90.   /// 1. Use Standard Hough Transform
  91.   HoughLines( edges, s_lines, 1, CV_PI/180, min_threshold + s_trackbar, 0, 0 );
  92.  
  93.   /// Show the result
  94.   for( size_t i = 0; i < s_lines.size(); i++ )
  95.      {
  96.       float r = s_lines[i][0], t = s_lines[i][1];
  97.       double cos_t = cos(t), sin_t = sin(t);
  98.       double x0 = r*cos_t, y0 = r*sin_t;
  99.       double alpha = 1000;
  100.  
  101.        Point pt1( cvRound(x0 + alpha*(-sin_t)), cvRound(y0 + alpha*cos_t) );
  102.        Point pt2( cvRound(x0 - alpha*(-sin_t)), cvRound(y0 - alpha*cos_t) );
  103.        line( standard_hough, pt1, pt2, Scalar(255,0,0), 3, CV_AA);
  104.      }
  105.  
  106.    imshow( standard_name, standard_hough );
  107. }
  108.  
  109. /**
  110.  * @function Probabilistic_Hough
  111.  */
  112. void Probabilistic_Hough( int, void* )
  113. {
  114.   vector<Vec4i> p_lines;
  115.   cvtColor( edges, probabilistic_hough, CV_GRAY2BGR );
  116.  
  117.   /// 2. Use Probabilistic Hough Transform
  118.   HoughLinesP( edges, p_lines, 1, CV_PI/180, min_threshold + p_trackbar, 30, 10 );
  119.  
  120.   /// Show the result
  121.   for( size_t i = 0; i < p_lines.size(); i++ )
  122.      {
  123.        Vec4i l = p_lines[i];
  124.        line( probabilistic_hough, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(255,0,0), 3, CV_AA);
  125.      }
  126.  
  127.    imshow( probabilistic_name, probabilistic_hough );
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement