Merge pull request #3103 from vpisarev:core_imgproc_optim_rearrangements
[profile/ivi/opencv.git] / samples / cpp / houghcircles.cpp
1 #include "opencv2/imgcodecs.hpp"
2 #include "opencv2/highgui/highgui.hpp"
3 #include "opencv2/imgproc/imgproc.hpp"
4
5 #include <iostream>
6
7 using namespace cv;
8 using namespace std;
9
10 static void help()
11 {
12     cout << "\nThis program demonstrates circle finding with the Hough transform.\n"
13             "Usage:\n"
14             "./houghcircles <image_name>, Default is pic1.png\n" << endl;
15 }
16
17 int main(int argc, char** argv)
18 {
19     const char* filename = argc >= 2 ? argv[1] : "board.jpg";
20
21     Mat img = imread(filename, 0);
22     if(img.empty())
23     {
24         help();
25         cout << "can not open " << filename << endl;
26         return -1;
27     }
28
29     Mat cimg;
30     medianBlur(img, img, 5);
31     cvtColor(img, cimg, COLOR_GRAY2BGR);
32
33     vector<Vec3f> circles;
34     HoughCircles(img, circles, HOUGH_GRADIENT, 1, 10,
35                  100, 30, 1, 30 // change the last two parameters
36                                 // (min_radius & max_radius) to detect larger circles
37                  );
38     for( size_t i = 0; i < circles.size(); i++ )
39     {
40         Vec3i c = circles[i];
41         circle( cimg, Point(c[0], c[1]), c[2], Scalar(0,0,255), 3, LINE_AA);
42         circle( cimg, Point(c[0], c[1]), 2, Scalar(0,255,0), 3, LINE_AA);
43     }
44
45     imshow("detected circles", cimg);
46     waitKey();
47
48     return 0;
49 }