Skip to content

Instantly share code, notes, and snippets.

@foundry
Last active August 29, 2015 14:26
Show Gist options
  • Save foundry/a7b0e95c52f6ace97315 to your computer and use it in GitHub Desktop.
Save foundry/a7b0e95c52f6ace97315 to your computer and use it in GitHub Desktop.
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "openCVUtils.h"
#include <iostream>
using namespace cv;
using namespace std;
static Mat _inImage;
static Mat _outImage;
static void help()
{
cout << "\nThis program demonstrates circle finding with the Hough transform.\n"
"Usage:\n"
"./houghcircles <image_name>, Default is pic1.png\n" << endl;
}
static const char* keys =
{
"{1| |circle.800.jpg|input image file}"
};
int houghcircles(int argc, char** argv)
{
CommandLineParser parser(argc, argv, keys);
string filename = parser.get<string>("1");
filename = assetPath(filename.c_str());
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);
vector<Vec3f> circles;
HoughCircles(img
, circles
, CV_HOUGH_GRADIENT //method – Detection method to use. Currently, the only implemented method is CV_HOUGH_GRADIENT , which is basically 21HT , described in [Yuen90].
, 1 //p – Inverse ratio of the accumulator resolution to the image resolution. For example, if dp=1 , the accumulator has the same resolution as the input image. If dp=2 , the accumulator has half as big width and height.
, 60 //minDist – Minimum distance between the centers of the detected circles. If the parameter is too small, multiple neighbor circles may be falsely detected in addition to a true one. If it is too large, some circles may be missed.
,
100 //cannyThreshold – The higher threshold of the two passed to the gpu::Canny() edge detector (the lower one is twice smaller).
, 30 //votesThreshold – The accumulator threshold for the circle centers at the detection stage. The smaller it is, the more false circles may be detected.
, 250 //minRadius – Minimum circle radius.
, 300 // change the last two parameters
//maxRadius – Maximum circle radius.
//maxCircles – Maximum number of output circles.
// buf – Optional buffer to avoid extra memory allocations (for many calls with the same sizes).
// (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, CV_AA);
circle( cimg, Point(c[0], c[1]), 2, Scalar(0,255,0), 3, CV_AA);
}
imshow("detected circles", cimg);
waitKey();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment