[Image edge detection] ellipse edge detection based on matlab least squares method [including Matlab source code 079]

1. Introduction

Classical ellipse detection method
After years of research and development, the ellipse detection algorithm has basically formed a relatively complete system. They can be roughly divided into three categories: voting (clustering), optimization, and arc-based methods.
Voting (clustering) method
Because the ellipse has five parameters: the center position coordinate, the length of the long and short axis, and the tilt angle, the standard Hough transform has strong robustness, but it has high memory requirements and low computational efficiency, which is not realistic. The Hough transform algorithm is based on the Hough transform. After years of unremitting efforts by researchers from different countries, many improved algorithms have been derived, each with its own advantages and disadvantages. Compared with the standard Hough transform, the calculation speed of the random Hough transform algorithm is greatly improved, but the accuracy is low when detecting mutually occluded ellipses.

Random hough transform ellipse detection algorithm
Random ellipse detection uses a combination of least square method and Hough transform algorithm. The first step is preprocessing to obtain a more ideal edge map. The second step is to randomly select three points, take these three points as all points in the neighborhood of the same size with the center, and fit them into an ellipse by the least square method. As shown in Figure 2-3. The third step is to randomly select a fourth point from the edge points to determine whether this point is on the fitted circle. If so, it is considered that the ellipse is more likely to be a real ellipse, and then evidence is collected to verify the authenticity of the ellipse.
Insert picture description here
The specific process of the algorithm is as follows (starting from the second step):
1. Collect the points obtained by the edge detection into the set V, and set the initial value of the failure counter f to 0. Set 5 thresholds, which are the maximum number of failures that can be tolerated Tf, the required threshold Tem for the number of edge points in V during detection, the minimum distance Ta between three randomly selected points, and the fourth randomly selected fourth The maximum distance Td from the point to the boundary of the possible ellipse, and the ellipse defect ratio threshold Tr.
2. np represents the number of points remaining in the set V. When np is less than Tem or when the number of failures f is greater than Tf, the algorithm stops and the algorithm terminates; otherwise, four points are randomly selected from V and deleted from V.
3. If the distance between the three points used to solve the ellipse parameters is greater than Ta, fit the ellipse, calculate the distance from the fourth point to the boundary of the ellipse, if the distance is less than Td, go to step 4; if not Satisfy one of the two, return these four points to V, add one to the number of failures, and return to step 2.
4. Let E be the ellipse fitted in step 3, and initialize the number num of points that meet the threshold Td. Traverse the midpoint of V, calculate and judge whether the distance between them and the boundary of ellipse E is less than Td, if so, num=num+1, and remove the point from V until the traversal is completed.
5. If num>=Tr*K, where K is the circumference of the ellipse E, then skip to step 6; otherwise, the ellipse E is considered to be not a real ellipse, and num+4 deleted in steps 4 and 2 Each point returns to V and jumps to step 2.
6. Think that the ellipse E is a real ellipse, set f to 0, and jump to step 2.
The advantages and disadvantages of random hough transform are as follows:
First, because the algorithm is based on the least squares method, on the one hand, the detection result is often smaller than the real ellipse and is sensitive to noise, but on the other hand, the detection accuracy is high when the preprocessing effect is good. .
Second, because the algorithm is based on random sampling, on the one hand, there may be a situation where the selected points are relatively close, resulting in a larger deviation of the fitted ellipse, but on the other hand, the detection speed is increased because of the flexibility of random sampling.
Third, on the one hand, when the parameters are selected well, the detection is fast and accurate; on the other hand, because the algorithm is strictly controlled by the parameters Ta, Td, Tr and these parameters are not easy to obtain appropriate values, inappropriate parameters will appear Not only increases the amount of calculation, but also increases the chance of false detection.

Optimization method
The advantage of the optimization method is its accuracy. The disadvantage is that it can only process one graph at a time, that is, the image information must be classified and separated before. Andrew Fitzgibb et al. proposed a direct least squares ellipse fitting algorithm. This method can ensure that the fitted ellipse must be. However, this method is affected by isolated points and noise points. At present, most optimization algorithms are used in combination with other algorithms.

Arc-based method
The ellipse detection method based on boundary clustering combines the arc-based method and the least square method. The arc is extracted from the boundary map, filtered and clustered, and finally the ellipse is fitted by the least square method. This method can effectively deal with complex situations such as multiple ellipses, mutual ellipse occlusion, and partial defects of ellipses, which has attracted widespread attention.

Boundary clustering algorithm process
Boundary clustering algorithm is a bottom-up algorithm. The algorithm steps are mainly divided into three steps, which are preprocessing, boundary clustering and direct least squares fitting ellipse. The flow chart is as follows: The
Insert picture description here
Insert picture description here
Insert picture description here
fourth step is binarization. The effect of binarization almost entirely depends on the selection of the segmentation threshold. Therefore, the method of automatically finding the best segmentation threshold is very critical. One way to find a suitable segmentation threshold for image binarization is to divide the image into two parts: the background and the target according to the gray characteristics of the image. The segmentation with the largest inter-class variance between the background and the target means the smallest probability of misclassification. . The Graythresh function of MATLAB uses this method to obtain an adaptive threshold as the basis for binarization.
The fifth step, after binarization, because the non-correlated pixels near the elliptical arc will seriously affect the detection results, in order to greatly reduce the non-correlated pixels, this paper uses morphological corrosion operations to obtain the refined boundary.

Boundary pixel connection
Using Kovesi's boundary connection algorithm, the binary image is scanned from top to bottom and left to right according to the 8-neighbor connection criterion, and the boundary pixels are connected into directed boundary columns. Then, the threshold condition of the number of pixels in the boundary column is used to remove the set with the smaller number of pixels. Because if the number of pixels in the boundary column is less than the threshold number, it is likely to be noise or background and should be deleted. The specific steps are as follows:
1. Scan the binary image from top to bottom and from left to right according to the 8-neighbor connectivity criterion, and cluster the pixels in the image according to the connected regions.
2. Find all end points and bifurcation points in the boundary pixels of each connected domain (the bifurcation point is the intersection of three or more curves) and store them.
3. Using these end points and bifurcation points as end signs, divide the set of pixels in each connected domain into small sets that are disconnected when they encounter end points and connection points.
4. Delete the part of these sets whose number of pixels is less than a certain threshold.

Line segment extraction
Because it is difficult to obtain accurate tangents from image rasterization, and the tangents of arcs are needed in the subsequent process, line segment fitting is required, that is, multi-segment polylines are used to replace the original arcs. The specific steps are as follows:
1. Take the i-th directed boundary column connected by boundary pixels, and judge whether it exceeds the total number of boundary columns total, if not, proceed to step 2, if it is to terminate the algorithm.
2. Determine whether the boundary column has been processed, if it is otherwise, proceed to step 3; if yes, then i=i+1, repeat step 2.
3. Starting from the third point, calculate the first point to this point (Denoted as point j), and determine the distance between the first point and all points between this point and the line in turn. If all distances are less than a certain threshold, then j=j+1, re Proceed to step 3, otherwise the directed boundary column is disconnected from there, and only the first point and the jth point are kept in the front part, and the line formed by the previous j points is a straight line between the first point and the jth point Replace the line; the latter part is still recorded as the boundary column i.
4. Determine whether the number of pixels behind the directed boundary column broken in step 3 is less than a certain threshold, if it is, delete it, otherwise it will not be processed. Finally, skip to step 1.
After completing this process, the curve formed by the pixels of a connected domain becomes a polyline formed by some of the pixels. Although a part of the pixel information is lost after this process, the accuracy of obtaining the arc tangent is improved in a sense because there is no rasterization effect. And processing becomes simpler with less data. Then use the line segment threshold condition to remove the shorter line segment column. If the number of line segments is less than the threshold number, it is likely to be noise or background or too large error during fitting, so it must be deleted.

The rotation direction of the line segment column is unified.
This article unifies the rotation direction of all the line segment columns to the counterclockwise direction.
Assuming that the black dots in Figure 3-4 are points in the line segment column, the arrow represents the direction of the line segment column, P1 (x1, y1), P2 (x2, y2), P3 (x3, y3) are three consecutive lines in the line segment column. Pixels, pixels are introduced into the z coordinate, and let it be 0, then P1 (x1, y1, 0), P2 (x2, y2, 0), P3 (x3, y3, 0), space vector
Insert picture description here
Insert picture description here
arc cluster
circle Arc clustering is to cluster two or more elliptical arcs that belong to the same ellipse but are separated. Before clustering, we must first judge the completeness of the arc. Generally, the angle between the two vectors P2P1 and P2P3 formed by the end points P1, P3 and the midpoint P2 of the arc is used for judgment. The smaller the angle, the more complete the elliptical arc, and the larger the angle, the more serious the elliptical arc defect is generally considered. Set a threshold. When the included angle is less than the threshold, the arc is considered to be complete enough. Only the points on the arc can accurately fit the real ellipse, so the arc does not need to participate in the following Clustering process. If you want the threshold to be adaptive, you must determine the threshold before dividing the elliptical arc to be clustered and the arc that does not need to be clustered (more complete arc). Use the direct least squares method to fit the ellipse where the arc is. If it is rounder, in order to reduce the error, the threshold angle should be slightly larger, such as 90 degrees; if the ellipse where the arc is flat, the threshold angle should be made Slightly smaller, such as 60 degrees. Then, the arc judgment is performed according to the adaptive threshold. When it is greater than the threshold, it is considered that there are too few points on the arc, and it is not enough to fit an accurate ellipse. It is necessary to find the arc that belongs to the same ellipse as the arc and then use all of their points to fit an ellipse together. After this judgment process, the elliptical arc is divided into two groups. Arrange the elliptical arcs that need to participate in the clustering in descending order of the number of points. The following processes are performed in the order of priority to the arcs with the larger number.
For the ellipse arc to be clustered, the search area must be defined first. Since the ellipse is a closed figure, the entire ellipse can be determined to belong to the ellipse in the area enclosed by any part of the arc and the tangent line at the two ends of the arc. The other arcs of is determined to be in the area enclosed by the rays of the corresponding chord and tangents of the arc ends. This is our search area. The search area of ​​a1 in the figure is the area within the ray l1, l2, chord l3 and the edge of the image. Finding arcs in this area can narrow the search range and improve efficiency. To determine whether an arc is in the search area of ​​the elliptical arc to be clustered, we only need to take the first and end points j3 and j4 of this arc whether they are in the search area. The method is to find the intersection points of the two points parallel to the line and the tangent of the chord l3 of the elliptical arc to be clustered separately. If there are two intersection points, the intersection points are on the ray and the two endpoints are between the corresponding two intersection points. Then the arc is in the search area. In the figure, it is obvious that a2, a3, and a4 are in the search area of ​​a1 but a5 is not.
Insert picture description here
Insert picture description here
Insert picture description here
As shown in the figure (a) above, most of the points involved in the fitting fall on the fitted ellipse, so it is more likely to meet the constraint two condition; (b) most of the fitted points are separated from the fitted ellipse boundary For a certain distance, it is more likely that the constraint condition 2 will not be satisfied.
If you want these two thresholds to be adaptive, the method is to first fit the ellipse and determine the size of the ellipse. If the ellipse is small, the limit should be appropriately lowered, that is, increase the distance threshold and decrease the proportional threshold; if the ellipse is large, the limit should be appropriately increased, that is, decrease the distance threshold and increase the proportional threshold. In this algorithm, if the short axis of the ellipse is less than 50, the distance threshold is 0.05, and the ratio threshold is set to 0.7; otherwise, the former takes 0.03 and the latter takes 0.8.

Adding to
the arc after the arc clustering and more complete or more complete two elliptical arcs may belong to the same but they are only in the previous step is separated, and is not paired, then it is necessary to increase the matching process, the detection accuracy. The matching method is also the method of constraint two. Because this method is similar to the original algorithm's de-counterfeiting process, there is no need to de-counterfeit after this method.


Many of the previous steps of direct least squares ellipse fitting delete the set with fewer pixels, or replace many points in the boundary column with a few points, or delete the set with fewer points after segmentation. These operations are The ellipse fitting step actually basically removes all the background and noise, and even includes some useful information. So even the direct least squares method that is sensitive to noise and isolated points can be used to fit ellipses, and because this method is not sensitive to ellipse defects, it is very suitable.

Second, the source code

clc;
clear all;
im=imread('1.jpg');
im0=rgb2gray(im);%变彩色为黑白
im1=medfilt2(im0,[3 3]);%中值滤波函数
BW = edge(im1,'sobel'); %边缘检测,边缘为1,其余为0
[imx,imy]=size(BW);%获取矩阵行数和列数
L = bwlabel(BW,8);% Calculating connected components
mx=max(max(L))
% There will be mx connected components.Here U can give a value between 1 and mx for L or in a loop you can extract all connected components
% If you are using the attached car image, by giving 1,23 to L you can extract the number plate completely.
[r,c] = find(L==3); 
rc = [r c];
[sx sy]=size(rc);
n1=ones(imx,imy);%zeros
for i=1:sx
    x1=rc(i,1);
    y1=rc(i,2);
    n1(x1,y1)=0;
end % Storing the extracted image in an array
figure,imshow(n1);
title('边缘提取');
[S]=ellipsefit(c,r)
a=S.Phi;
jdg(S.Xc,S.Yc,S.A,S.B,a,n1)%plot ellipse
 
   

Three, running results

Insert picture description here

Four, remarks

Complete code or writing add QQ2449341593 past review
>>>>>>
[Matlab 024] [Image processing 1] Image compression of Matlab image processing tutorial series
[Matlab 025] [Image processing 2] Matlab image processing tutorial series Image segmentation (1)
[Matlab 026 issue] [Image processing 3] Image segmentation of Matlab image processing tutorial series (2)
[Matlab 029] [Image processing 4] Matlab fingerprint recognition
[Matlab 030] [Image processing 5] Bank Card number recognition matlab source code
[Matlab 074] [Image processing 6] [Image clustering] Based on FCM and improved FCM brain CT image clustering processing
[Matlab 075] [Image processing 7] [Image evaluation] Based on CCF algorithm Image quality evaluation
[Matlab 076] [Image processing 8] [Image enhancement] CLAHE algorithm based on local contrast enhancement-histogram enhancement
[Matlab 077] [Image processing 9] [Image fusion] Image fusion based on Frequency Partition
[ Matlab Issue 078] [Image Processing 10] [Image Evaluation] Image quality evaluation without reference based on svm

Guess you like

Origin blog.csdn.net/TIQCmatlab/article/details/112986616