from 0 to combat opencv N (9) - Hough transform, a straight line and circle detection
Hough line transform, transform round
1, line Hough transform is a method of detecting a straight line, is determined primarily by a plurality of points with a straight line, the first straight line using a polar coordinate representation of all possible points through,
The polar equation is a sine curve, whether the intersection at the same point, they can determine whether a straight line through the same by comparing a plurality of sinusoids of known points.
2, a circular Hough transform Hough transform with similar lines, using center coordinates and polar coordinates to express a circle, all to give a little circle described curve, if the same plurality of intersection points,
The instructions on these points in the same round.
3, opencv function:
(1) Standard Hough Transform line
HoughLines function is achieved by the OpenCV give some of the polar coordinate parameters
(2) the statistical probability Hough transform line
It is achieved by the function in the OpenCV HoughLinesP, to give straight line endpoints
(3) circular Hough transform HoughCircles (src_gray, circles, CV_HOUGH_GRADIENT, 1, src_gray.rows / 8, 200, 100, 0, 0);
Function with the following arguments:
src_gray: input image (grayscale)
circles Social: storing the following three parameters: a container set to represent each circle detected.
CV_HOUGH_GRADIENT:. Specify a detection method is now only OpenCV Hough gradient
dp = 1: inverse image resolution of the accumulator
min_dist = src_gray.rows / 8: detecting the minimum distance between the center
param_1 = 200: Canny edge high threshold function
param_2 = 100: center detection threshold.
min_radius = 0: can detect the smallest radius, the default is 0.
max_radius = 0: can detect the maximum radius, the default is 0
4, program test:
void main()
{
Mat src, dst,gray;
/// 装载图像
src = imread("5.jpg");
cvtColor(src, gray,CV_BGR2GRAY);
imshow("src", gray);
GaussianBlur(gray, gray, Size(3, 3), 1, 2);
vector<Vec4i> lines;
vector<Vec3f> circles;
//HoughLinesP(gray, lines, 1, CV_PI / 20, 200, 20, 10);
HoughCircles(gray, circles, CV_HOUGH_GRADIENT, 1, 1, 200, 60, 0, 2000);
//for (size_t i = 0; i < lines.size(); i++)
//{
// Vec4i l = lines[i];
// line(src, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0, 0, 255), 3, CV_AA);
//}
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);
}
imshow("dst", src);
waitKey(0);
}
More attention to micro-channel public number: ML_Study