Advertisement
Guest User

code

a guest
Aug 30th, 2014
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. #include <iostream>
  2. #include "opencv2/highgui/highgui.hpp"
  3. #include "opencv2/imgproc/imgproc.hpp"
  4.  
  5. using namespace cv;
  6. using namespace std;
  7. int hi = 1, lo = 1;
  8.  
  9.  
  10. void detect1(Mat src)
  11. {
  12. Mat src_gray;
  13.  
  14.  
  15.  
  16. /// Convert it to gray
  17. cvtColor( src, src_gray, CV_BGR2GRAY );
  18.  
  19. /// Reduce the noise so we avoid false circle detection
  20. GaussianBlur( src_gray, src_gray, Size(9, 9), 2, 2 );
  21.  
  22. vector<Vec3f> circles;
  23.  
  24. /// Apply the Hough Transform to find the circles
  25. // HoughCircles( src_gray, circles, CV_HOUGH_GRADIENT, 1, src_gray.rows/8, 200, 100, 0, 0 );
  26. HoughCircles(src_gray, circles, CV_HOUGH_GRADIENT, 1, 10,
  27. 100, 30, 30, 150 // change the last two parameters
  28. // (min_radius & max_radius) to detect larger circles
  29. );
  30.  
  31. /// Draw the circles detected
  32. for( size_t i = 0; i < circles.size(); i++ )
  33. {
  34. Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
  35. int radius = cvRound(circles[i][2]);
  36. // circle center
  37. circle( src, center, 3, Scalar(0,255,0), -1, 8, 0 );
  38. // circle outline
  39. circle( src, center, radius, Scalar(0,0,255), 3, 8, 0 );
  40. }
  41.  
  42. /// Show your results
  43.  
  44. imshow( "Hough Circle Transform Demo", src );
  45.  
  46. waitKey(0);
  47.  
  48. }
  49.  
  50. int main( int argc, char** argv ){
  51.  
  52. VideoCapture cap("http://192.168.1.123:8080/video?x.mjpeg"); //capture the video from web cam
  53. waitKey(5000);
  54.  
  55. if ( !cap.isOpened() ) // if not success, exit program
  56. {
  57. cout << "Cannot open the web cam" << endl;
  58. return -1;
  59. }
  60.  
  61. namedWindow("Original", CV_WINDOW_AUTOSIZE); //create a window called "Control"
  62. namedWindow( "Hough Circle Transform Demo", CV_WINDOW_AUTOSIZE );
  63.  
  64.  
  65.  
  66. while (true)
  67. {
  68. Mat imgOriginal;
  69.  
  70. bool bSuccess = cap.read(imgOriginal); // read a new frame from video
  71.  
  72. if (!bSuccess) //if not success, break loop
  73. {
  74. cout << "Cannot read a frame from video stream" << endl;
  75. break;
  76. }
  77.  
  78. Mat src =imgOriginal;
  79.  
  80.  
  81. Mat src_gray;
  82.  
  83.  
  84.  
  85. /// Convert it to gray
  86. cvtColor( src, src_gray, CV_BGR2GRAY );
  87.  
  88. /// Reduce the noise so we avoid false circle detection
  89. GaussianBlur( src_gray, src_gray, Size(9, 9), 2, 2 );
  90.  
  91. vector<Vec3f> circles;
  92.  
  93. /// Apply the Hough Transform to find the circles
  94. // HoughCircles( src_gray, circles, CV_HOUGH_GRADIENT, 1, src_gray.rows/8, 10, 100,300, 1000 );
  95. // HoughCircles(src_gray, circles, CV_HOUGH_GRADIENT, 1, 10,100, 30, 30, 150);
  96. HoughCircles(src_gray, circles, CV_HOUGH_GRADIENT,2,src_gray.rows/4,100,50,5,400);
  97.  
  98.  
  99. /// Draw the circles detected
  100. for( size_t i = 0; i < circles.size(); i++ )
  101. {
  102. Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
  103. int radius = cvRound(circles[i][2]);
  104. // circle center
  105. circle( src, center, 3, Scalar(0,255,0), -1, 8, 0 );
  106. // circle outline
  107. circle( src, center, radius, Scalar(0,0,255), 3, 8, 0 );
  108. }
  109.  
  110. /// Show your results
  111.  
  112. imshow( "Hough Circle Transform Demo", src );
  113. imshow("Original", imgOriginal); //show the original image
  114.  
  115.  
  116.  
  117. if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
  118. {
  119. cout << "esc key is pressed by user" << endl;
  120. break;
  121. }
  122. }
  123.  
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement