2011-06-29 03:32:48 +08:00
|
|
|
/**
|
|
|
|
* @file HoughCircle_Demo.cpp
|
|
|
|
* @brief Demo code for Hough Transform
|
|
|
|
* @author OpenCV team
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "opencv2/highgui/highgui.hpp"
|
|
|
|
#include "opencv2/imgproc/imgproc.hpp"
|
|
|
|
#include <iostream>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
using namespace std;
|
2011-06-29 03:32:48 +08:00
|
|
|
using namespace cv;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function main
|
|
|
|
*/
|
2012-11-07 22:21:20 +08:00
|
|
|
int main(int, char** argv)
|
2011-06-29 03:32:48 +08:00
|
|
|
{
|
|
|
|
Mat src, src_gray;
|
|
|
|
|
|
|
|
/// Read the image
|
|
|
|
src = imread( argv[1], 1 );
|
|
|
|
|
|
|
|
if( !src.data )
|
|
|
|
{ return -1; }
|
|
|
|
|
2012-10-17 07:18:30 +08:00
|
|
|
/// Convert it to gray
|
2011-06-29 03:32:48 +08:00
|
|
|
cvtColor( src, src_gray, CV_BGR2GRAY );
|
|
|
|
|
|
|
|
/// Reduce the noise so we avoid false circle detection
|
|
|
|
GaussianBlur( src_gray, src_gray, Size(9, 9), 2, 2 );
|
|
|
|
|
|
|
|
vector<Vec3f> circles;
|
|
|
|
|
|
|
|
/// Apply the Hough Transform to find the circles
|
|
|
|
HoughCircles( src_gray, circles, CV_HOUGH_GRADIENT, 1, src_gray.rows/8, 200, 100, 0, 0 );
|
|
|
|
|
|
|
|
/// Draw the circles detected
|
|
|
|
for( size_t i = 0; i < circles.size(); i++ )
|
|
|
|
{
|
|
|
|
Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
|
|
|
|
int radius = cvRound(circles[i][2]);
|
|
|
|
// circle center
|
|
|
|
circle( src, center, 3, Scalar(0,255,0), -1, 8, 0 );
|
|
|
|
// circle outline
|
|
|
|
circle( src, center, radius, Scalar(0,0,255), 3, 8, 0 );
|
|
|
|
}
|
|
|
|
|
2012-10-17 07:18:30 +08:00
|
|
|
/// Show your results
|
2011-06-29 03:32:48 +08:00
|
|
|
namedWindow( "Hough Circle Transform Demo", CV_WINDOW_AUTOSIZE );
|
|
|
|
imshow( "Hough Circle Transform Demo", src );
|
|
|
|
|
|
|
|
waitKey(0);
|
|
|
|
return 0;
|
|
|
|
}
|