Keywords

1 Introduction

Product testing is an important link to ensure product quality. Traditional manual testing is easy to be influenced by subjective factors. Long-term testing can lead to fatigue of workers and lead to missed or wrong inspection, which leads to low precision of testing results. And in a large number of product testing, sampling inspection is usually adopted, which cannot guarantee the quality of all products. Varistors are widely used in industry. Because the shape of varistors is round, image recognition can be used to solve this problem. The common circular detection methods include standard Hough transform (SHT) [1] and Hough transform based on gradient transform [2]. But the method needs to introduce a three-dimensional voter, the calculation amount is large, and a large amount of occupied calculation memory is occupied, so the operation speed is relatively slow, and the timeliness of industrial detection is not met. In view of this defect of standard Hough transform, Xu et al. [3] proposed random Hough transform (RHT). By randomly sampling three non-collinear edge points, the position parameters of circle can be accumulated, but it is easy to detect repeated circle by this method. In order to solve this problem, an improved algorithm based on random Hough transform is proposed in this paper. The limit of center coordinate and radius is added. If the two center distance and radius distance are less than the threshold, then one of them is kicked out. Then, the accuracy of detection is greatly improved.

2 Improvement of a Canny Edge Algorithm

The conventional Canny edge detection is processed by using a Gaussian filter at the time of filtering, while the interference of the noise can be suppressed to a certain extent, but the useful edge cannot be extracted well for the image with the “Burrs” [4]. In this case, an improved Canny edge detection is proposed.

2.1 Mean Filter and Closed Operation

Mean filtering is also called linear smoothing filter. The principle of mean filtering is to calculate the average value of the neighborhood of any point, and the average value is obtained from the sum of the neighborhood divided by the area of the neighborhood [5]. The filtering effect of the mean filtering method is related to the template size used. Usually, the larger the radius of the template, the more blurred the filtered image is and the slower it runs. In this paper, the window selected by many experiments is order 5. Meanwhile, under the condition that the edge information is preserved as far as possible without changing the size of the image, the closed operation is introduced. Closed operation is the first expansion of the image and then corrosion [6].

$$I \cdot S = (I \oplus S) \otimes S$$
(9.1)

where I is the image and S is the structural element. Common structural elements are rectangular structure, elliptical structure and cross-shaped structure. The image to be detected in this paper is circular, and through practice, the structure is more similar to the cross-shaped structural elements. Figure 9.1 is a cross-shaped structural element. \(I \oplus S\) denotes the expansion operation on the image, taking the maximum value within the cross neighborhood of each position as the output grayscale value of that position. \(I \otimes S\) indicates that the image is eroded. That is, take the minimum value of the value in the cross neighborhood of each position as the output value of that position.

Fig. 9.1
figure 1

Cross-shaped structure

2.2 Edge Detection

The edge of the image refers to the location where the grayscale value changes dramatically. Sometimes the image undefined content can be understood only by considering the edge of the image [7]. In this paper, the third-order sobel operator is used to calculate the gradient amplitude and direction.

$${\text{sobel}}_{x} = \left[ {\begin{array}{*{20}c} 1 \\ 2 \\ 1 \\ \end{array} } \right] * \left[ {\begin{array}{*{20}c} 1 & 0 & { - 1} \\ \end{array} } \right] = \left[ {\begin{array}{*{20}c} 1 & 0 & { - 1} \\ 2 & 0 & { - 2} \\ 1 & 0 & { - 1} \\ \end{array} } \right]$$
(9.2)
$${\text{sobel}}_{y} = \left[ {\begin{array}{*{20}c} 1 & 2 & 1 \\ \end{array} } \right] * \left[ {\begin{array}{*{20}c} 1 \\ 0 \\ { - 1} \\ \end{array} } \right] = \left[ {\begin{array}{*{20}c} 1 & 2 & 1 \\ 0 & 0 & 0 \\ { - 1} & { - 2} & { - 1} \\ \end{array} } \right]$$
(9.3)

where sobelx denotes the convolutional kernel in the horizontal direction and is used to detect the vertical edge. sobely denotes the convolutional kernel in the vertical direction and is used to detect the horizontal edge. Image matrix I and sobelx, sobely convolution to get dx and dy. So you can get to the gradient G and direction θ of the pixel.

$$G = \sqrt {{\text{d}}x^{2} + {\text{d}}y^{2} }$$
(9.4)
$$\theta = \arctan 2({\text{d}}y,{\text{d}}x)$$
(9.5)

In order to improve the accuracy of edge location, the gradient assignment is refined by interpolation on the basis of Canny algorithm [8]. Interpolation pixels in the neighborhood. Decompose the perimeter of the pixel into 8 pixel neighborhoods, as shown in Fig. 9.2. A total of 9 pixels centered on A(i, j) are calculated and interpolation in the gradient direction. If the amplitude of the center point A(i, j) is less than the interpolation of its adjacent two gradient directions, then it should be a non-edge point and assign A(i, j) to 0; if A(i, j) is greater than or equal to two adjacent interpolation in the gradient direction, then A(i, j) is used as the primary edge point.

Fig. 9.2
figure 2

Location of pixel points

Taking A(i, j) as an example, the gradient angle at A(i, j) is first found, then the straight line in the gradient direction is drawn according to the gradient direction, and finally in the neighborhood centered on A(i, j), the neighborhood in the gradient direction is generally located. Next, A(i, j) is compared with the value in the gradient direction, if its value is larger than the neighborhood value in the gradient direction, it can be regarded as the maximum value, A(i, j) is selected as the edge point; if its value is not all larger than the value of the neighborhood in the gradient direction, it can be regarded as a non-maximum value, which needs to be suppressed so that A(i, j) = 0 [9].

The false edge can be effectively reduced by threshold of the processed image. It sets two threshold values and deals with them according to the following rules. The main results are as follows:

  1. 1.

    Those points whose edge strength is greater than the high threshold are used to determine the edge points.

  2. 2.

    Those points whose edge strength is smaller than the low threshold are immediately eliminated.

  3. 3.

    Those points whose edge strength is between the low threshold and the high threshold can only be accepted as the edge points when they can be connected to the determined edge points according to a certain path. The edge strength of all the points that make up this path is greater than the low threshold.

3 Improved Circle Detection Algorithm Based on Random Hough Transform

On the basis of Canny edge detection, the circular device is located. The traditional Hough transform (CHT) detection principle is as follows:

The circle with (a, b) as the center of the circle and r as the radius is transformed into the three-dimensional space with a, b, r as the parameter, and the following equation is obtained: \(\left( {x_{i} - a} \right)^{2} + \left( {x_{i} - b} \right)^{2} = r^{2}\). Each pixel on the edge of the same hole corresponds to a unique conical surface in the parameter space, and these cones intersect at one point. The point circle parameter can be obtained by cumulative voting, that is, the position parameter of the hole.

However, this method needs to establish a three-dimensional voting machine, the calculation is large, the operation speed is slow, and it is not suitable for industrial detection. Random Hough transform is the random selection of three non-collinear points in the image space to map to a point in the parameter space, which is a multi-to-one mapping; thus, avoiding the huge amount of computation of the traditional Hough transform from one to multiple mapping. In order to reduce the memory requirement, the dynamic linked list structure is adopted to accumulate the parameter allocation units obtained by more than one mapping. Compared with the traditional Hough transform, the memory requirement is reduced and the parameter space of RHT is infinite. The parameter precision is arbitrarily high and so on. However, when dealing with complex images, a large number of invalid units are still introduced into random sampling, resulting in a large number of invalid accumulations. In this paper, an improved RHT is proposed for circle detection.

Let D be the set of edge points, select four edge pixels randomly, and define a distance criterion to find the possible circle. When we find a possible circle, we use constraints to determine whether it is a true circle or not. The principles of the algorithm are as follows:

In a rectangular coordinate system, the general equation of a circle is expressed as follows:

$$\left( {x - a} \right)^{2} + (y - b)^{2} = r^{2}$$
(9.6)

It can also be expressed as:

$$2xa + 2yb + d = x^{2} + y^{2}$$
(9.7)

Among them: d = r2 − a2 − b2. Select three pixes vi(xi, yi), i = 1, 2, 3. If these three points are not collinear, they may be a circle with a center (a123, b123) and a radius of r123. Obtained by Eq. (9.5):

$$\left\{ {\begin{array}{*{20}c} {2x_{1} a_{123} + 2y_{1} b_{123} + d_{123} = x_{1}^{2} + y_{1}^{2} } \\ {2x_{2} a_{123} + 2y_{2} b_{123} + d_{123} = x_{2}^{2} + y_{2}^{2} } \\ {2x_{3} a_{123} + 2y_{3} b_{123} + d_{123} = x_{3}^{2} + y_{3}^{2} } \\ \end{array} } \right.$$
(9.8)

Among them, \(d_{123} = r_{123}^{2} - a_{123}^{2} - b_{123}^{2}\). The above three equations are expressed in matrix form:

$$\left( {\begin{array}{*{20}c} {2x_{1} } & {2y_{1} } & 1 \\ {2x_{2} } & {2y_{2} } & 1 \\ {2x_{3} } & {2y_{3} } & 1 \\ \end{array} } \right)\left( {\begin{array}{*{20}c} {a_{123} } \\ {b_{123} } \\ {d_{123} } \\ \end{array} } \right) = \left( {\begin{array}{*{20}c} {x_{1}^{2} + y_{1}^{2} } \\ {x_{2}^{2} + y_{2}^{2} } \\ {x_{3}^{2} + y_{3}^{2} } \\ \end{array} } \right)$$
(9.9)

When v1, v2, v3 not collinear, that is, (x2 − x1)(y3 − y1)−(x3 − x1)(y2 − y1\(\ne\) 0. Solve it:

$$a = \frac{{\left| {\begin{array}{*{20}c} {x_{2}^{2} + y_{2}^{2} - (x_{1}^{2} + y_{1}^{2} )} & {2(y_{2} - y_{1} )} \\ {x_{3}^{2} + y_{3}^{2} - (x_{1}^{2} + y_{1}^{2} )} & {2(y_{3} - y_{1} )} \\ \end{array} } \right|}}{{4\left( {(x_{2} - x_{1} )(y_{3} - y_{1} ) - (x_{3} - x_{1} )(y_{2} - y_{1} )} \right)}}$$
(9.10)
$$b = \frac{{\left| {\begin{array}{*{20}c} {2(x_{2} - x_{1} )} & {x_{2}^{2} + y_{2}^{2} - (x_{1}^{2} + y_{1}^{2} )} \\ {2(x_{3} - x_{1} )} & {x_{3}^{2} + y_{3}^{2} - (x_{1}^{2} + y_{1}^{2} )} \\ \end{array} } \right|}}{{4\left( {(x_{2} - x_{1} )(y_{3} - y_{1} ) - (x_{3} - x_{1} )(y_{2} - y_{1} )} \right)}}$$
(9.11)
$$r_{123} = \sqrt {(x_{i} - a_{123} )^{2} + (y_{i} - b_{123} )^{2} }$$
(9.12)

Is taken v4(x4, y4) as the fourth pixel point, and the distance from the pixel v4 to the circle c123 is d4−123. That is:

$$d_{4 \to 123} = \left| {\sqrt {(x_{4} - a_{123} )^{2} + (y_{4} - b_{123} )^{2} } - r_{123} } \right|$$
(9.13)

We set a threshold t (t = 1), when d4−123 \(\le\) t, the point is considered to be on the circle. Random Hough transform sometimes detects multiple adjacent circles near the detected target, as shown in Fig. 9.1. In order to solve this problem, we introduce the limit of center coordinate distance and radius coordinate distance. The distance between the center of the circle, the longitudinal coordinates, and the radius of the detected circle is calculated in pairs, that is:

$$m = \left| {a_{123} - a_{{1^{'} 2^{'} 3^{'} }} } \right|$$
(9.14)
$$n = \left| {b_{123} - b_{{1^{'} 2^{'} 3^{'} }} } \right|$$
(9.15)
$$l = \left| {r_{123} - r_{{1^{'} 2^{'} 3^{'} }} } \right|$$
(9.16)

If the values of the above three are all less than the set threshold z (z = 15), then delete one of the circles, leaving a better detection effect of the circle (Fig. 9.3).

Fig. 9.3
figure 3

Two adjacent circles

The algorithm steps are as follows:

  1. 1.

    Constructing edge pixes l set V, Vi = (xi, yi). Initialize the failure counter f to be 0. Set the six thresholds of Tf, Tmin, Ta, Tr, Tb, Tc. If is the maximum number of failures allowed, Tmin represents the minimum number of remaining pixels in V; Ta represents the minimum distance between any two selected pixels; Td represents the distance from the fourth pixel point to the center of the circle being detected; Tr represents the ratio of the detected edge point set to the theoretical \(2\pi r\) point set; Tb represents the distance threshold of the detected close circular horizontal and vertical coordinates; Tc represents the radius distance threshold. |V| represents the number of pixels remaining in V.

  2. 2.

    If f = Tf or |V| < Tmin, then stop detecting. Otherwise, 4 pixels (vi, i = 1, 2, 3, 4) are selected randomly. At the same time, v = v  {vi}.

  3. 3.

    Select three edge pixels, solve the parameters of the circle and satisfy the constraints. If fit, turn Eq. (9.4). Otherwise, it is put back into collection V, and f = f+1. Then, turn Eq. (9.2).

  4. 4.

    Initialize the counter C = 0, and for each vi in V, check whether di−ijk is less than the given distance threshold Td. If satisfied, the value of the counter is added by 1, and the vi is taken out of V. Traverse all the pixels in V, let c = d, the number of pixels satisfying the condition is np.

  5. 5.

    If np \(\ge 2\pi {\text{r}}T_{r}\) and m, n \(\le\) Tb, l \(\le\) Tc, turn Eq. (9.6). Otherwise, the detected circle is considered to be a false circle. Put np pixels back into V and turn Eq. (9.2).

  6. 6.

    Think that the possible circle detected is true circle, set f = 0, turn Eq. (9.2).

4 Dimensional Inspection of Varistor

In order to verify the effectiveness of the algorithm, this paper uses OpenCV3.4 and Python3.5 language to simulate. In the experiment, a 2 million-pixel industrial camera was used to capture a varistor image. Figure 9.4 is the traditional Canny algorithm detection, Fig. 9.5 is improved Canny algorithm detection, we can clearly see that the improved algorithm not only smoothed the contour of varistor image, but also eliminated the noise well, and made the edge clearer. And retained the real edge, more in line with the industrial testing requirements of varistors.

Fig. 9.4
figure 4

Traditional Canny edge detection

Fig. 9.5
figure 5

Improved Canny edge detection

Traditional RHT detection circles are prone to false circles or omissions, that is, multiple circles are detected in the same circle or some circles are not detected, as shown in Fig. 9.6. In view of this situation, an improved RHT is proposed to increase the limit of center distance and radius distance, which effectively solves the problem. The average detection time is kept at 0.9 s, and the time standard of industrial detection is met at the same time. The test result is shown in Fig. 9.7.

Fig. 9.6
figure 6

Traditional RHT detection images

Fig. 9.7
figure 7

RHT detection image after improvement

5 Conclusion

Varistor is a common electronic component, for its automatic detection, and is a certain market demand. The time and space complexity of the standard Hough transform is relatively high, so the detection time is relatively long that it is not suitable for industrial detection. Before the detection, the mean filter is used to de-noising the image, and the expansion and corrosion of the image edge are smoothed to make it more consistent with the circular features. As can be seen from Fig. 9.6, the traditional Hough transform detects a plurality of duplicate circles in the first row and omits the first varistor in the second line. Obviously, this effect does not meet the test requirements. The research in this paper shows that the improved Hough transform can avoid the occurrence of this situation, and the new detection method satisfies the detection time and the detection accuracy.