1 Introduction

With the development of science and technology, the traditional manual testing technology has been unable to meet the needs of the modern, machine vision inspection technology in the field of industrial promotion and application of high-precision, low-cost advantages. The basic principle of machine vision [1] is to analyze the measured target image obtained by the computer vision system so as to obtain the required measurement information, and determine whether the measured target complies with the specification based on prior knowledge.Machine vision includes capturing digital video,using algorithms which can detect and analyze specific objects and interested patterns [2], including the design and selection of hardware, image splicing and preprocessing algorithms, image edge detection algorithm research, data processing methods, and so on.

With the development of science and technology, the traditional manual testing technology have been unable to meet this need, machine vision inspection technology for its high precision, low-cost advantage in the field of industrial promotion and application, as the machine vision inspection technology are briefly introduced, and then to a non-woven masks of factory as an example, Chen [3] expounds the application of machine vision inspection technology. Carlos Calderon-Cordova proposed an architecture and program of board game design based on computer vision [4]. Chen et al. proposed the current application of machine vision technology in agriculture [5]. Morison et al. described the development process of a senior undergraduate course that introduced machine vision and image processing algorithms and implementation topics [6]. Choi and Kim proposed the design of structural safety inspection system based on computer vision [7].

Inspection of the geometrical dimensions of small parts includes the geometric dimension, surface finish, surface texture, and surface defects. At present, for the detection of small parts is mainly machine vision detection, machine vision detection technology has been widely used in these aspects.

Pavim established a flexible detection system for small batch production by combining sensors and data [8]. Jin et al. proposed a method of using computer vision technology to detect small ceramic tube defects [9], using computer and computer vision, electromechanical and other technologies to develop an automatic detection system, and to achieve a small ceramic tube for automatic detection and classification. Böhm and Jech [10] scanned the surface with a small scattered light sensor using a red LED as a light source. The intensity distribution of scattered light detected depends on the topography of the reflective surface. The system can detect scratches on the high gloss metal sheet with a typical width of 1 μm and a depth of 40 nm. Kawasue and Komatsu [11] proposed a mobile robot equipped with two lasers and a CCD camera for pipeline inspection. The mobile robot can reconstruct the 3D shape of the sewage pipeline. Since the tilt of the mobile robot with respect to the axis of the pipe is expressed as a deformation between the two circular stripes, the shape of the sewage pipe can be accurately measured regardless of the inclination of the robot. Li et al. [12] applied machine vision technology to automate the inspection and condition monitoring of railway tracks. An imaging setup framework for capturing multiple video streams was proposed for the detection of important rail assemblies such as backing plates, nails, anchor rods, and connecting rod bolts, and for defect identification such as raised spikes, defect severity analysis and time condition analysis, and long-term forecast evaluation. The average detection rate of component detection has been initially achieved 98.2%, the false-positive rate was 1.57%, and the false-negative rate was 1.78%. He and Wang [13] developed an improved small defect detection algorithm for real-time leather surface detection using an enhancement method based on discrete cosine transform (DCT). The experimental results show that the proposed small defect detection method can achieve 94% detection rate of small defects.

With the continuous improvement of precision requirements for industrial inspection, the basic pixel-level accuracy in machine vision recognition has not been able to meet the requirements of actual measurement, so it needs a more accurate edge extraction algorithm [14, 15], that is, sub-pixel algorithm[16]. The sub-pixel algorithm [17] generally needs to find the position of the edge pixel [18] using the classical algorithm first and then use the gray value of the surrounding pixel [18] as the supplementary information for judgment, using interpolation, fitting, and so on [19,20,21] to position the edge in a more precise position.

On the basis of the discussion of the accuracy, the anti-noise ability, and the computation of the common sub-pixel location algorithm [22,23,24], this paper proposes an improved least squares subdivision method [25,26,27] to sub-pixel fitting for straight lines [28,29,30]. In this paper, three standard nuts are used as the measurement data, mainly using the interpolation method before binomial modified and after binomial modified based on Canny operator to sub-pixel locate the inner circle of the test sample, extract the sub-pixel coordinates, and use the least square method to fit the circle diameter. The error of sub-pixel value is 0.0314, and the error of the improved sub-pixel algorithm is 0.0108. The three measured value is 3.392, and the average value of machine vision recognition technology is 3.403. By contrast, it can be found that the improved sub-pixel value improves the positioning accuracy and operation speed.

2 Proposed method

The key to the detection of small part dimension is vision technology, including image acquisition, image edge detection and sub-pixel positioning, image stitching, and dimension estimation.

2.1 Image acquisition

The visual image that can be seen by the eye is continuous, and the computer can only process discrete data, so the visual inspection system must convert the continuous image function into a discrete data set. This process is called digitization of the image. The process of digitizing an image is the process of generating a one-dimensional matrix in a computer. The digitization process consists of one step: scanning, sampling, and quantification.

The image acquisition is actually to convert the visual image and intrinsic features of the measured object into a series of discrete data that can be processed by the computer. The result of scanning an image according to a rectangular scanning grid is to generate a one-dimensional integer matrix corresponding to the image. The position of each element (pixel) in the matrix is determined by the scanning order, and the gray value of each pixel is determined by the samples that are generated and quantized to give an integer representation of each pixel’s gray value. Therefore, the result of image acquisition is to digitize a natural continuous image and finally obtain a digital image. It generally has two common representations.

  1. (1)

    Array representation of grayscale images

Suppose continuous images f(x, y) are sampled at equal intervals and arranged in an M × N array (generally squared N × N), each element in the image array is a discrete value called a pixel (pix-el). In digital image processing, it is generally assumed that the array N and the gray level are both integer powers of two, as shown in Eq. 1.

$$ \mathrm{f}\left(\mathrm{x},\mathrm{y}\right)=\left[\begin{array}{ccc}f\left(0,0\right)& f\left(0,1\right)\dots & f\left(0,N-1\right)\\ {}f\left(1,0\right)& f\left(1,1\right)\dots & f\left(1,N-1\right)\\ {}f\left(N-1,0\right)& f\left(N-1,1\right)& f\left(N-1,N-1\right)\end{array}\right] $$
(1)
  1. (2)

    Binary image representation

The binarization of the image is that the entire image presents a clear black and white effect. The 256 brightness level grayscale image is selected by an appropriate threshold to obtain a binarized image that can still reflect the overall image and local characteristics of the image. If a particular object has a uniform gray value inside, and it is in a uniform background with other levels of gray values, we can get the comparison segmentation effect by using the threshold method. When the difference between the object and the background is not reflected in the grayscale value, this difference feature can be converted to a grayscale difference, and then the threshold selection technique is used to segment the image. The image can observe the specific results of the segmented image dynamically by adjusting the threshold dynamically to achieve the binary.

In digital image processing, in order to reduce the amount of computation, grayscale images are often converted to binary image processing. The so-called binary image is that the image has two gray levels (only black and white), that is the pixel gray level is 1 or 0.

2.2 Image edge detection

The visual measurement system of mechanical parts is generally carried out under a carefully designed and controllable environment. The acquired image of the measured part has a large contrast between the target and the background, and the signal-to-noise ratio is relatively high. A high signal-to-noise ratio image is edge-detected by the edge detection operator to obtain a single-pixel edge point. The edges of most mechanical parts are composed of straight lines and arcs. The linear parameters are usually determined by the least square linear regression method, which is the most commonly used sub-pixel detection method for line edges. The least squares linear regression method in the fitting method can calculate the boundary of the straight line portion of the edge image at one time, and the positioning is fast.

The edge points of a straight line can be regarded as a set of vectors, and the edge vectors can be accurately located by least square straight line fitting.

Let the linear regression equation be:

$$ \widehat{y}={a}_0+{a}_1x $$
(2)

In Eq. 2, a0 and a1 are the constants and coefficients of the regression equation.

The corresponding regression values are:

$$ \left\{\begin{array}{c}{\widehat{y}}_1={a}_0+{a}_1{x}_1\\ {}{\widehat{y}}_2={a}_0+{a}_2{x}_2\\ {}\dots \\ {}{\widehat{y}}_n={a}_0+{a}_n{x}_n\end{array}\right. $$
(3)

when the value of x is x1,x2,…xn.

According to extreme conditions, there are:

$$ \left.\begin{array}{c}\frac{\partial S}{\partial {a}_0}=2\sum \limits_{i=1}^n\left({a}_0+{a}_1{x}_i-{y}_i\right){x}_i=0\\ {}\frac{\partial S}{\partial {a}_1}=2\sum \limits_{i=1}^n\left({a}_0+{a}_1{x}_i-{y}_i\right){x}_i=0\end{array}\right\} $$
(4)

Solve the above equations and get the result:

$$ \left.\begin{array}{c}{a}_0=\frac{\sum \limits_{i=1}^n{x}_i\sum \limits_{i=1}^n{x}_i{y}_i-\sum \limits_{i=1}^n{y}_{i\sum \limits_{i=1}^n{x_i}^2}}{\left(2n+1\right)\sum \limits_{i=1}^n{x_i}^2-{\left(\sum \limits_{i=1}^n{x}_i\right)}^2}\\ {}{a}_1=\frac{\sum \limits_{i=1}^n{x}_i\sum \limits_{i=1}^n{y}_i-\left(2n+1\right)\sum \limits_{i=1}^n{y}_i{x}_i}{\sum \limits_{i=1}^n{x_i}^2-\left(2n+1\right)\sum \limits_{i=1}^n{x_i}^2}\end{array}\right\} $$
(5)

From Eq. 5, the constants and coefficients of the regression equation are obtained, and the fitted line is obtained as Eq. 2, which is the sub-pixel position passing through the edges of the straight line pixels. The positioning accuracy of sub-pixels is related to the number of points and noise involved in fitting. The more points participating in fitting, the more accurate the sub-pixel positioning of the straight line, but the more time it takes for the calculation. The smaller the noise, the higher the positioning accuracy.

2.3 Image stitching

Image matching is a key step before stitching. The purpose is to find the overlapping part of adjacent images, that is to achieve image alignment. The essence is to find the internal relationship between the two images and find the relationship between the two sets of corresponding data. The matching based on the similarity between the images can be divided into a feature-based method and a non-feature method from the matching method. The feature-based method requires selecting a series of features from two images and then performing feature matching between images according to the principle of similarity. Finally, the optimization process is used to calculate the transformation from one image to another. The selection of features can be feature points or feature flags. The feature point can be the corner point of the image, the center of gravity of the closed contour line, the texture feature point, or other prominent points. The key issue is to find the feature that fits the target. Non-feature matching is the alignment based on the correlation principle. The least squares difference function of some parameters is used to find the registration region of the image. For example, the region with the smallest gray-scale difference is directly searched from the overlapping region, although it does not rely on the external target feature, but it is required that the changes in grayscale, rotation, and deformation between images are small.

Szeliski proposed an image smoothing algorithm: the gray value of the pixel in the overlapping region of the image is obtained by weighted averaging of the gray value of the corresponding pixel in the two images. The specific method is as follows:

  1. (1)

    Project image B onto the coordinate system of image A, as shown in Fig. 1. Areas A1 and B2 do not belong to the overlapping area. The values of the merged image pixels take the values of A1 and B2.

  2. (2)

    The overlapping areas A2 and B1 represent the same physical scene and have the same geometric properties after registration. In the overlap region, take the gray weighted mean: G = d1 × A2 + d2 × Bd1 ∈ (0, 1), and d1 + d2 = 1; d1 is related to the abscissa of the pixel point, in the direction from A to B, gradient from 0 to 1.

  3. (3)

    Divide A2 and B1 into two L1, L2, and L3 columns, taking (L1 = L3) > L2.

  4. (4)

    Let the resulting image after fusion be R and set the threshold Th. In L1, if |A2 − G| < Th, R = G, otherwise R = A2. In L2, if |max(A2, B1) − G| < Th, R = G, otherwise R = max(A2, B1). In L3, if |B1 − G| < Th, R = G, otherwise R = B1.

Fig. 1
figure 1

Image overlapping area

2.4 Defect feature extraction

After obtaining the stitched image of the surface of the part, extract the global image of the surface with surface defects for edge extraction of defects. Once the surface defects of the test sample are detected, the defects are classified. Firstly, feature extraction is performed on the defects. Feature extraction is an important part of image pattern recognition. Its purpose is to extract a set of features that are as concise as possible and most effective in order to improve the recognition effect and efficiency of the classifier. In this paper, sub-pixel positioning is applied to the defect boundary in the global image of the surface of the part. And the fuzzy clustering algorithm is used to identify and divide the defects. Fuzzy clustering algorithm is a clustering algorithm based on partition. The idea is to make the similarity among the objects that are divided into the same cluster be the largest, and the similarity between different clusters is the smallest. The FCM(Fuzzy C-means) clustering uses fuzzy partitioning to make each given data point determine the degree to which it belongs to each group with a membership degree between [0,1]. The general form of the value function of FCM is as Eq. 6:

$$ J\left(U,{c}_1,\dots {c}_c\right)=\sum \limits_{i=1}^c{J}_i=\sum \limits_{i=1}^c\sum \limits_j^n{u}_{ij}^m{d}_{ij}^2 $$
(6)

Where uij is between [0,1], ci is the clustering center of fuzzy group i, dij = ‖ci − xj‖ is the Euclidean distance between the i cluster center and the j data point, and m ∈ [i, ∞) is a weighted exponent. By constructing an objective function such as Eq. 7, the necessary conditions for the minimum value of Eq. 6 can be obtained:

$$ \overline{J}\left(\mathrm{U},{\mathrm{c}}_1,\dots {\mathrm{c}}_c,{\lambda}_1,{\lambda}_2,\dots .{\lambda}_n\right)=J\left(U,{c}_1,\dots {c}_c\right)+{\sum}_{j=1}^n{\lambda}_j\left({\sum}_{i=1}^c{u}_{ij}-1\right)=\sum \limits_{i=1}^c\sum \limits_j^n{u}_{ij}^m{d}_{ij}^2+{\sum}_{j=1}^n{\lambda}_j\left({\sum}_{i=1}^c{u}_{ij}-1\right) $$
(7)

The above algorithm can also choose to initialize the clustering center and then perform the iterative process. Because it cannot ensure that the FCM converges to an optimal solution, the performance of the algorithm depends on the initial clustering center. Therefore, we either use another fast algorithm to determine the initial cluster center or start the algorithm each time with a different initial cluster center and run the FCM multiple times.

The work of this paper is to detect the thread geometry of the nut bolt and its defects, both of which are based on machine vision. This article deals with the image acquired by the camera after writing the program in the image processing software. The image processing content includes the following parts: image grayscale, binarization, edge detection, edge sub-pixel detection, etc., achieving the ultimate goal of detecting component defects and dimensions. The edge detection algorithm and data processing method of the image are explored.

3 Experimental results

In the visual inspection system, it is necessary to establish the mutual correspondence between the location of the pixel points in the image and the location of the surface points of the space parts, for obtaining the geometric dimensions from the small parts image. This mutual correspondence is determined by the imaging system imaging model; the parameter in the model is that the camera parameter measurement system captures the image reflecting the surface characteristics of the shaft through the CCD camera. By processing the acquired image data, the computer can quickly and accurately calculate the geometric dimensions e of the small parts, and the results are displayed directly. The whole schematic diagram of machine vision system is shown in Fig. 2.

Fig. 2
figure 2

Schematic diagram of machine vision system

The dimension of the small parts is the dimension of the shape and the dimension of the position. For thin plate parts, the hole diameter and hole center distance are the parameters that need to be measured. For threaded parts, the inner diameter, outer diameter, and thread pitch are important parameters for thread measurement. To verify the accuracy and repeatability of the measurement, the test samples are shown in Fig. 3. Three standard nuts (H1, H2, H3) are used as measurement data for the inner diameter of the test circle.

Fig. 3
figure 3

Parts to be measured

The method of edge detection in this paper is to combine the Canny operator with the quadratic polynomial improved interpolation; firstly use the Canny operator to coarsely locate the edges. Then, the gradient image is calculated by adding a template operator to calculate the gradient image. Finally, the binomial expression is used to perform interpolation calculations, that is, the improved quadratic polynomial interpolation based on Canny operator. Follow the F1 coordinates to find the adjacent F0 and F2, which detects the sub-pixel edges and finds the coordinates of the edge points, as shown in Table 1.

Table 1 The direction of the template

4 Discussion

In this paper, the basic process of detecting machine parts based on machine vision is shown in Fig. 4.

Fig. 4
figure 4

Detection process

In this paper, the five million pixel camera with industrial practical value is used to analyze the accuracy of sub-pixel; the results after binarization of sample images are shown in Fig. 5. And use the interpolation method before binomial modified and after binomial modified based on Canny operator to sub-pixel locate the inner diameter and outer diameter of the test sample shown in Fig. 6。.

Fig. 5
figure 5

The actual scanned circle

Fig. 6
figure 6

Three edge points determine parameters

Regardless of the least-squares method commonly used for circle fitting or the algorithm used in this paper, it requires a single pixel at the edge. To facilitate grammatical expression, the system divides the circle in the image into two parts with a straight line passing through the center and perpendicular to the y-axis firstly, and it is shown in Fig. 7. After single-pixel refinement as shown in Fig. 8, the edges of the image are all optimized to the desired single-pixel edge and relatively complete, to facilitate the calculation of the inner hole diameter in the next step.

  1. (1)

    This paper designs two sets of experimental comparisons. The previous algorithm and the improved algorithm are tested and run by using MATLAB. Through visual and data comparison, it is judged whether the improved algorithm achieves the desired effect of this paper. Extracting sub-pixel coordinates and fitting a circle with least squares is used to find the diameter of a circle. If the higher measurement accuracy is needed, the system can also use partial close-ups to capture images. It can be seen using the machine vision technology to perform 5 measurements of the sample data as shown in Fig. 9. There are three values compared in Fig. 9 such as the standard value, the measured value using the sub-pixel algorithm measurement, and the measured value using the improved algorithm in this article. The measurement error of the sub-pixel algorithm is 0.0314, 0.0304, 0.0304, 0.0315, and 0.0305, while the error of the improved algorithm in this paper is 0.0108, 0.0110, 0.0107, 0.0108, and 0.0108. The average error was improved from 0.0305 to 0.0108. Under the same threshold conditions, the improved sub-pixel edges detect more pixels, the edges looked finer, and the improved edge detection effect is better than the improved edge detection effect.

  2. (2)

    The inner circle of the three nut samples is sub-pixel-positioned by using this algorithm, sub-pixel coordinates are extracted, and the circle diameter is obtained by fitting the circle by least squares method. The inner diameter data is measured as shown in Table 2. The average of the three measured samples (actual measured values) are 3.403 (3.372), 3.407 (3.413), and 3.407 (3.418), and the average error values are 0.021, 0.015, and 0.011 respectively. According to the test results, the detection method proposed in this paper satisfies the detection requirements.

Fig. 7
figure 7

The left half and right half

Fig. 8
figure 8

Single pixel stitching and refinement results

Fig. 9
figure 9

Comparison of measured value (unit mm)

Table 2 Inner diameter of three test components

5 Conclusions

This paper discusses the measurement of micro-miniature parts based on machine vision. The binomial interpolation sub-pixel algorithm is analyzed and optimized. Finally, the Cannes operator is used as the edge coarse positioning and the binomial interpolation is used to improve the sub-pixels for edge fine positioning. It completes the non-contact measurement of the critical part size based on the least squares straight line fitting and circle fitting, and the proposed method can meet the testing requirements. The measurement range is extended while ensuring measurement accuracy.

From the above results, it can be seen that the accuracy of the sub-pixel edge detection based on the improved binomial interpolation is higher than the accuracy of the original binomial interpolation edge detection. And the error is reduced from the general 0.0305 to 0.0108. At the same time, with the increase of the number of measurements, the error of the test sample is decreasing. The error between the measured average and the measured value is smaller; the accuracy of the measurement is improved. The results show that whether in the detection efficiency or measurement accuracy, the requirements of the engineering practice are met.