opencv/samples/cpp/houghcircles.cpp

50 lines
1.3 KiB
C++
Raw Normal View History

2014-07-04 16:48:15 +02:00
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;
2012-06-07 19:21:29 +02:00
static void help()
{
cout << "\nThis program demonstrates circle finding with the Hough transform.\n"
"Usage:\n"
2014-09-13 16:28:41 +02:00
"./houghcircles <image_name>, Default is ../data/board.jpg\n" << endl;
}
int main(int argc, char** argv)
{
2014-09-13 16:28:41 +02:00
const char* filename = argc >= 2 ? argv[1] : "../data/board.jpg";
Mat img = imread(filename, 0);
if(img.empty())
{
help();
cout << "can not open " << filename << endl;
return -1;
}
Mat cimg;
medianBlur(img, img, 5);
cvtColor(img, cimg, COLOR_GRAY2BGR);
2012-06-07 19:21:29 +02:00
vector<Vec3f> circles;
HoughCircles(img, circles, HOUGH_GRADIENT, 1, 10,
2012-06-07 19:21:29 +02:00
100, 30, 1, 30 // change the last two parameters
// (min_radius & max_radius) to detect larger circles
);
for( size_t i = 0; i < circles.size(); i++ )
{
Vec3i c = circles[i];
circle( cimg, Point(c[0], c[1]), c[2], Scalar(0,0,255), 3, LINE_AA);
circle( cimg, Point(c[0], c[1]), 2, Scalar(0,255,0), 3, LINE_AA);
}
imshow("detected circles", cimg);
waitKey();
return 0;
}