Blame view

3rdparty/opencv-4.5.4/samples/cpp/tutorial_code/ImgTrans/houghcircles.cpp 1.68 KB
f4334277   Hu Chunming   提交3rdparty
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  /**
   * @file houghcircles.cpp
   * @brief This program demonstrates circle finding with the Hough transform
   */
  #include "opencv2/imgcodecs.hpp"
  #include "opencv2/highgui.hpp"
  #include "opencv2/imgproc.hpp"
  
  using namespace cv;
  using namespace std;
  
  int main(int argc, char** argv)
  {
      //![load]
      const char* filename = argc >=2 ? argv[1] : "smarties.png";
  
      // Loads an image
      Mat src = imread( samples::findFile( filename ), IMREAD_COLOR );
  
      // Check if image is loaded fine
      if(src.empty()){
          printf(" Error opening image\n");
          printf(" Program Arguments: [image_name -- default %s] \n", filename);
          return EXIT_FAILURE;
      }
      //![load]
  
      //![convert_to_gray]
      Mat gray;
      cvtColor(src, gray, COLOR_BGR2GRAY);
      //![convert_to_gray]
  
      //![reduce_noise]
      medianBlur(gray, gray, 5);
      //![reduce_noise]
  
      //![houghcircles]
      vector<Vec3f> circles;
      HoughCircles(gray, circles, HOUGH_GRADIENT, 1,
                   gray.rows/16,  // change this value to detect circles with different distances to each other
                   100, 30, 1, 30 // change the last two parameters
              // (min_radius & max_radius) to detect larger circles
      );
      //![houghcircles]
  
      //![draw]
      for( size_t i = 0; i < circles.size(); i++ )
      {
          Vec3i c = circles[i];
          Point center = Point(c[0], c[1]);
          // circle center
          circle( src, center, 1, Scalar(0,100,100), 3, LINE_AA);
          // circle outline
          int radius = c[2];
          circle( src, center, radius, Scalar(255,0,255), 3, LINE_AA);
      }
      //![draw]
  
      //![display]
      imshow("detected circles", src);
      waitKey();
      //![display]
  
      return EXIT_SUCCESS;
  }