2017-08-26 22:11:46 +08:00
|
|
|
/**
|
|
|
|
* @file houghcircles.cpp
|
|
|
|
* @brief This program demonstrates circle finding with the Hough transform
|
|
|
|
*/
|
|
|
|
#include "opencv2/imgcodecs.hpp"
|
|
|
|
#include "opencv2/highgui.hpp"
|
|
|
|
#include "opencv2/imgproc.hpp"
|
|
|
|
|
|
|
|
using namespace cv;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
//![load]
|
2019-07-28 17:09:17 +08:00
|
|
|
const char* filename = argc >=2 ? argv[1] : "smarties.png";
|
2017-08-26 22:11:46 +08:00
|
|
|
|
|
|
|
// Loads an image
|
2019-07-28 17:09:17 +08:00
|
|
|
Mat src = imread( samples::findFile( filename ), IMREAD_COLOR );
|
2017-08-26 22:11:46 +08:00
|
|
|
|
|
|
|
// Check if image is loaded fine
|
|
|
|
if(src.empty()){
|
|
|
|
printf(" Error opening image\n");
|
|
|
|
printf(" Program Arguments: [image_name -- default %s] \n", filename);
|
2019-07-28 17:09:17 +08:00
|
|
|
return EXIT_FAILURE;
|
2017-08-26 22:11:46 +08:00
|
|
|
}
|
|
|
|
//![load]
|
|
|
|
|
|
|
|
//![convert_to_gray]
|
|
|
|
Mat gray;
|
|
|
|
cvtColor(src, gray, COLOR_BGR2GRAY);
|
|
|
|
//![convert_to_gray]
|
|
|
|
|
|
|
|
//![reduce_noise]
|
|
|
|
medianBlur(gray, gray, 5);
|
|
|
|
//![reduce_noise]
|
|
|
|
|
|
|
|
//![houghcircles]
|
|
|
|
vector<Vec3f> circles;
|
|
|
|
HoughCircles(gray, circles, HOUGH_GRADIENT, 1,
|
|
|
|
gray.rows/16, // change this value to detect circles with different distances to each other
|
|
|
|
100, 30, 1, 30 // change the last two parameters
|
|
|
|
// (min_radius & max_radius) to detect larger circles
|
|
|
|
);
|
|
|
|
//![houghcircles]
|
|
|
|
|
|
|
|
//![draw]
|
|
|
|
for( size_t i = 0; i < circles.size(); i++ )
|
|
|
|
{
|
|
|
|
Vec3i c = circles[i];
|
|
|
|
Point center = Point(c[0], c[1]);
|
|
|
|
// circle center
|
|
|
|
circle( src, center, 1, Scalar(0,100,100), 3, LINE_AA);
|
|
|
|
// circle outline
|
|
|
|
int radius = c[2];
|
|
|
|
circle( src, center, radius, Scalar(255,0,255), 3, LINE_AA);
|
|
|
|
}
|
|
|
|
//![draw]
|
|
|
|
|
|
|
|
//![display]
|
|
|
|
imshow("detected circles", src);
|
|
|
|
waitKey();
|
|
|
|
//![display]
|
|
|
|
|
2019-07-28 17:09:17 +08:00
|
|
|
return EXIT_SUCCESS;
|
2017-08-26 22:11:46 +08:00
|
|
|
}
|