/circledetect.cpp

https://github.com/jlsync/detector · C++ · 59 lines · 37 code · 9 blank · 13 comment · 7 complexity · 32ba46f43efc250317edc3f82c79999f MD5 · raw file

  1. /*
  2. * usageage:
  3. *
  4. * for just the numbers run
  5. * circledetect infile.jpg
  6. *
  7. * for the numbers and modified image run
  8. * circledetect infile.jpg outfile.jpg
  9. *
  10. * for the numbers and modified image run and onscreen display
  11. * circledetect infile.jpg outfile.jpg blah
  12. *
  13. */
  14. #include "cv.h"
  15. #include "highgui.h"
  16. #include "math.h"
  17. #include <iostream>
  18. #include <stdio.h>
  19. #include <math.h>
  20. #include <string.h>
  21. using namespace std;
  22. int main(int argc, char** argv)
  23. {
  24. IplImage* img;
  25. if( argc >= 2 && (img=cvLoadImage(argv[1], 1))!= 0)
  26. {
  27. IplImage* gray = cvCreateImage( cvGetSize(img), 8, 1 );
  28. CvMemStorage* storage = cvCreateMemStorage(0);
  29. cvCvtColor( img, gray, CV_BGR2GRAY );
  30. cvSmooth( gray, gray, CV_GAUSSIAN, 9, 9 ); // smooth it, otherwise a lot of false circles may be detected
  31. CvSeq* circles = cvHoughCircles( gray, storage, CV_HOUGH_GRADIENT, 2, gray->height/4, 200, 100 );
  32. int i;
  33. for( i = 0; i < circles->total; i++ )
  34. {
  35. float* p = (float*)cvGetSeqElem( circles, i );
  36. cout <<"x="<<p[0]<<" / y="<<p[1]<<" radius="<<p[2]<<endl;
  37. cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])), 3, CV_RGB(0,255,0), -1, 8, 0 );
  38. cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])), cvRound(p[2]), CV_RGB(255,0,0), 3, 8, 0 );
  39. }
  40. if( argc == 3 ) {
  41. cvSaveImage(argv[2] ,img);
  42. }
  43. if( argc == 4 ) {
  44. cvNamedWindow( "circles", 1 );
  45. cvShowImage( "circles", img );
  46. sleep(10);
  47. }
  48. }
  49. return 0;
  50. }