opencv/samples/cpp/houghcircles.cpp

72 lines
1.7 KiB
C++
Raw Normal View History

2014-07-04 22:48:15 +08:00
#include "opencv2/imgcodecs.hpp"
2016-02-15 21:37:29 +08:00
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;
2012-06-08 01:21:29 +08:00
static void help()
{
cout << "\nThis program demonstrates circle finding with the Hough transform.\n"
"Usage:\n"
2014-09-13 22:28:41 +08:00
"./houghcircles <image_name>, Default is ../data/board.jpg\n" << endl;
}
int main(int argc, char** argv)
{
2015-08-01 23:24:23 +08:00
cv::CommandLineParser parser(argc, argv,
"{help h ||}{@image|../data/board.jpg|}"
);
if (parser.has("help"))
{
help();
return 0;
}
2016-07-18 21:32:05 +08:00
//![load]
2015-08-01 23:24:23 +08:00
string filename = parser.get<string>("@image");
2016-07-18 21:32:05 +08:00
Mat img = imread(filename, IMREAD_COLOR);
if(img.empty())
{
help();
cout << "can not open " << filename << endl;
return -1;
}
2016-07-18 21:32:05 +08:00
//![load]
//![convert_to_gray]
Mat gray;
cvtColor(img, gray, COLOR_BGR2GRAY);
//![convert_to_gray]
2016-07-18 21:32:05 +08:00
//![reduce_noise]
medianBlur(gray, gray, 5);
//![reduce_noise]
2012-06-08 01:21:29 +08:00
2016-07-18 21:32:05 +08:00
//![houghcircles]
vector<Vec3f> circles;
2016-07-18 21:32:05 +08:00
HoughCircles(gray, circles, HOUGH_GRADIENT, 1,
gray.rows/16, // change this value to detect circles with different distances to each other
2012-06-08 01:21:29 +08:00
100, 30, 1, 30 // change the last two parameters
// (min_radius & max_radius) to detect larger circles
);
2016-07-18 21:32:05 +08:00
//![houghcircles]
//![draw]
for( size_t i = 0; i < circles.size(); i++ )
{
Vec3i c = circles[i];
2016-07-18 21:32:05 +08:00
circle( img, Point(c[0], c[1]), c[2], Scalar(0,0,255), 3, LINE_AA);
circle( img, Point(c[0], c[1]), 2, Scalar(0,255,0), 3, LINE_AA);
}
2016-07-18 21:32:05 +08:00
//![draw]
2016-07-18 21:32:05 +08:00
//![display]
imshow("detected circles", img);
waitKey();
2016-07-18 21:32:05 +08:00
//![display]
return 0;
}