首页 > 代码库 > OpenCV Tutorials —— Hough Circle Transform

OpenCV Tutorials —— Hough Circle Transform

Hough 圆变换 和 Hough 直线变换原理相同,只是参数空间不同 :

In the line detection case, a line was defined by two parameters (r, \theta). In the circle case, we need three parameters to define a circle:

C : ( x_{center}, y_{center}, r )

where (x_{center}, y_{center}) define the center position (gree point) and r is the radius, which allows us to completely define a circle

 

Code

#include "stdafx.h"#include "opencv2/highgui/highgui.hpp"#include "opencv2/imgproc/imgproc.hpp"#include <iostream>#include <stdio.h>using namespace cv;/** @function main */int main(int argc, char** argv){	Mat src, src_gray;	/// Read the image	src = http://www.mamicode.com/imread("yuan.jpg", 1 );	if( !src.data )	{ return -1; }	/// Convert it to gray	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 );	}	/// Show your results	namedWindow( "Hough Circle Transform Demo", CV_WINDOW_AUTOSIZE );	imshow( "Hough Circle Transform Demo", src );	waitKey(0);	return 0;}

 

vector<Vec3f> circles;HoughCircles( src_gray, circles, CV_HOUGH_GRADIENT, 1, src_gray.rows/8, 200, 100, 0, 0 );

with the arguments:

  • src_gray: Input image (grayscale)
  • circles: A vector that stores sets of 3 values: x_{c}, y_{c}, r for each detected circle.
  • CV_HOUGH_GRADIENT: Define the detection method. Currently this is the only one available in OpenCV
  • dp = 1: The inverse ratio of resolution
  • min_dist = src_gray.rows/8: Minimum distance between detected centers
  • param_1 = 200: Upper threshold for the internal Canny edge detector
  • param_2 = 100*: Threshold for center detection.
  • min_radius = 0: Minimum radio to be detected. If unknown, put zero as default.
  • max_radius = 0: Maximum radius to be detected. If unknown, put zero as default

OpenCV Tutorials —— Hough Circle Transform