码迷,mamicode.com
首页 > 其他好文 > 详细

OpenCV Tutorials —— Hough Circle Transform

时间:2014-11-19 00:14:29      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:style   http   io   ar   color   os   sp   for   div   

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

In the line detection case, a line was defined by two parameters bubuko.com,布布扣. In the circle case, we need three parameters to define a circle:

bubuko.com,布布扣

where bubuko.com,布布扣 define the center position (gree point) and bubuko.com,布布扣 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 = 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: bubuko.com,布布扣 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

标签:style   http   io   ar   color   os   sp   for   div   

原文地址:http://www.cnblogs.com/sprint1989/p/4106823.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!