Circle Detection by using the Hough transform (Appendix: MATLAB program)

Source: Internet
Author: User


The basic principle of Hough transformation is to convert the line of the image space into the gathering point of the parameter space by the parity of the point and line, so as to detect whether the given image has a curve of a given nature. The equation of the circle is: (x-a) ^ 2 + (y-2) ^ 2 = R ^ 2, and the image space is mapped to the parameter space by means of Hough transformation. The MATLAB program in the appendix is common on the Internet, and some problems exist in actual operation. Here we make some modifications.
Principle:

 

Hof transform is one of the basic methods for recognizing geometric shapes from images in image processing. It is widely used and has many improved algorithms. The most basic Hof transformation is to detect straight lines (line segments) from black and white images ).

Let's take a look at this problem first: if a black and white image has been known to draw a straight line, the position of this line is required. We know that the linear equation can be represented by Y = K * x + B, where K and B are parameters, respectively slope and intercept. All linear parameters of a certain point (x0, y0) will satisfy the equation Y0 = kx0 + B. Point (x0, y0) determines a family of straight lines. Equation Y0 = kx0 + B is a straight line on the K -- B plane of the parameter (you can also be a straight line corresponding to Equation B =-x0 * k + y0 ). In this way, a foreground pixel on the x-y plane corresponds to a straight line on the parameter plane. Let's take an example to illustrate how to solve the previous problem. Set the straight line on the image to Y = x. Let's first take the above three points: A (), B (), C (22 ). We can find that the parameters of the straight line passing through the point must satisfy the equation B = 0, and the parameters of the straight line passing through the B point must satisfy the equation 1 = K + B, the parameters of the straight line passing through the C point must satisfy equation 2 = 2 K + B. These three equations correspond to three straight lines on the parameter plane, these three straight lines will be at one point (k = 1, B = 0 ). Similarly, other points (such as (3, 3) and (4, 4) on the X line in the original image) correspond to the straight lines on the parameter plane through the points (k = 1, B = 0 ). This nature provides us with a solution to the problem:

First, we initialize a buffer, corresponding to the parameter plane, and set all its data to 0.

For each foreground point in the image, find the straight line corresponding to the parameter plane, and Add 1 to the values of all vertices in the straight line.

Finally, find the maximum point position on the parameter plane, which is the parameter of the straight line on the original image. The above is the basic idea of HOF transformation. It is to map the points on the image plane to the line on the parameter plane, and finally solve the problem through statistical features. There are two straight lines on the dummy plane, so two peak points will be seen on the parameter plane, and so on.

In practical application, the linear equation in the form of Y = K * x + B cannot represent a straight line in the form of X = C (at this time, the slope of the straight line is infinite ). In practice, the parameter equation P = x * Cos (theta) + y * sin (theta) is used ). In this way, a point on the Image Plane Maps to a curve on the p-Theta plane. Others are the same.

Let's look at the following question: we need to detect the radius from a pair of images in a circular shape. This problem is more intuitive than the previous one. We can take the same parameter plane as the image plane, circle each foreground point on the image with a known radius on the parameter plane, and accumulate the results. Finally, find the peak point on the parameter plane, which corresponds to the center of the image. In this case, each point on the image plane corresponds to a circle on the parameter plane.

Let's change the problem above. If we don't know the radius value, we need to find the circle on the image. In this way, the expansion of the parameter plane is called a three-dimensional space. That is to say, the parameter space is changed to X -- y -- r three-dimensional, which corresponds to the center and radius of the circle. Each point on the image plane corresponds to a circle under each radius in the parameter space, which is actually a cone. Finally, find the peak point in the parameter space. However, this method obviously requires a large amount of memory, and the running speed is also a big problem.

Is there any better way? We previously assumed that the images were both black and white images (2-value images). In fact, these 2-value images are mostly colored or grayscale images extracted by edge. As we mentioned earlier, besides the location information and the Direction information, the edge of the image is also very important. According to the nature of the circle, the radius of the circle must be perpendicular to the straight line of the tangent of the circle, that is, the method line at any point on the circle. In this way, to solve the above problem, we still use a 2-dimensional parameter space. For each foreground point on the image, plus its direction information, we can determine a straight line, the center of the circle is on this line. In this way, the problem will be much simpler.

There are also many similar problems, such as detecting an elliptic, square, rectangle, and arc. Most of these methods are similar. The key is to be familiar with the mathematical properties of these geometric shapes. Hof transformation is widely used. For example, we want to do a check recognition task. Suppose there must be a square stamp in red on the check, we can quickly locate the seal through the HOF transform, and perform other processing with other means. Hof transform is easy to locate because it is not affected by image rotation. There are many ways to improve the HOF transformation. A more important concept is the generalized Hof transformation, which is applicable to all curves and is of great use. There are also many improved algorithms for the HOF transformation of straight lines. For example, we didn't consider whether the online points on the image were continuous in the previous method, these methods must be optimized based on different applications.

Implementation:

The method for detecting the tangent of a circle is mentioned above. This method is not discussed at the moment. Here we discuss the classical Hough algorithm. The following is an algorithm flow that uses polar coordinates to represent circles.

1. grayscale and binarization of images (Note: The quality of binarization has a great impact on the detection results. Common Sobel operators are used)

2. Detect the edge points in the image and save their coordinates. Set the variation range and step of angle Theta, and the transformation range and step of radius R.

3. Use the formula X = a + rcos (theta), Y = B + rsin (theta) to obtain the values of A and B. (Note: X and Y are the coordinates of an edge point in the actual image space, and A and B are the coordinates of the corresponding parameter space ), if the values of A and B are in a reasonable range, the positions are accumulated.

For example:

for i=1:ecount    for r=1:size_r        for k=1:size_angle            a = round(rows(i)-(r_min+(r-1)*step_r)*cos(k*step_angle));            b = round(cols(i)-(r_min+(r-1)*step_r)*sin(k*step_angle));            if(a>0&a<=m&b>0&b<=n)                hough_space(a,b,r) = hough_space(a,b,r)+1;            end        end    endend

4. Search for the maximum value, find the coordinates and radius of the center, and save the search.

Appendix: I modified the red part of the program. Modified and compiled.

 

Function [hough_space, cycle, para] = hough_circle (BW, step_r, step_angle, r_min, r_max, P); % [hough_space, hough_circle, para] = hough_circle (BW, step_r, step_angle, r_max, p) % ---------------------------- algorithm overview ----------------------------- % The algorithm uses a = x-R * Cos (angle), B = Y-R * sin (angle) map the edge vertex % in the circular image to the parameter space (a, B, R). Because it is a digital image and uses polar coordinates, angle and R both take a certain range and step size, in this way, the points in the original image % space can be mapped to the parameter space through a two-loop (angle loop and R loop), and then in the parameter space (that is, a % consisting of many small cubes) Find the center of the center, and then obtain the radius coordinate. % Radius % -------------------------------- input parameter --------------------------- % BW: binary image; % step_r: radius step % step_angle: angle step, Unit: % radian % r_min: Minimum radius % r_max: radius of the largest circle % P: Take the maximum value of P * hough_space as the threshold, and take the number between 0 and 1 as the threshold % ---------------------------- output parameter %hough_space: parameter space, H (A, B, r) indicates the number of points on the circle with the center (a, B) radius R % hough_circl: Binary Image, detected circle % para: center of the detected circle, radius % found % from Internet, modified by mhjerry, 2011-12-11 [M, N] = size (BW); size_r = round (r_max-r_min)/step_r) + 1; size_angle = round (2 * PI/step_angle); hough_space = zeros (m, n, size_r); [rows, cols] = find (BW ); ecount = size (rows); % Hough Transformation % map the image space (x, y) to the parameter space (a, B, R) % A = x-R * Cos (angle) % B = Y-R * sin (angle) for I = 1: ecount for r = 1: size_r for k = 1: size_angle A = round (rows (I)-(r_min + (r-1) * step_r) * Cos (K * step_angle); B = round (Cols (I) -(r_min + (r-1) * step_r) * sin (K * step_angle); If (a> 0 & A <= M & B> 0 & B <= N) hough_space (a, B, R) = hough_space (a, B, R) + 1; end end endend % searches for aggregation points that exceed the threshold value max_para = max (hough_space); Index = find (hough_space> = max_para * P ); length = size (INDEX); hough_circle = zeros (m, n); for I = 1: ecount for k = 1: length par3 = floor (index (k) /(m * n) + 1; par2 = floor (index (k)-(par3-1) * (M * n)/m) + 1; par1 = index (k)-(par3-1) * (M * n)-(par2-1) * m; If (rows (I)-par1) ^ 2 + (Cols (I)-par2) ^ 2 <(r_min + (par3-1) * step_r) ^ 2 + 5 &... (rows (I)-par1) ^ 2 + (Cols (I)-par2) ^ 2> (r_min + (par3-1) * step_r) ^ 2-5) hough_circle (rows (I), cols (I) = 1; end endend % print the result for k = 1: length par3 = floor (index (k) /(m * n) + 1; par2 = floor (index (k)-(par3-1) * (M * n)/m) + 1; par1 = index (k)-(par3-1) * (M * n)-(par2-1) * m; par3 = r_min + (par3-1) * step_r; fprintf (1, 'center % d radius % d \ n', par1, par2, par3); Para (:, K) = [par1, par2, par3] '; End

The Code has been uploaded to my resources. If you need to download the code, you can download it in my space.

Note that the selection of radius ranges directly affects the circle you want to detect. In addition, if the image is too large and the step size is too small, there may be insufficient memory.

Program:


Running result:

Source image:

After Edge Detection:

 

Detection Result: center 62 59 radius 52


 

 
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.