Automatic Measurement of External Thread at the End of Sucker Rod Based on Machine Vision

Aiming at the low efficiency of manual measurement of threads and the lack of practicability in machine vision measurement before, online size measurement of threads at the end of sucker rods based on machine vision was studied. A robotic arm is used to carry an optical device to achieve high-quality image acquisition of threads. Based on the prior knowledge of the thread profile angle, the directional edge detection operator is customized to achieve the accurate detection of the left and right edges of the thread. Noise filtering, sorting, and left and right edge-matching algorithms based on connected domains are developed to eliminate the interference effects of electrostatic dust and oil pollution in online measurement, and the dimension of thread profile angles, pitches, major diameters, and minor diameters can be precisely calculated. The experimental results show that the screw thread parameter measurement time is about 0.13 s; the maximum and minimum average errors of the thread angles are 0.011° and 0.632°, respectively; and the total average deviation is less than 0.08°. For the screw thread pitch, major diameter, minor diameter, and pitch diameter parameter measurement, the deviation of the measurement results between the proposed method and the universal tool microscope (UTM) method is less than 10 μm. It fully proves the effectiveness and accuracy of the method in this paper and, at the same time, shows that the method has good real-time performance and high application significance, which lays a good foundation for the subsequent online thread measurement.


Introduction
A sucker rod is an important component of sucker rod pumping equipment, which transmits the power of the pumping unit to the downhole oil pump. The fatigue strength and service life of a sucker rod determine the maximum pumping depth and displacement of the whole set of oil pumping equipment. In the process of oil production, a sucker rod is subjected to asymmetric cyclic loads, and the breakage of the sucker rod will seriously affect the production of crude oil and increase the cost of workover operations and the cost of crude oil. The failure parts of a sucker rod are mainly concentrated in the external thread and the thread relief groove, so the processing quality of the thread directly affects the life of a sucker rod [1][2][3].
The structure of the end of a sucker rod is shown in Figure 1. At present, thread parameter measurement usually adopts the method of externalthread go-stop gauge, which is not only time-consuming and labor-intensive, slow in measurement speed, and low in reliability but also prone to problems such as missed inspections and misjudgments. Unqualified sucker rods that are missed or misjudged will cause major production safety accidents and huge economic losses. The development of online rapid precise measurement system has become an urgent problem to be solved, and machine vision technology provides the possibility for the realization of this function. At present, thread parameter measurement usually adopts the method of external-thread go-stop gauge, which is not only time-consuming and labor-intensive, slow in measurement speed, and low in reliability but also prone to problems such as missed inspections and misjudgments. Unqualified sucker rods that are missed or misjudged will cause major production safety accidents and huge economic losses. The development of online rapid precise measurement system has become an urgent problem to be solved, and machine vision technology provides the possibility for the realization of this function.
A variety of approaches have been developed for the precision measurement of external thread. In terms of the measurement method, it is mainly divided into three-and two-dimensional measurements.  A variety of approaches have been developed for the precision measurement of external thread. In terms of the measurement method, it is mainly divided into three-and two-dimensional measurements.
Feng et al. proposed the application of corner detection in thread vision measurement [4]. Yang et al. discussed the effect of oil adhesion on the measurement of screw thread with machine vision and put forward the compensation method for the pitch diameter [5]. Lee et al. used machine vision to develop an on-machine thread measurement system for computer numerical control lathe machines and verified the feasibility and accuracy of the on-machine external thread measurement system developed in this study [6]. Man confirmed that the phenomenon of thread profile distortion always exists in measuring screw thread by projection image and proposed the compensation of thread profile distortion in image measuring screw thread [7]. Min designed the measurement method of screw thread geometric error based on machine vision, whose linear precision is less than 10 µm and can be used to detect the comprehensive parameters of screw thread [8]. Senthil et al. proposed the vision measurement of metric screw thread parameters based on orientation invariant feature [9]. Lavrinov et al. presented the laser triangulation 2D scanner signal processing for premium thread pitch measurement [10]. Gadelmawla proposed the computer vision algorithms for measurement and inspection of external screw threads, and the measurement results showed that the maximum difference between the standard and measured values was ±5.4 µm, which shows a good accuracy for measurement [11]. Jing designed a comprehensive measurement system for screw thread parameters based on machine vision, and the feasibility and correctness of this method have been theoretically and practically validated [12]. Wu et al. proposed the thread measurement algorithm research and software development, which does some simple refits to the existing 19JA universal tool-measuring microscope [13]. Shao et al. designed a noncontact thread parameter measuring system based on an array CCD, and the measurement results obtained indicate that this system has advantages such as quick test, high precision, and good repeatability [14]. Hunsicker et al. proposed an automatic vision inspection and measurement system for external screw threads [15].
Antonin proposed a measurement algorithm that adopts the principle of laser triangulation to realize the three-dimensional measurement of the profile parameters of a single cross-section of a screw thread [16]. Farana et al. proposed a method of screw thread measurement using a 3D vision system, which is fast enough to be used on every element in the production line and accurate enough to provide reliable data about the quality of the final product [17]. Sheng et al. presented a new compensation method for measurement of the thread pitch diameter by profile scanning [18]. Tong et al. designed a novel laser-based system for measuring internal thread parameters, and the experimental results show that the measurement accuracy of the thread angle is 2 and the other parameters are less than 4 µm [19]. Kosarevsky proposed the detection of screw threads in computed tomography 3D density fields, and the described method can be used to automate many operations during screw thread inspection process [20]. Jiang et al. proposed the research on screw thread vision measurement, analyzed and simulated the screw thread's 3D mathematic model, and, finally, provided a corresponding algorithm to correct the calculation error resulting from the shading effect of backlight projection [21]. Latypov developed an algorithm to detect screw threads in planar point clouds. The described method can be used to automate many operations during screw thread pre-alignment and drastically reduce the operator's influence on the measurement process resulting in lower measurement times and increased repeatability [22]. Huang et al. developed a laser-based measuring system, which integrated the triangulation laser measuring system and the dual-axis rotary stage for the thread profile of ball screws [23].
Although existing methods and systems can provide high-precision thread detection results, their designs have the following problems in practical application.
(1) At present, the screw thread images in most literature are collected under the ideal conditions of the laboratory, and the structural requirements of the image acquisition system in the complex environment during on-machine measurement are not considered. (2) Due to the existence of the helical structure in the thread, the position of the thread teeth in the different thread images collected on-machine is uncertain, and the template matching method based on grayscale or shape cannot solve the precise positioning of the thread teeth; therefore, the precise measurement of the thread cannot be realized. (3) Due to the generation of static electricity, iron filings or dust will adhere to the thread teeth during the threading process. How to design algorithms to effectively remove such noise factors is also the focus of research.
Aiming at the above problems, inspired by the robotic arm in the literature [6], a machine vision system with an adjustable light source device based on a manipulator is designed to solve the first problem above. Based on the characteristics of the thread profile, this paper extended the concept of directional edge detection in the literature [8], and a directional edge detection operator is proposed to solve the problem of precise positioning of the thread teeth. Noise filtering algorithms are developed based on the connected domain to solve the third problem above, and the on-machine high-precision measurement of thread parameters is realized.

Sucker Rod Thread
In practical applications, there are many types of sucker rods. The research object selected in this paper is the model 1 1 16 , whose theoretical geometric dimension requirement is shown in Figure 2.

Sucker Rod Thread
In practical applications, there are many types of sucker rods. The research object selected in this paper is the model 1 , whose theoretical geometric dimension requirement is shown in Figure 2. The geometric dimension of the sucker rod thread includes five parameters: the major diameter, minor diameter, effective diameter, pitch, and thread angle. Table 1 lists the measurement standard of the end thread of the sucker rod with model 1 , which is developed by the American Petroleum Institute. The geometric dimension of the sucker rod thread includes five parameters: the major diameter, minor diameter, effective diameter, pitch, and thread angle. Table 1 lists the measurement standard of the end thread of the sucker rod with model 1 1 16 , which is developed by the American Petroleum Institute.

Measurement System
The screw thread measurement system is shown in Figure 3, and it includes a machine vision module, manipulator, and analog CNC lathe machine tool. Among them, the manipulator is a robot produced by ABB, which can realize six degrees of freedom movement and meet the needs of complex working conditions. The machine vision module includes a camera, telocentric lens, and parallel light source.

Principle of Dimension Calculation
Most of the previous literature on thread measurement directly adopts the standard part ratio method [Error! Reference source not found.,Error! Reference source not found.], which is the ideal condition and cannot be applied to the actual production line. In order to improve the robustness of dimensional measurement accuracy, this paper The measurement based on machine vision mainly includes two parts: the hardware and software parts. The hardware system is responsible for stably and reliably collecting high-quality thread images, involving the selection of light sources, cameras, and lenses, as well as reasonable layout; the software part is responsible for the processing and analysis of thread images, and the accurate calculation of various parameters of the screw thread.
In order to ensure the accuracy of thread dimension measurement, the machine vision module adopts a high-performance industrial camera to capture the thread image at a resolution of 5480 × 3648. Taking full account of the structure and measurement tolerance requirements of the measured screw thread, the parameters of the camera and lens are listed in Table 2. In order to realize multitype thread measurement, the manipulator can carry the light source device for adaptive light source adjustment. The manipulator motion trajectory planning includes the origin waiting position, internal workpiece position, and motion trajectory. The motion trajectory needs to be taught in advance to avoid collisions between the manipulator and the CNC lathe machine tool.

Principle of Dimension Calculation
Most of the previous literature on thread measurement directly adopts the standard part ratio method [8,11], which is the ideal condition and cannot be applied to the actual production line. In order to improve the robustness of dimensional measurement accuracy, this paper converted pixel coordinates to world coordinates for dimension calculation based on camera calibration parameters.
The height of the diffuse reflection light source plate 4 is at the same height as the center line of the tested thread by adjusting the height adjustment mechanism 6. On the premise of ensuring that the captured image is clear, 20 pictures of the calibration plate 5 in different positions in space are collected, and the first picture is selected as the main world coordinate system. The camera calibration system is shown in Figure 4.  Based on pinhole projection, assuming the measurement plane in the main world coordinate system is = 0 , the perspective projection formula from the world coordinates to the pixel coordinates [24] is as follows: Based on pinhole projection, assuming the measurement plane in the main world coordinate system is Z W = 0, the perspective projection formula from the world coordinates to the pixel coordinates [24] is as follows: In the equation, (u, v) is the coordinate in the pixel coordinate system, and (X w , Y w , 0) in the main world coordinate system.
represents the intrinsic camera parameters, and R T represents the extrinsic camera parameters. H is the product of the above two, which is a three-order matrix. s is the scale factor, and R and T denote the rotation and translation matrixes, respectively. The result of the camera calibration distortion parameter is as follows: The above distortion parameter results prove that the distortion of telecentric imaging is small, which ensures the accuracy of visual dimension measurement.
The result of the internal parameter matrix obtained by the camera calibration is as follows: The result of the external parameter matrix obtained in the main world coordinate system by the camera calibration is as follows: Given the coordinate of the pixel point, multiply the inverse matrix of H to the left, and then obtain the corresponding world coordinate by de-homogenization.
After solving formula (7) to obtain X w and Y w , the real space point (X w , Y w , 0) in the main world coordinate system is obtained. The screw thread dimension measurement is the distance between these real points.

MPEE and Measurement Uncertainty
For a high-precision measurement system, there are many factors that affect the measurement error, which need to be verified by experiments. In order to verify the validity of the above-mentioned visual measurement system, the maximum permissible error for length measurement (MPEE) and the measurement uncertainty of the measurement system are given. In this paper, the following verification scheme was designed with reference to the MPEE calculation process of CMM and the uncertainty of length measurement in the literature [25]. In this paper, three standard length gauge blocks and three angle gauge blocks were selected to realize the calculation of MPEE of this vision system. The three length gauge blocks are all grade I precision gauge blocks; the theoretical values are 5 mm, 8 mm, and 20 mm, respectively, and the accuracy deviations are ±0.2 µm, ±0.2 µm, and ±0.3 µm, respectively. The theoretical values of the three grade I angle gauge blocks are 45 • , 60 • , and 75 • , respectively, and the accuracy deviations are all ±10". Because none of the measuring equipment in our laboratory can realize the true value measurement of the above-mentioned length gauge blocks and angle gauge blocks, this paper considered that the theoretical value of each gauge block is its true value. The measurement experiment process is shown in Figure 5.

/
After solving formula (7) to obtain and , the real space point ( , , 0) in the main world coordinate system is obtained. The screw thread dimension measurement is the distance between these real points.

MPEE and Measurement Uncertainty
For a high-precision measurement system, there are many factors that affect the measurement error, which need to be verified by experiments. In order to verify the validity of the above-mentioned visual measurement system, the maximum permissible error for length measurement (MPEE) and the measurement uncertainty of the measurement system are given. In this paper, the following verification scheme was designed with reference to the MPEE calculation process of CMM and the uncertainty of length measurement in the literature [25]. In this paper, three standard length gauge blocks and three angle gauge blocks were selected to realize the calculation of MPEE of this vision system. The three length gauge blocks are all grade I precision gauge blocks; the theoretical values are 5 mm, 8 mm, and 20 mm, respectively, and the accuracy deviations are ±0.2 μm, ±0.2 μm, and ±0.3 μm, respectively. The theoretical values of the three grade I angle gauge blocks are 45°, 60°, and 75°, respectively, and the accuracy deviations are all ±10''. Because none of the measuring equipment in our laboratory can realize the true value measurement of the above-mentioned length gauge blocks and angle gauge blocks, this paper considered that the theoretical value of each gauge block is its true value. The measurement experiment process is shown in Figure 5.  Each of the above length gauge blocks is placed in five different positions, and three different poses are randomly assigned to each position. For the length measurement experiment, a total of 3 × 5 × 3 = 45 images were collected. The above same process was used in the angle measurement experiment, and 45 angle gauge block images of angle calibration were also obtained. The five positions in the camera's field of view are shown in Figure 6. The acquired images of the length and angle gauge blocks are shown in Figures 7 and 8, respectively.
Each of the above length gauge blocks is placed in five different positions, and three different poses are randomly assigned to each position. For the length measurement experiment, a total of 3 × 5 × 3 = 45 images were collected. The above same process was used in the angle measurement experiment, and 45 angle gauge block images of angle calibration were also obtained. The five positions in the camera's field of view are shown in Figure 6. The acquired images of the length and angle gauge blocks are shown in Figures 7 and 8, respectively.  used in the angle measurement experiment, and 45 angle gauge block images of angl calibration were also obtained. The five positions in the camera's field of view are show in Figure 6. The acquired images of the length and angle gauge blocks are shown i Figures 7 and 8, respectively. After obtaining the collected image of the standard gauge block, based on the camera calibration parameters obtained above, the canny edge detection algorithm is used to realize the visual measurement of the length and angle. The measurement results are shown in Table 3.  After obtaining the collected image of the standard gauge block, based on the camera calibration parameters obtained above, the canny edge detection algorithm is used to realize the visual measurement of the length and angle. The measurement results are shown in Table 3.

MPEE
(30 + L/10) µm L is the measured length, in mm 60 0 0 -360 0 angle measurement As shown in the above table, after measurement and calculation, the MPEE of the visual measurement system in this paper is 30 + L 10 µm, and the accuracy deviation of the angle is 60 . The measurement results fully verify the validity and accuracy of the measurement system in this paper.

Algorithm and Principles
The actual captured thread image and nomenclatures of thread teeth are shown in Figure 9a,b, respectively. The screw thread dimension measurement in this study measures the thread angle; pitch; and minor, major, and pitch diameters in Figure 9b. As shown in the above table, after measurement and calculation, the MPEE of the visual measurement system in this paper is 30 + μm, and the accuracy deviation of the angle is 60′′. The measurement results fully verify the validity and accuracy of the measurement system in this paper.

Algorithm and Principles
The actual captured thread image and nomenclatures of thread teeth are shown in Figure 9a,b, respectively. The screw thread dimension measurement in this study measures the thread angle; pitch; and minor, major, and pitch diameters in Figure 9b. The basic specification of the screw thread is shown in Figure 10. The procedure to measure the thread dimensions is shown in Figure 11. The basic specification of the screw thread is shown in Figure 10. As shown in the above table, after measurement and calculation, the MPEE of the visual measurement system in this paper is 30 + μm, and the accuracy deviation of the angle is 60′′. The measurement results fully verify the validity and accuracy of the measurement system in this paper.

Algorithm and Principles
The actual captured thread image and nomenclatures of thread teeth are shown in Figure 9a,b, respectively. The screw thread dimension measurement in this study measures the thread angle; pitch; and minor, major, and pitch diameters in Figure 9b. The basic specification of the screw thread is shown in Figure 10. The procedure to measure the thread dimensions is shown in Figure 11. The procedure to measure the thread dimensions is shown in Figure 11.  Figure 11. Procedure to measure thread dimensions.
The procedure of the screw thread dimension measurement includes light so device adjustment, image preprocessing, thread teeth positioning, thread image anal and dimension measurement. The screw thread dimension measurement starts from positioning of the spindle of the CNC lathe to determine the position of the th workpiece to be measured; and then the manipulator carries the light source devic lighting; after that, screw thread images were captured through the vision modul order to improve the contrast between the screw thread profile and the backgroun the image, the method of back-to-parallel light source illumination is adopted, comb with a high-intensity light source, to obtain a clear and sharp thread profile, so a quickly and easily realize subsequent image processing and analysis.
In order to improve the screw thread dimension measurement speed, the orig screw thread image is cropped to obtain the region of interest (ROI) by fixed pixel and column coordinate position because the position of the threaded workpiece t tested is almost unchanged. Next, we perform image grayscale, pixel smoothing filte and OTSU binarization image preprocessing operations on the ROI area. The obta binarized image retains the core contour information of the screw thread and then the custom directional edge detection operators (DEDOs) to detect the left and The procedure of the screw thread dimension measurement includes light source device adjustment, image preprocessing, thread teeth positioning, thread image analysis, and dimension measurement. The screw thread dimension measurement starts from the positioning of the spindle of the CNC lathe to determine the position of the thread workpiece to be measured; and then the manipulator carries the light source device for lighting; after that, screw thread images were captured through the vision module. In order to improve the contrast between the screw thread profile and the background in the image, the method of back-to-parallel light source illumination is adopted, combined with a highintensity light source, to obtain a clear and sharp thread profile, so as to quickly and easily realize subsequent image processing and analysis.
In order to improve the screw thread dimension measurement speed, the original screw thread image is cropped to obtain the region of interest (ROI) by fixed pixel row and column coordinate position because the position of the threaded workpiece to be tested is almost unchanged. Next, we perform image grayscale, pixel smoothing filtering, and OTSU binarization image preprocessing operations on the ROI area. The obtained binarized image retains the core contour information of the screw thread and then uses the custom directional edge detection operators (DEDOs) to detect the left and right edges of the screw thread. After that, we use the connected domain area and its minimum circumscribed rectangle inclination (CDAMRI) algorithm to filter out the noise area such as dust and oil. Then, in order to facilitate the pairing calculation of the left and right edges of the screw thread, the left and right contour edges of the screw thread are sequentially sorted from top to bottom and left to right based on the connected domain centroid (CDC) method. Actually, on account of the obstruction of dust and oil, the original thread profile edge may be divided into several isolated connected domains after edge detection by DEDO; we design the connected domain union (CDU) algorithm to judge whether the adjacent connected domains are collinear; and then we perform straight-line fitting on each independent unified connected domain and use the left and right edges matching (LREM) algorithm to position each complete thread. Finally, we apply a screw thread parameter calculation (STPC) algorithm to obtain the measured parameter results. The principles of the key algorithms are explained as follows.

Image Processing
After cropping the original image to obtain the ROI, the three-channel original image is grayed by the weighted average method to obtain a single-channel image [27], which improves the subsequent image processing speed. Because the screw thread dimension measurement system is performed on-machine, the actual captured image, which is disturbed by noise, is filtered by a three-order Gaussian convolution kernel [28]. After that, the OTSU [29] with adaptive threshold is used to realize the binarization of the image, and the obtained binarized image data are simple in content and convenient for subsequent image processing and, at the same time, retain the core profile information of the thread to ensure the measurement accuracy.

Directional Edge Detection Operator (DEDO) for Screw Thread Positioning
The premise of obtaining the accurate measurement of screw thread parameters is to realize the accurate positioning of the screw thread. In the actual production process, the position and orientation of the camera in the vision module are fixed, and the spindle of the CNC lathe machine tool rotates with the screw thread to be measured. Due to the characteristics of the helical structure of the screw thread, the positions of the screw thread profile are uncertain when capturing the screw thread image at different times. This means that precise positioning of thread teeth cannot be achieved by template matching. Although there are many papers on the screw thread dimension measurement, very few papers mention the problem of automatic positioning of thread teeth. The corner detection method mentioned in [13] is used to separate the straight-line boundary and bottom arc of the thread teeth. The DP algorithm in [30] is used to approximate the thread profile, but not single thread teeth are positioned in the thread image, which makes automatic measurement of sucker rod end threads impossible. In this paper, according to the prior knowledge of the measured thread profile angle, a directional edge detection operator (DEDO) was specially designed to realize the edge detection of a specific angle in the screw thread image, so as to realize the high-precision positioning of the thread teeth.

Principle of DEDO
The calculation principle of the DEDO's convolution kernel is shown in Figure 12. The standard value of the measured thread angle is 60 • . In image edge detection, the normal direction of the edge line is the direction in which the gray value of the pixel changes the fastest. In order to enable the convolution kernel to achieve directional edge detection, it is supposed that the weight of each position in the convolution kernel is related to the distance from the center point and the distance D 1 , D 2 in Figure 12b. As shown in Figure 12b, the size of the convolution kernel is 7 × 7, and the center point of the convolution kernel is the origin of the coordinate system. The yellow line is the straight line to be detected at a given angle, and the two red dotted lines are the normal lines of the edge line passing through points P1 and P2. and are the distances from each position point in the convolution kernel to the two normal lines.
The theoretical functional relationship between weight and distance needs to be further studied and explored in the future. For the convolution kernel of 60° directional edge detection, the weight assignment algorithm is given in this paper as follows: As shown in Figure 12b, the size of the convolution kernel is 7 × 7, and the center point of the convolution kernel is the origin of the coordinate system. The yellow line is the straight line to be detected at a given angle, and the two red dotted lines are the normal lines of the edge line passing through points P1 and P2. D 1 and D 2 are the distances from each position point in the convolution kernel to the two normal lines.
The theoretical functional relationship between weight and distance needs to be further studied and explored in the future. For the convolution kernel of 60 • directional edge detection, the weight assignment algorithm is given in this paper as follows: x 2 + y 2 ≥ Thred2 and D 1 + D 2 < Thred1 2 * sgn(a e x + b e y + c e ) x 2 + y 2 < Thred2 and D 1 + D 2 < Thred1 In the equation, a e , b e , c e are the coefficients of the edge line equation, and sgn(x) is the signum function.
Based on the above algorithm principle, the convolution kernel weights are shown in Figure 12c assuming Thred1 = 2 and Thred2 = √ 2. Similarly, all directional edge detection convolution kernels of the thread teeth can be designed, which are shown in Figure 13.
As shown in Figure 13, there is a symmetric relationship between the convolution kernel weights. In fact, (b), (c), and (d) in Figure 13 can be obtained by flipping the left and right or up and down of (a).
The Sobel operator [31] is a widely used edge detection operator in image processing, which has the characteristics of simple principle and fast operation speed. It has been extended to realize edge detection in eight directions. In order to highlight the superiority of the custom DEDOs, we compare the 67.5 • and 112.5 • convolution kernels in the eightdirection Sobel operator. The 67.5 • and 112.5 • Sobel convolution kernels are shown in Figure 14, and the results of edge detection with DEDO and the Sobel operator are shown in Figure 15.
As shown in Figure 15, the custom DEDOs increase the weight of the specific directional edge while reducing the weight of the edges in other directions, and the image edge was obviously more concise and precise, which is more favorable for the edge extraction analysis. By comparison, it is found that the 67.5 • and 112.5 • Sobel operator detects more edge information, which includes the crest and root part of the screw thread, rather than a complete straight line, which interferes with the straight-line fitting of the edge. Thus, the effectiveness and superiority of the proposed DEDO operator are significantly demonstrated. is the signum function. Based on the above algorithm principle, the convolution kernel weights are shown in Figure 12c assuming ℎ 1 = 2 and ℎ 2 = √2. Similarly, all directional edge detection convolution kernels of the thread teeth can be designed, which are shown in Figure 13. As shown in Figure 13, there is a symmetric relationship between the convolution kernel weights. In fact, (b), (c), and (d) in Figure 13 can be obtained by flipping the left and right or up and down of (a).
The Sobel operator [Error! Reference source not found.] is a widely used edge detection operator in image processing, which has the characteristics of simple principle and fast operation speed. It has been extended to realize edge detection in eight directions. In order to highlight the superiority of the custom DEDOs, we compare the 67.5° and 112.5° convolution kernels in the eight-direction Sobel operator. The 67.5° and 112.5° Sobel convolution kernels are shown in Figure 14, and the results of edge detection with DEDO and the Sobel operator are shown in Figure 15.  As shown in Figure 15, the custom DEDOs increase the weight of the specific directional edge while reducing the weight of the edges in other directions, and the image edge was obviously more concise and precise, which is more favorable for the edge extraction analysis. By comparison, it is found that the 67.5° and 112.5° Sobel operator detects more edge information, which includes the crest and root part of the screw thread, rather than a complete straight line, which interferes with the straight-line fitting of the edge. Thus, the effectiveness and superiority of the proposed DEDO operator are significantly demonstrated. As shown in Figure 13, there is a symmetric relationship between the convolution kernel weights. In fact, (b), (c), and (d) in Figure 13 can be obtained by flipping the left and right or up and down of (a).
The Sobel operator [Error! Reference source not found.] is a widely used edge detection operator in image processing, which has the characteristics of simple principle and fast operation speed. It has been extended to realize edge detection in eight directions. In order to highlight the superiority of the custom DEDOs, we compare the 67.5° and 112.5° convolution kernels in the eight-direction Sobel operator. The 67.5° and 112.5° Sobel convolution kernels are shown in Figure 14, and the results of edge detection with DEDO and the Sobel operator are shown in Figure 15.  As shown in Figure 15, the custom DEDOs increase the weight of the specific directional edge while reducing the weight of the edges in other directions, and the image edge was obviously more concise and precise, which is more favorable for the edge extraction analysis. By comparison, it is found that the 67.5° and 112.5° Sobel operator detects more edge information, which includes the crest and root part of the screw thread, rather than a complete straight line, which interferes with the straight-line fitting of the edge. Thus, the effectiveness and superiority of the proposed DEDO operator are significantly demonstrated.

Noise Area Filtering Based on Connected Domain Area and Its Minimum Circumscribed Rectangle Inclination (CDAMRI)
The edge image detected by DEDOs is affected by dust and oil, and the contour edges of the thread teeth can be segmented into multiple connected domains. At the same time, the edge image may contain small discrete noise domains, which obstruct subsequent image analysis and processing. Firstly, based on the connected domain, the small discrete noise domains whose area is less than the set threshold are deleted. Then, the image is dilated to connect the originally collinear discrete connected domains. Because the result of the image dilation operation is controlled by the structural elements, in order to ensure the accuracy of thread dimension measurement, the structural elements need to expand the edge image as far as possible in the direction of the edge straight line but keep the same as far as possible in the direction of the normal line. These two requirements are exactly the functions of the above DEDOs. Thus, this paper repeatedly used the DEDOs to perform the dilation operation.
The results of image dilation are shown in Figure 16. Compared with the original edge image, the connectivity of the image is greatly improved. At the same time, it is only limited to the edge straight-line direction, which ensures the accuracy of the straight-line fitting. For other noise-connected domains, after dilation operation, the area and direction are also enhanced to a certain extent. At this time, it can be further filtered out by the area of the connected domain [32] and the angle between the long side of the minimum circumscribed rectangle of the connected domain and the X-axis. subsequent image analysis and processing. Firstly, based on the connected domain, the small discrete noise domains whose area is less than the set threshold are deleted. Then, the image is dilated to connect the originally collinear discrete connected domains. Because the result of the image dilation operation is controlled by the structural elements, in order to ensure the accuracy of thread dimension measurement, the structural elements need to expand the edge image as far as possible in the direction of the edge straight line but keep the same as far as possible in the direction of the normal line. These two requirements are exactly the functions of the above DEDOs. Thus, this paper repeatedly used the DEDOs to perform the dilation operation.
The results of image dilation are shown in Figure 16. Compared with the original edge image, the connectivity of the image is greatly improved. At the same time, it is only limited to the edge straight-line direction, which ensures the accuracy of the straight-line fitting. For other noise-connected domains, after dilation operation, the area and direction are also enhanced to a certain extent. At this time, it can be further filtered out by the area of the connected domain [Error! Reference source not found.] and the angle between the long side of the minimum circumscribed rectangle of the connected domain and the X-axis.
The minimum circumscribed rectangle inclination angle is shown in Figure 17. Let the rectangle 0 1 2 3 be the minimum circumscribed rectangle of a certain connected domain, and define the angle α between the long side 0 1 and the X-axis as the inclination of the rectangle 0 1 2 3. The angle α threshold of the left and right edges is defined as 150° and 30°, respectively. The inclination results of connected domains are shown in Figure 16e,f, and the results after filtering the noise-connected domains are shown in Figure 16g,h.  The minimum circumscribed rectangle inclination angle is shown in Figure 17. Let the rectangle P0P1P2P3 be the minimum circumscribed rectangle of a certain connected domain, and define the angle α between the long side P0P1 and the X-axis as the inclination of the rectangle P0P1P2P3. The angle α threshold of the left and right edges is defined as 150 • and 30 • , respectively. The inclination results of connected domains are shown in Figure 16e,f, and the results after filtering the noise-connected domains are shown in Figure 16g,h. the minimum circumscribed rectangle of each connected domain in the left edge dilation ima the result of the inclination angle of the minimum circumscribed rectangle of each conn domain in the right edge dilation image; (g) the left edge image after filtering ou noise-connected domains based on the inclination angle; (h) the right edge image after filterin the noise-connected domains based on the inclination angle. As shown in Figure 16, based on the CDAMRI algorithm, the noise-conn domains are well filtered out, and only the thread profile edge information w interested in is retained, which greatly facilitates the subsequent image analysis processing and improves the dimension measurement accuracy.

Connected Domain Sorting Algorithm Based on Connected Domain Centroid (CSC)
Before realizing the accurate positioning of the thread teeth, it is necessary to so connected domains to ensure that the connected domains of the left and right edg the thread teeth follow the same marking sequence rules. In this paper, the conn domain was marked from top to bottom and from left to right. Considering that the lines on the same side of the thread are approximately parallel, the centroids o connected domains can be used to judge their positional relationship. Com connectivity methods include four-connectivity and eight-connectivity. Based on eight-connectivity method, this paper used depth-first search (DFS) [Error! Refer source not found.] to locate each connected domain in the image and solve its centro Generally, the centroid calculation formula is as follows: For a two-dimensional image, it is an ideal uniform object, and the pixel coord points are discrete. The above formula can be transformed into a formula, that is average value of each coordinate point of each pixel point in the connected domain. Figure 18 shows the results of connected domain sorting. As shown in Figure 1 sequential sort of the connected domains is well-realized by applying the CDC algor which provides convenience for the subsequent image analysis and dimen measurement. As shown in Figure 16, based on the CDAMRI algorithm, the noise-connected domains are well filtered out, and only the thread profile edge information we are interested in is retained, which greatly facilitates the subsequent image analysis and processing and improves the dimension measurement accuracy.

Connected Domain Sorting Algorithm Based on Connected Domain Centroid (CDC)
Before realizing the accurate positioning of the thread teeth, it is necessary to sort the connected domains to ensure that the connected domains of the left and right edges of the thread teeth follow the same marking sequence rules. In this paper, the connected domain was marked from top to bottom and from left to right. Considering that the edge lines on the same side of the thread are approximately parallel, the centroids of the connected domains can be used to judge their positional relationship. Common connectivity methods include four-connectivity and eight-connectivity. Based on the eight-connectivity method, this paper used depth-first search (DFS) [33] to locate each connected domain in the image and solve its centroid.
Generally, the centroid calculation formula is as follows: For a two-dimensional image, it is an ideal uniform object, and the pixel coordinate points are discrete. The above formula can be transformed into a formula, that is, the average value of each coordinate point of each pixel point in the connected domain.
x = ∑ n 1 x i n , y = ∑ n 1 y i n (10) Figure 18 shows the results of connected domain sorting. As shown in Figure 18, the sequential sort of the connected domains is well-realized by applying the CDC algorithm, which provides convenience for the subsequent image analysis and dimension measurement.

Connected Domain Union (CDU) Algorithm
The above connected-domain-sorting results show that under the influence of electrostatic dust and oil in the field production environment, even after morphological dilation operations, the screw thread edge may be divided into multiple discrete connected domains. As shown in Figure 12, there are 14 connected domains on the left edge, which actually has only eight complete straight lines. Thus, it is necessary to perform a unified operation on the above sorted connected domains and unify the discrete connected domains that belong to the same thread edge as a new connected domain. Straight-line fitting is performed on each of the above connected domains in order, and the degree of coincidence of the two straight lines before and after can be judged. When the degree of coincidence is greater than the set threshold, it means the two adjacent connected domains before and after belong to the same straight line. Considering that the edge lines on the same side of each thread are approximately parallel, the inner product between the straight-line coefficients cannot be directly used for judgment. On the other hand, the intercept of the straight line and the Y-axis is significantly affected by the size of the image. Even if the angle between the two adjacent straight lines is small, if the image pixels are large, the difference between the intercepts of the two straight lines is large enough, even exceeding the pitch of the screw thread. In this paper, the connected domain centroid data obtained in the CDC algorithm were used to judge whether the two adjacent connected domains belong to the same profile edge of the thread teeth by judging the distance from the connecting centroid to the previous straight line. The pseudocode of the CDU algorithm is shown in Algorithm 1.

Input:
: Connected domain lists, : remove the connected domain whose area is smaller than

Connected Domain Union (CDU) Algorithm
The above connected-domain-sorting results show that under the influence of electrostatic dust and oil in the field production environment, even after morphological dilation operations, the screw thread edge may be divided into multiple discrete connected domains. As shown in Figure 12, there are 14 connected domains on the left edge, which actually has only eight complete straight lines. Thus, it is necessary to perform a unified operation on the above sorted connected domains and unify the discrete connected domains that belong to the same thread edge as a new connected domain. Straight-line fitting is performed on each of the above connected domains in order, and the degree of coincidence of the two straight lines before and after can be judged. When the degree of coincidence is greater than the set threshold, it means the two adjacent connected domains before and after belong to the same straight line. Considering that the edge lines on the same side of each thread are approximately parallel, the inner product between the straight-line coefficients cannot be directly used for judgment. On the other hand, the intercept of the straight line and the Y-axis is significantly affected by the size of the image. Even if the angle between the two adjacent straight lines is small, if the image pixels are large, the difference between the intercepts of the two straight lines is large enough, even exceeding the pitch of the screw thread. In this paper, the connected domain centroid data obtained in the CDC algorithm were used to judge whether the two adjacent connected domains belong to the same profile edge of the thread teeth by judging the distance from the connecting centroid to the previous straight line. The pseudocode of the CDU algorithm is shown in Algorithm 1.
Straight lines of the new connected domains, which are obtained by the CDU algorithm, are fitted with the least squares method [34], and the results are shown in Figure 19.   As shown in Figure 19, the yellow straight line is the fitting result of the new connected domain, and the red line below it is the fitting straight line of a certain connected domain. It can be found that the fitting result of the new connected domain better fits the edge information, which proves the necessity of performing the unified operation of segmented connected domains and ensures the measurement accuracy of thread dimension.

Left and Right Edges Matching (LREM) Algorithm
The thread profile edge detection results are significantly affected by on-site dust and oil pollution. If the left and right edges of the same thread teeth are polluted to different degrees, the number of detected left and right edges of the thread will be inconsistent. The inconsistent number of left and right edges is shown in Figure 20. In Figure 20, the number of left edges is one more than that on the right side, so it is necessary to judge whether the left and right edges are matched.

Left and Right Edges Matching (LREM) Algorithm
The thread profile edge detection results are significantly affected by on-site dust and oil pollution. If the left and right edges of the same thread teeth are polluted to different degrees, the number of detected left and right edges of the thread will be inconsistent. The inconsistent number of left and right edges is shown in Figure 20. In Figure 20, the number of left edges is one more than that on the right side, so it is necessary to judge whether the left and right edges are matched.     Figure 21 shows the precise positioning of the thread teeth with the LREM algorithm. After that, based on the least squares method, the parametric equation of the fitted straight line can be obtained, and then the thread teeth profile angles, as well as parameters such as the pitch and major diameter, can be obtained.  Figure 21 shows the precise positioning of the thread teeth with the LREM algorithm. After that, based on the least squares method, the parametric equation of the fitted straight line can be obtained, and then the thread teeth profile angles, as well as parameters such as the pitch and major diameter, can be obtained.

Measurement of Standard Angle Gauge Block
To verify the accuracy of the DEDOs, a standard 60° angle gauge block is tested. The captured image is preprocessed according to the above image preprocessing methods in Section 3.1, and then the directional edge detection is performed on the binarized image by applying the DEDOs. The actual gauge block angle is 60°, and the angle between the left and right sides and the horizontal line is approximately 60° and 120°, respectively. The obtained detection effect and actual measurement angle result are shown in Figure  22.

Measurement of Standard Angle Gauge Block
To verify the accuracy of the DEDOs, a standard 60 • angle gauge block is tested. The captured image is preprocessed according to the above image preprocessing methods in Section 3.1, and then the directional edge detection is performed on the binarized image by applying the DEDOs. The actual gauge block angle is 60 • , and the angle between the left and right sides and the horizontal line is approximately 60 • and 120 • , respectively. The obtained detection effect and actual measurement angle result are shown in Figure 22. As shown in Figure 22, the DEDOs accurately detect the edge line at the specified angle and calculates the included angle of the gauge block, which deviates from the standard value by less than 0.3° under the condition with noise such as dust, which proves the effectiveness and accuracy of the DEDO.

Screw Thread Parameters Calculation (STPC) Algorithm
The measurement diagram of the screw thread angle, pitch, major diameter, minor diameter, and pitch diameter parameter measurement is shown in Figure 23. As shown in Figure 22, the DEDOs accurately detect the edge line at the specified angle and calculates the included angle of the gauge block, which deviates from the standard value by less than 0.3 • under the condition with noise such as dust, which proves the effectiveness and accuracy of the DEDO.

Screw Thread Parameters Calculation (STPC) Algorithm
The measurement diagram of the screw thread angle, pitch, major diameter, minor diameter, and pitch diameter parameter measurement is shown in Figure 23.
Due to the generation of static electricity, iron filings or dust will adhere to the thread teeth during the threading process; dimension measurement by the distance between pixel points alone cannot guarantee the accuracy and stability of the measurement. Thus, this paper proposed to use the distance between the fitted straight lines to measure the size so as to ensure the accuracy and robustness of the measurement. As shown in Figure 23, the upper peak point P 1 , P 2 , P 3 is the intersection point of the fitting line between the left and right edges of the upper part of the screw thread, and the upper peak line specifically is the fitting line of the upper peak points P 1 , P 2 , P 3 . Similarly, the upper crest line is the fitting line of the upper crest points, which are the intersection points between the thread crest profile and the vertical line through the peak points. Each peak point has its corresponding crest point. In the same way, other fitted straight lines, such as the lower peak and crest line, can be obtained. Due to the generation of static electricity, iron filings or dust will adhere to the thread teeth during the threading process; dimension measurement by the distance between pixel points alone cannot guarantee the accuracy and stability of the measurement. Thus, this paper proposed to use the distance between the fitted straight lines to measure the size so as to ensure the accuracy and robustness of the measurement. As shown in Figure 23, the upper peak point , , is the intersection point of the fitting line between the left and right edges of the upper part of the screw thread, and the upper peak line specifically is the fitting line of the upper peak points , , . Similarly, the upper crest line is the fitting line of the upper crest points, which are the intersection points between the thread crest profile and the vertical line through the peak points. Each peak point has its corresponding crest point. In the same way, other fitted straight lines, such as the lower peak and crest line, can be obtained.

Screw Thread Angle
The screw thread angle is the angle between the left and right profile edges of the thread. After the algorithm analysis in the third section, the least squares method can be used to obtain the left and right thread edge fitting straight-line parameters, including the straight-line slope and the intercept on the Y-axis. The screw thread angle is given by

Pitch
The pitch is the distance between two adjacent thread teeth axes, that is, the distance between two adjacent peak points. The screw threads in the on-machine measurement are subject to many interference factors. In order to ensure the accuracy of the thread pitch measurement, the thread pitches between adjacent peak points are measured, and the average value is taken as the final thread pitch value.
In the image coordinate system, the unit of the calculated pitch is pixels, which needs to be converted to the world coordinate system. Most of the previous literature on thread measurement directly adopts the standard part ratio method, which cannot be applied to the actual production line. Based on the dimension calculation principle in

Screw Thread Angle
The screw thread angle is the angle between the left and right profile edges of the thread. After the algorithm analysis in the third section, the least squares method can be used to obtain the left and right thread edge fitting straight-line parameters, including the straight-line slope k and the intercept b on the Y-axis. The screw thread angle θ is given by

Pitch
The pitch is the distance between two adjacent thread teeth axes, that is, the distance between two adjacent peak points. The screw threads in the on-machine measurement are subject to many interference factors. In order to ensure the accuracy of the thread pitch measurement, the thread pitches between adjacent peak points are measured, and the average value is taken as the final thread pitch value.
In the image coordinate system, the unit of the calculated pitch is pixels, which needs to be converted to the world coordinate system. Most of the previous literature on thread measurement directly adopts the standard part ratio method, which cannot be applied to the actual production line. Based on the dimension calculation principle in Section 2.3, this paper converted the pixel points to the world coordinate system one by one and then calculated the distance between adjacent points in the world coordinate system to obtain the actual physical value of the thread pitch.

Major Diameter, Minor Diameter, and Effective Diameter
The major diameter of the screw thread is the distance between the crest line in the upper profile of the screw thread and the crest line in the lower profile of the screw thread. In the same way, the minor diameter is the distance between the upper and lower valley lines, and the effective diameter can be calculated by taking the average of the major and minor diameters. In actual measurement, the upper and lower crest lines cannot be guaranteed to be completely parallel, and the major diameter cannot be directly calculated from the slope and the difference of intercept. Taking the measurement of the major diameter of the thread as an example, in order to ensure the accuracy of the measurement, this paper selected multiple crest points in the upper crest line and calculated the distance of these crest points to the lower crest line. Finally, we take the average value as the major diameter of the thread. The measurement of the minor diameter is the same. It should be noted that, similar with the above pitch measurement method, the pixel coordinates are converted to the world coordinate system to obtain the actual physical size.

Discussion
In order to verify the validity of the measurement results of the machine vision algorithm proposed in this paper, a 19JA-780/250 universal tool microscope (UTM) shown in Figure 24 was used to measure the screw thread angle, major diameter, minor diameter, and pitch of the end of a sucker rod, and these measurement results are the standard reference values. The indoor temperature is 20 • C during the measurement, the graduation value for the length measurement of this device is 1 µm, and the division value of the 360 • angle measurement range is 1'. MPEE: (2.25 + L/40) um; MPEP: 1.5 um. The screw thread measurement platform by applying the UTM is shown in Figure 24.
valley lines, and the effective diameter can be calculated by taking the average of the major and minor diameters. In actual measurement, the upper and lower crest lines cannot be guaranteed to be completely parallel, and the major diameter cannot be directly calculated from the slope and the difference of intercept. Taking the measurement of the major diameter of the thread as an example, in order to ensure the accuracy of the measurement, this paper selected multiple crest points in the upper crest line and calculated the distance of these crest points to the lower crest line. Finally, we take the average value as the major diameter of the thread. The measurement of the minor diameter is the same. It should be noted that, similar with the above pitch measurement method, the pixel coordinates are converted to the world coordinate system to obtain the actual physical size.

Discussion
In order to verify the validity of the measurement results of the machine vision algorithm proposed in this paper, a 19JA-780/250 universal tool microscope (UTM) shown in Figure 24 was used to measure the screw thread angle, major diameter, minor diameter, and pitch of the end of a sucker rod, and these measurement results are the standard reference values. The indoor temperature is 20 °C during the measurement, the graduation value for the length measurement of this device is 1 μm, and the division value of the 360° angle measurement range is 1'. MPEE: (2.25 + L/40) um; MPEP: 1.5 um. The screw thread measurement platform by applying the UTM is shown in Figure 24. The results of the screw thread parameters measured by machine vision and the UTM are shown in Table 4. The display of measurement results in the screw thread is shown in Figure 25. The results of the screw thread parameters measured by machine vision and the UTM are shown in Table 4. The display of measurement results in the screw thread is shown in Figure 25. It can be shown from Table 3 that (1) The actual measured value of the tooth profile angle is greater than the theoretical value. After analysis, it is the reason for the real processing and production. As shown in Table 1, although the theoretical standard value of the thread angle is 60 • , it has a large qualified interval, from 59.5 to 61.5, and other size parameters also have corresponding size ranges. (2) The minimum and maximum deviations of the thread angle measured by the proposed method and the UTM method are 0.011 • and 0.63 • , respectively, and the total average deviation is less than 0.08 • , which proves the effectiveness and accuracy of the method in this paper for the thread angle. Moreover, the robustness of the visual measurement method is better than that of the UTM method. (3) For the screw thread pitch, major diameter, and minor diameter parameter measurement, the deviation of the measurement results of the proposed method and the UTM method is less than 10 µm. At the same time, the stability of the proposed method is better than that of the UTM method, and the effectiveness and accuracy of the visual method proposed in this paper for on-machine thread dimension measurement are significantly demonstrated. On the other hand, the completion time of the thread parameter measurement method proposed in this paper is about 0.13 s, which fully meets the real-time requirements of online thread measurement.
The positioning of each thread teeth and the measurement of each of the thread parameters are shown in Figure 25. In Figure 25, (X, Y) is the coordinate of the intersection point in the pixel coordinate system.

Conclusions
Most previous studies mainly focused on the off-machine thread dimension measurement and rarely discussed on-machine detection and measurement, which is still not up to the requirements of practical production applications. Thus, this paper studied the on-machine measurement method of sucker rod end screw threads based on machine vision. The procedure of the screw thread dimension measurement includes light source device adjustment, image preprocessing, thread positioning, thread image analysis, and dimension measurement.
(1) CNC machine tools and a manipulator are combined to achieve adjustable light source illumination for high-quality image acquisition of screw threads. (2) The custom DEDOs based on the prior knowledge of the thread profile edge angle is developed to accurately and reliably detect the left and right edges of the thread teeth for the positioning of the thread teeth. (3) CDAMR, CS, CD, and LREM algorithms are developed to eliminate the influence and interference of electrostatic dust and oil pollution in on-site measurement so as to achieve high-precision measurement of the thread profile angle, pitch, major diameter, minor diameter, and pitch diameter. (4) The experimental results show that the maximum and minimum average errors of the thread angles are 0.011 • and 0.63 • , respectively, and the total average deviation is less than 0.08 • . For the screw thread pitch, major diameter, minor diameter, and pitch diameter parameter measurement, the deviation of the measurement results of the proposed method and the UTM method is less than 10 µm. At the same time, the completion time of the thread parameter measurement method is about 0.13 s, which fully meets the real-time requirements of thread online measurement.
In general, the method proposed in this paper achieves high-precision thread parameter dimension measurement under the complex background containing dust and oil pollution, which proves the effectiveness and accuracy of the method proposed. In the future, the measurement accuracy can be further improved by optimizing the camera calibration method and using subpixels. This system has laid a good foundation for the thread on-line measurement of subsequent production lines and has great practical application significance.