1 | #include <cv.h>
|
2 | #include <cv.hpp>
|
3 |
|
4 | #include <highgui.h>
|
5 | #include <highgui.hpp>
|
6 |
|
7 |
|
8 | int main()
|
9 | {
|
10 | using namespace cv;
|
11 |
|
12 | namedWindow("orig", CV_WINDOW_AUTOSIZE);
|
13 | namedWindow("canny", CV_WINDOW_AUTOSIZE);
|
14 | namedWindow("hough", CV_WINDOW_AUTOSIZE);
|
15 |
|
16 | Mat orig = cv::imread("1.jpg", 0);
|
17 | Mat hough;
|
18 | orig.copyTo(hough);
|
19 |
|
20 | Mat canny;
|
21 | Canny(orig, canny, 100, 50);
|
22 |
|
23 | int bestRad = 20;
|
24 | int minRad = bestRad / 1.3;
|
25 | int maxRad = bestRad * 1.3;
|
26 |
|
27 | vector<Vec3f> circles;
|
28 | HoughCircles(orig, circles, CV_HOUGH_GRADIENT,
|
29 | 1,
|
30 | 20,
|
31 | 100,
|
32 | 10,
|
33 | minRad,
|
34 | maxRad);
|
35 |
|
36 | for( size_t i = 0; i < circles.size(); i++ )
|
37 | {
|
38 | Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
|
39 | int radius = cvRound(circles[i][2]);
|
40 |
|
41 | circle( hough, center, 3, Scalar(0), -1, 8, 0 );
|
42 |
|
43 | circle( hough, center, radius, Scalar(200), 1, 8, 0 );
|
44 | }
|
45 |
|
46 |
|
47 | Point c(bestRad * 3, bestRad * 3);
|
48 | circle(hough, c, bestRad, 255);
|
49 | circle(hough, c, minRad, 255);
|
50 | circle(hough, c, maxRad, 255);
|
51 |
|
52 |
|
53 |
|
54 | cv::imshow("orig", orig);
|
55 | cv::imshow("canny", canny);
|
56 | cv::imshow("hough", hough);
|
57 |
|
58 | cv::waitKey();
|
59 | return 0;
|
60 | } |