imgproc_HoughLinesCircles.cpp 999 B

123456789101112131415161718192021222324252627282930313233
  1. #include <opencv2/imgproc.hpp>
  2. #include <opencv2/highgui.hpp>
  3. #include <math.h>
  4. using namespace cv;
  5. using namespace std;
  6. int main(int argc, char** argv)
  7. {
  8. Mat img, gray;
  9. if( argc != 2 || !(img=imread(argv[1], 1)).data)
  10. return -1;
  11. cvtColor(img, gray, COLOR_BGR2GRAY);
  12. // smooth it, otherwise a lot of false circles may be detected
  13. GaussianBlur( gray, gray, Size(9, 9), 2, 2 );
  14. vector<Vec3f> circles;
  15. HoughCircles(gray, circles, HOUGH_GRADIENT,
  16. 2, gray.rows/4, 200, 100 );
  17. for( size_t i = 0; i < circles.size(); i++ )
  18. {
  19. Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
  20. int radius = cvRound(circles[i][2]);
  21. // draw the circle center
  22. circle( img, center, 3, Scalar(0,255,0), -1, 8, 0 );
  23. // draw the circle outline
  24. circle( img, center, radius, Scalar(0,0,255), 3, 8, 0 );
  25. }
  26. namedWindow( "circles", 1 );
  27. imshow( "circles", img );
  28. waitKey(0);
  29. return 0;
  30. }