2014-07-04 22:48:15 +08:00
|
|
|
#include "opencv2/imgcodecs.hpp"
|
2012-03-27 20:00:22 +08:00
|
|
|
#include "opencv2/highgui/highgui.hpp"
|
|
|
|
#include "opencv2/imgproc/imgproc.hpp"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using namespace cv;
|
|
|
|
using namespace std;
|
|
|
|
|
2012-06-08 01:21:29 +08:00
|
|
|
static void help()
|
2012-03-27 20:00:22 +08:00
|
|
|
{
|
|
|
|
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;
|
2012-03-27 20:00:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
string filename = parser.get<string>("@image");
|
|
|
|
if (filename.empty())
|
|
|
|
{
|
|
|
|
help();
|
|
|
|
cout << "no image_name provided" << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
2012-03-27 20:00:22 +08:00
|
|
|
Mat img = imread(filename, 0);
|
|
|
|
if(img.empty())
|
|
|
|
{
|
|
|
|
help();
|
|
|
|
cout << "can not open " << filename << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Mat cimg;
|
|
|
|
medianBlur(img, img, 5);
|
2013-04-06 22:16:51 +08:00
|
|
|
cvtColor(img, cimg, COLOR_GRAY2BGR);
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2012-03-27 20:00:22 +08:00
|
|
|
vector<Vec3f> circles;
|
2013-04-06 22:16:51 +08:00
|
|
|
HoughCircles(img, circles, HOUGH_GRADIENT, 1, 10,
|
2012-06-08 01:21:29 +08:00
|
|
|
100, 30, 1, 30 // change the last two parameters
|
2012-03-27 20:00:22 +08:00
|
|
|
// (min_radius & max_radius) to detect larger circles
|
|
|
|
);
|
|
|
|
for( size_t i = 0; i < circles.size(); i++ )
|
|
|
|
{
|
|
|
|
Vec3i c = circles[i];
|
2013-04-08 02:45:38 +08:00
|
|
|
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);
|
2012-03-27 20:00:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
imshow("detected circles", cimg);
|
|
|
|
waitKey();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|