forked from oreillymedia/Learning-OpenCV-3_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_12-02.cpp
44 lines (33 loc) · 1012 Bytes
/
example_12-02.cpp
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
// Example 12-2. Using cv::HoughCircles() to return a sequence of circles found in a
// grayscale image
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
if(argc != 2) {
cout << "Hough Circle detect\nUsage: " <<argv[0] <<" <imagename>\n" << endl;
return -1;
}
cv::Mat src, image;
src = cv::imread( argv[1], 1 );
if( src.empty() ) { cout << "Cannot load " << argv[1] << endl; return -1; }
cv::cvtColor(src, image, cv::COLOR_BGR2GRAY);
cv::GaussianBlur(image, image, Size(5,5), 0, 0);
vector<cv::Vec3f> circles;
cv::HoughCircles(image, circles, cv::HOUGH_GRADIENT, 2, image.cols/4);
for( size_t i = 0; i < circles.size(); ++i ) {
cv::circle(
src,
cv::Point(cvRound(circles[i][0]), cvRound(circles[i][1])),
cvRound(circles[i][2]),
cv::Scalar(0,0,255),
2,
cv::LINE_AA
);
}
cv::imshow( "Hough Circles", src);
cv::waitKey(0);
return 0;
}