HoughCircle_Demo.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * @file HoughCircle_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 std;
  11. using namespace cv;
  12. namespace
  13. {
  14. // windows and trackbars name
  15. const std::string windowName = "Hough Circle Detection Demo";
  16. const std::string cannyThresholdTrackbarName = "Canny threshold";
  17. const std::string accumulatorThresholdTrackbarName = "Accumulator Threshold";
  18. // initial and max values of the parameters of interests.
  19. const int cannyThresholdInitialValue = 100;
  20. const int accumulatorThresholdInitialValue = 50;
  21. const int maxAccumulatorThreshold = 200;
  22. const int maxCannyThreshold = 255;
  23. void HoughDetection(const Mat& src_gray, const Mat& src_display, int cannyThreshold, int accumulatorThreshold)
  24. {
  25. // will hold the results of the detection
  26. std::vector<Vec3f> circles;
  27. // runs the actual detection
  28. HoughCircles( src_gray, circles, HOUGH_GRADIENT, 1, src_gray.rows/8, cannyThreshold, accumulatorThreshold, 0, 0 );
  29. // clone the colour, input image for displaying purposes
  30. Mat display = src_display.clone();
  31. for( size_t i = 0; i < circles.size(); i++ )
  32. {
  33. Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
  34. int radius = cvRound(circles[i][2]);
  35. // circle center
  36. circle( display, center, 3, Scalar(0,255,0), -1, 8, 0 );
  37. // circle outline
  38. circle( display, center, radius, Scalar(0,0,255), 3, 8, 0 );
  39. }
  40. // shows the results
  41. imshow( windowName, display);
  42. }
  43. }
  44. int main(int argc, char** argv)
  45. {
  46. Mat src, src_gray;
  47. // Read the image
  48. String imageName("stuff.jpg"); // by default
  49. if (argc > 1)
  50. {
  51. imageName = argv[1];
  52. }
  53. src = imread( samples::findFile( imageName ), IMREAD_COLOR );
  54. if( src.empty() )
  55. {
  56. std::cerr << "Invalid input image\n";
  57. std::cout << "Usage : " << argv[0] << " <path_to_input_image>\n";;
  58. return -1;
  59. }
  60. // Convert it to gray
  61. cvtColor( src, src_gray, COLOR_BGR2GRAY );
  62. // Reduce the noise so we avoid false circle detection
  63. GaussianBlur( src_gray, src_gray, Size(9, 9), 2, 2 );
  64. //declare and initialize both parameters that are subjects to change
  65. int cannyThreshold = cannyThresholdInitialValue;
  66. int accumulatorThreshold = accumulatorThresholdInitialValue;
  67. // create the main window, and attach the trackbars
  68. namedWindow( windowName, WINDOW_AUTOSIZE );
  69. createTrackbar(cannyThresholdTrackbarName, windowName, &cannyThreshold,maxCannyThreshold);
  70. createTrackbar(accumulatorThresholdTrackbarName, windowName, &accumulatorThreshold, maxAccumulatorThreshold);
  71. // infinite loop to display
  72. // and refresh the content of the output image
  73. // until the user presses q or Q
  74. char key = 0;
  75. while(key != 'q' && key != 'Q')
  76. {
  77. // those parameters cannot be =0
  78. // so we must check here
  79. cannyThreshold = std::max(cannyThreshold, 1);
  80. accumulatorThreshold = std::max(accumulatorThreshold, 1);
  81. //runs the detection, and update the display
  82. HoughDetection(src_gray, src, cannyThreshold, accumulatorThreshold);
  83. // get user key
  84. key = (char)waitKey(10);
  85. }
  86. return 0;
  87. }