Adaptive Bidirectional Gray-Scale Center of Gravity Extraction Algorithm of Laser Stripes

Aiming at the realization of fast and high-precision detection of the workpiece, an adaptive bidirectional gray-scale center of gravity extraction algorithm for laser stripes is proposed in this paper. The algorithm is processed in the following steps. Firstly, the initial image processing area is set according to the floating field of the camera’s light stripe, followed by setting the adaptive image processing area according to the actual position of the light stripe. Secondly, the center of light stripe is obtained by using the method of combining the upper contour with the barycenter of the bidirectional gray-scale. The obtained center of the light stripe is optimized by reducing the deviation from adjacent center points. Finally, the slope and intercept are used to complete the breakpoint. The experimental results show that the algorithm has the advantages of high speed and precision and has specific adaptability to the laser stripes of the complex environment. Compared with other conventional algorithms, it greatly improves and can be used in industrial detection.


Introduction
In the industrial production line, the real-time detection of the workpiece is an essential link in the industrial production process. However, there are some difficulties in achieving the real-time, fast and high-precision measurement of workpieces, which result from the testing environment of the factory, noise, material and testing surface. A linear-structured light measurement technology based on the triangle method is widely used in industrial measurement because it has advantages, including real-time performance, high precision and not having to touch the measuring surface. This laser scanning detection system is usually built by industrial cameras, line lasers and industrial computers. In order to realize the real-time, fast and high-precision detection of the workpiece, the pixel coordinates of the center of the light stripe are extracted by a thinning algorithm, and the pixel coordinates are converted into world coordinates by camera calibration method [1].
Usually, the methods [2] of extreme value, edge, threshold, geometric center, curve fitting and gray barycenter are used to extract the central pixel coordinates of the optical stripe. These methods have a simple calculation process, but they are easily affected by noise and have low accuracy. The gray-scale barycenter method [3][4][5][6][7][8][9][10][11][12] is simple in calculation and has been improved by researchers through combining it with other methods, such as the extremum method, principal component analysis, curve fitting algorithm, adaptive operation, mean value calculation, filtering, contour segmentation, etc. The gray-scale center of the gravity method combined with the image adaptive operation can also greatly reduce the processing time of images. Wang Fubin et al. [13] used image mask manipulation, an adaptive convolution template, the quadratic-weighted gray-scale center of gravity method and principal component analysis to obtain fringed subpixel coordinates. LU Yonghua et al. [14] segmented the strip area and obtained the width of the laser stripe by boundary detection. Then, they used the quadratic-weighted gray centroid center points. Finally, the slope and intercept are used to complete the breakpoint. To verify the running speed, measurement accuracy and adaptability of the proposed algorithm, comparative experiments are carried out between the proposed algorithm in this paper and Steger, the gray-scale center of gravity, the improved gray-scale center of gravity and geometric barycenter algorithms, which focus on four aspects, including the extraction effect, RMSD, standard deviation and average extraction time. In addition, the algorithm is used to process the image of the light stripes in different environments to verify the adaptability of the algorithm. Through these experiments, the algorithm is analyzed in detail, and the algorithm's validity in extraction accuracy and running speed is verified.

An Adaptive Bidirectional Gray-Scale Center of Gravity Extraction Algorithm
In this paper, we propose an algorithm to extract the center of a light stripe, which sets the initial image processing area according to the floating field of the light stripe in the camera. Then, the adaptive image processing area is adopted to reduce the redundant scanning of the image area without the light stripe. Secondly, the initial center coordinates of the light stripe are obtained by combining the center of gravity of transverse gray-scale with the center of gravity of longitudinal gray-scale in three steps. The slope threshold is used to optimize the weight to solve the problem of the deviation between adjacent centers. Finally, the center line is completed by slope and intercept. The detailed flow of the algorithm is shown in Figure 1.

Light Distribution Characteristics
Under the conditions that ambient light is good, the laser light quality is uniform, and the surface material of the measured object is appropriate; the cross-section intensity of the light stripe emitted by the laser is Gaussian distribution, as shown in Figure 2. However, the quality of the actual light stripe is easily affected, and the detection environment in the factory is complex, resulting in problems such as a large slope of the light stripe, broken line, fold line, curve, etc. The photo of the light streaks taken under actual conditions is shown in Figure 3. Additionally, the distribution of pixel gray values of the cross-section of the light bar is shown in Figure 4. The contrast between Figures 2 and 4 shows that the pixel gray values in the cross-section of the light stripes are not symmetrical and uniformly distributed, and the peak width of the gray values is not only one pixel.

Initial Image Processing Area Setting
In industrial detection, the line laser structure is usually used to detect a work The region of the light stripe is basically fixed, so the image can be set in the region. ever, there is mechanical vibration in a real factory detection environment, which c the light stripe to float in the image. Therefore, the primary image processing area in this paper, which not only improves the running speed of image processing bu adapts to the floating range of the field of the light stripe.
As shown in Figure 5, three kinds of calibration blocks (3, 5, 10 mm) were used experiment. In industrial detection, the thickness of the workpiece is not uniform, accompanied by the mechanical vibration, which will cause the light stripe to float image. Therefore, before image pre-processing, the initial image processing region

Initial Image Processing Area Setting
In industrial detection, the line laser structure is usually used to detect a workpiece. The region of the light stripe is basically fixed, so the image can be set in the region. However, there is mechanical vibration in a real factory detection environment, which causes the light stripe to float in the image. Therefore, the primary image processing area is set in this paper, which not only improves the running speed of image processing but also adapts to the floating range of the field of the light stripe.
As shown in Figure 5, three kinds of calibration blocks (3, 5, 10 mm) were used in the experiment. In industrial detection, the thickness of the workpiece is not uniform, being accompanied by the mechanical vibration, which will cause the light stripe to float in the image. Therefore, before image pre-processing, the initial image processing region is set according to the size range of the measured object, which can improve the speed of image processing without filtering out the useful information of light stripes. according to the size range of the measured object, which can improve the speed of image processing without filtering out the useful information of light stripes.

Image Preprocessing
In order to remove some useless interference information in the image, improve the detectability of useful information, find useful information and simplify these useful data, pre-processing operations such as gray-scale processing, binarization and Gauss filtering are used to denoise and enhance the optical stripe image in this paper.
Gray-scale processing is a pre-processing process that transforms the image from color to gray level. According to Formula (1), the R , G and B values of the color image are calculated to obtain the gray-level image, which has achieved the processing effect of image enhancement.

Image Preprocessing
In order to remove some useless interference information in the image, improve the detectability of useful information, find useful information and simplify these useful data, pre-processing operations such as gray-scale processing, binarization and Gauss filtering are used to denoise and enhance the optical stripe image in this paper.
Gray-scale processing is a pre-processing process that transforms the image from color to gray level. According to Formula (1), the R, G and B values of the color image are calculated to obtain the gray-level image, which has achieved the processing effect of image enhancement.
In the formula, Gray represents the gray value, and R, G and B represent the red, green and blue channel values of the color image, respectively.
In order to reduce the image noise and eliminate some useless interference noise in the image, the Gauss filtering method, which is a linear smoothing filter to reduce the image noise, is adopted, as shown in Formula (2).
In the formula, f (x) represents the probability, σ represents the standard deviation, µ represents the coordinate value of the curve peak center and 1 √ 2πσ represents the height of the curve peak.
In order to reduce the amount of data to be processed and find the upper contour of the light stripes conveniently, it is necessary to binary the gray image. The binarization process is the change in an image from gray or color to black and white, corresponding to two gray values, i.e., 0 and 255. The processing formula is shown in Formula (3).
In the formula, Gray(u, v) denotes the Gray value corresponding to the binarization of pixels (u, v), g(u, v) denotes the Gray value corresponding to pixels (u, v) and t denotes the global threshold.

Secondary Adaptive Image Processing Region Setting
After setting the initial image processing region, the second adaptive region is set to locate the specific position coordinates of the light stripe in the image, which can further reduce the redundant scanning of the image region without the light stripe and speed up the running rate.
The second adaptive image processing region is set on the basis of the initial image processing region. Additionally, the algorithm selects the random points v i by using the random function based on the binary image. The number of random points increases with the increase in image resolution. Then, there is a top-down and bottom-up search for edge points that meet the threshold condition. The threshold condition of the edge point is that the gray-level threshold of the point and three consecutive points in the column direction are 255. Then, the top and bottom points of the edge are selected to extend a point. The range of the extension points is set as the range of the secondary image processing area. A schematic diagram of the secondary adaptive image processing area setting is shown in Figure 6, and a flowchart is shown in Figure 7.
the increase in image resolution. Then, there is a top-down and bottom-up search for edge points that meet the threshold condition. The threshold condition of the edge point is that the gray-level threshold of the point and three consecutive points in the column direction are 255. Then, the top and bottom points of the edge are selected to extend a point. The range of the extension points is set as the range of the secondary image processing area. A schematic diagram of the secondary adaptive image processing area setting is shown in Figure 6, and a flowchart is shown in Figure 7.  points that meet the threshold condition. The threshold condition of the edge point is that the gray-level threshold of the point and three consecutive points in the column direction are 255. Then, the top and bottom points of the edge are selected to extend a point. The range of the extension points is set as the range of the secondary image processing area. A schematic diagram of the secondary adaptive image processing area setting is shown in Figure 6, and a flowchart is shown in Figure 7.

Principle of Bidirectional Center of Gravity Method
The barycenter method is to scan by column or row. The centroid of the gray value of the column or row is calculated by the gray-weighted algorithm, and its centroid is taken as the center of the light stripe. The pixel coordinate satisfying the threshold condition is u i /v i . f i is the gray value corresponding to the coordinate point in the image, and the centroid position of the column or row is obtained as The formula is as following: The gray-scale center of gravity method is used to calculate the horizontal and vertical centers of light stripes. However, in the case of a large tilt angle, the single-direction grayscale center of gravity method will result in the loss of some center points of light stripes; it is not suitable for the center extraction of all optical stripes. On the basis of the gray-scale center of gravity method, a bidirectional center of gravity algorithm is proposed in this paper. A schematic diagram is shown in Figure 8. Each upper contour point corresponds to a light stripe center point. If a center point exists in only one direction, the center point is retained. If there are two central points at the same time, the method of taking the average value preserves a central point coordinate. In Figure 8, c is the uppoint of the upper contour points of the light stripe, a is the existence of both horizontal and vertical search points and b is the existence of only one point in the horizontal and vertical search points. The flowchart is shown in Figure 9. lowing: The gray-scale center of gravity method is used to calculate the horizontal and vertical centers of light stripes. However, in the case of a large tilt angle, the single-direction gray-scale center of gravity method will result in the loss of some center points of light stripes; it is not suitable for the center extraction of all optical stripes. On the basis of the gray-scale center of gravity method, a bidirectional center of gravity algorithm is proposed in this paper. A schematic diagram is shown in Figure 8. Each upper contour point corresponds to a light stripe center point. If a center point exists in only one direction, the center point is retained. If there are two central points at the same time, the method of taking the average value preserves a central point coordinate. In Figure 8, c is the of the upper contour points of the light stripe, a is the existence of both horizontal and vertical search points and b is the existence of only one point in the horizontal and vertical search points. The flowchart is shown in Figure 9.  The bidirectional center of gravity flow chart is as follows.
(1) First, the horizontal and vertical gradation barycenter of the image are recorded respectively, and the set of the barycenter is called 1/ 2;. (2) Then, the horizontal and vertical search is carried out according to the . The The bidirectional center of gravity flow chart is as follows.
(1) First, the horizontal and vertical gradation barycenter of the image are recorded respectively, and the set of the barycenter is called P1/P2. (2) Then, the horizontal and vertical search is carried out according to the uppoint. The searching distance is defined as d1/d2, respectively, and the searching range does not exceed the width τ of the half-light stripe. The center points in P1/P2 that meet the search conditions are retained, and the ones that do not meet the search conditions are eliminated.
(3) If both horizontal and vertical search points exist, the middle point of the coordinates of two points P1/P2 will be counted as the mpoints of the middle point of the light stripe, as shown in Formula (6). Otherwise, the point of the horizontal or vertical search will be counted as the mpoints.
In Formula (6), P1(u, v) is the image coordinate of P1 and P2(u, v) is the image coordinate of P2.

The Principle of Weight Optimization
The center coordinates of the light stripe extracted by the gray-scale barycenter method have inflection points between adjacent center points, which are not smooth and have a large deviation. If the offset is large, it will affect the precision of the center coordinate of the light stripe. In order to solve this problem, the weight optimization algorithm is used to adjust the center coordinates.
Using the least squares method to fit the coordinates of the center point obtained by the improved barycenter method, the slope k and intercept b are obtained. k is used as the judging threshold. Then, the slope k 1 between the adjacent centers y i and y i+1 is calculated sequentially, and when k 1 exceeds the slope threshold k, the y i+1 is optimized by weighting.
In Formula (7), a1, a2, a3, a4 are the weight coefficients. The contrast figure of the light stripe center point distribution before and after weight optimization is shown in Figure 10. From the change in the fold line between the center points, it can be seen that the line segment of the center point is much smoother than before optimization. This is helpful to improve the accuracy of the center point.  (7), 1、 2、 3、 4 are the weight coefficients. The contrast figure of the light stripe center point distribution before and after weight optimization is shown in Figure 10. From the change in the fold line between the center points, it can be seen that the line segment of the center point is much smoother than before optimization. This is helpful to improve the accuracy of the center point.

Experimental Results and Analysis
Contrast experiments were carried out between the proposed algorithm and other traditional algorithms, including Steger, the gray center of gravity, geometric center and the improved gray-scale center of gravity algorithm. The Steger algorithm uses Hessian matrix and Taylor expansion to area the center of a light stripe. For the gray center of gravity algorithm, the centroid of gray values distribution in the direction of columns or rows is recorded as the center point of the light stripe. The geometric center algorithm takes the center of the contour points on both sides as the center point of the light stripe. The improved gray-scale center of gravity algorithm adopts a subpixel adaptive extraction algorithm for linear structure light stripe center. On the basis of the mask operation, the optical stripe center is obtained by the quadratic-weighted gray-scale center of gravity, regional growth and principal component analysis. Figure 11 shows the experimental platform we built. For the experiment, the CPU used was Intel (R) Core (TM) i7-9700F CPU@3.00 GHz with 16 GB of memory. In this experiment, we selected VS2015 as the development platform and called OpenCV to complete the optimization of the algorithm. The camera used in the experiment was a MER-051-120GM/C-P, and the image resolution was 808 × 608 pixel. The experimental results obtained by the presented algorithm in this paper were compared with that of the traditional gray-scale barycenter method, the geometric center method, the improved gray-scale center of gravity algorithm and the Steger algorithm. The display results in the image were rounded to subpixels. Figure 12 shows the original image of the light stripe and the central image of the light stripe (including the local magnification image) extracted by the algorithms. Figure 13 is the light stripe center graph extracted by the new algorithm.
As shown in Figures 12 and 13, there are some outer points in the center of the light stripe extracted by the Steger algorithm, which affects the precision. The center point of the light stripe extracted by the gray-scale center of gravity algorithm fluctuates greatly, and the accuracy will become very low when the center slope of the extracted light stripe is large. The center of the light stripe extracted by the geometric center algorithm has a certain deviation. Especially, when the image has a lot of noise, the deviation will be very large. The center of the light stripe extracted by the improved gray-scale center of the gravity algorithm has a slight fluctuation, and the extraction effect is also very good. There is no outer point in the center of the light stripe extracted by this algorithm, and all the center points are extracted with good continuity and smoothness.
plete the optimization of the algorithm. The camera used in the experiment was a MER-051-120GM/C-P, and the image resolution was 808 × 608 pixel. The experimental results obtained by the presented algorithm in this paper were compared with that of the traditional gray-scale barycenter method, the geometric center method, the improved grayscale center of gravity algorithm and the Steger algorithm. The display results in the image were rounded to subpixels. Figure 12 shows the original image of the light stripe and the central image of the light stripe (including the local magnification image) extracted by the algorithms. Figure 13 is the light stripe center graph extracted by the new algorithm.   As shown in Figures 12 and 13, there are some outer points in the center of the light stripe extracted by the Steger algorithm, which affects the precision. The center point of the light stripe extracted by the gray-scale center of gravity algorithm fluctuates greatly, and the accuracy will become very low when the center slope of the extracted light stripe is large. The center of the light stripe extracted by the geometric center algorithm has a certain deviation. Especially, when the image has a lot of noise, the deviation will be very  As shown in Figures 12 and 13, there are some outer points in the center of the light stripe extracted by the Steger algorithm, which affects the precision. The center point of the light stripe extracted by the gray-scale center of gravity algorithm fluctuates greatly, Figure 13. The center graph of the light stripe extracted by the proposed algorithm.
The accuracy of the algorithm was further analyzed, and the difference between the measured values is calculated by the RMSD. The RMSD represents the difference between the mean of the center point and the center point of each light stripe. The root-mean-square deviation (RMSD) of each algorithm is shown in Table 1. The RMSD is where n is the number of center points, y i is the predicted value of the first center point and y is the true center of each center point. It can be seen from Table 1 that the RMSD of the proposed algorithm is the smallest, and the accuracy of the center point of the light stripes extracted by the proposed algorithm is the best.
The standard deviation can be used as a measure of uncertainty and is suitable for reflecting the discreteness of data. First, the center pixel coordinates of the light stripe extracted by the algorithm are fitted to a straight line by the least square method. The formula is as following: where A, B and C are the coefficients obtained by fitting the linear equation. (x i , y i ), corresponding to the image coordinates of each central point, calculate the distance d i from the center pixel coordinates on the line, which is The standard deviation of the distance from the center pixel coordinates to the line extracted by the algorithm represents the accuracy of the center pixel coordinates extracted by the algorithm. n is the number of center points, d is the average value of the distance from all the center pixel coordinates to the line and the standard deviation ε of the center pixel coordinates to the line is The standard deviation of each algorithm is shown in Table 2. Compared with the other four algorithms, the standard deviation of the proposed algorithm is the smallest, and the precision of the center pixel coordinates is the best. In order to verify the extraction speed of this algorithm, we tested the images with different laser line width by using each algorithm, and the images with different resolution were collected in seven different conditions. No less than 20 images were taken in each case, and the average extraction time of each case is calculated. The average extraction time of each algorithm is shown in Table 3. The average extraction time of each algorithm was transformed into the polyline in Figure 14, which makes the observation more intuitive. tion time of each algorithm is shown in Table 3. The average extraction time of each algorithm was transformed into the polyline in Figure 14, which makes the observation more intuitive.  The experimental data show that the speed of this algorithm is 2.82 times faster than that of the Steger algorithm, 1.34 times faster than that of the gray centroid algorithm, 1.29 times faster than that of the geometric centroid algorithm and 1.76 times faster than that of the improved gray-scale center of gravity algorithm. It can be seen from the polyline in Figure 14 that the time to extract the center of the laser stripe by using this algorithm increases with the increase in the image resolution. Compared with other algorithms, the speed-up effect is not significant when the image resolution is small, but the speed-up effect will become more significant with the increase in image resolution.
The laser stripes are all straight lines. In order to verify the accuracy of the algorithm, a group of curve laser stripes were analyzed by the . The laser stripes of the curve are shown in Figure 15, and the calculated by each algorithm is shown in Table 4. The experimental data show that the speed of this algorithm is 2.82 times faster than that of the Steger algorithm, 1.34 times faster than that of the gray centroid algorithm, 1.29 times faster than that of the geometric centroid algorithm and 1.76 times faster than that of the improved gray-scale center of gravity algorithm. It can be seen from the polyline in Figure 14 that the time to extract the center of the laser stripe by using this algorithm increases with the increase in the image resolution. Compared with other algorithms, the speed-up effect is not significant when the image resolution is small, but the speed-up effect will become more significant with the increase in image resolution.
The laser stripes are all straight lines. In order to verify the accuracy of the algorithm, a group of curve laser stripes were analyzed by the RMSD. The laser stripes of the curve are shown in Figure 15, and the RMSD calculated by each algorithm is shown in Table 4.  Compared with the experimental data in the table above, the proposed algorithm has high accuracy and is suitable not only for the center extraction of linear laser stripe but also for the center extraction of curvilinear laser stripe.
In order to verify the applicability and anti-noise of the algorithm, some images were collected in the complex detection environment of the factory. The images were processed by the proposed algorithm. Figure 16 is the result of the algorithm. It can be seen from Figure 16 that the algorithm presented in this paper has effective results, wide applicability and a certain degree of anti-noise.

Conclusions
In this paper, an adaptive bidirectional gray-scale center of gravity extraction algorithm for laser stripes was proposed, and the core of the algorithm was described in detail. The algorithm preserves only the light stripe region of the image as far as possible. At the  Compared with the experimental data in the table above, the proposed algorithm has high accuracy and is suitable not only for the center extraction of linear laser stripe but also for the center extraction of curvilinear laser stripe.
In order to verify the applicability and anti-noise of the algorithm, some images were collected in the complex detection environment of the factory. The images were processed by the proposed algorithm. Figure 16 is the result of the algorithm.  Compared with the experimental data in the table above, the proposed algorithm has high accuracy and is suitable not only for the center extraction of linear laser stripe but also for the center extraction of curvilinear laser stripe.
In order to verify the applicability and anti-noise of the algorithm, some images were collected in the complex detection environment of the factory. The images were processed by the proposed algorithm. Figure 16 is the result of the algorithm. It can be seen from Figure 16 that the algorithm presented in this paper has effective results, wide applicability and a certain degree of anti-noise.

Conclusions
In this paper, an adaptive bidirectional gray-scale center of gravity extraction algorithm for laser stripes was proposed, and the core of the algorithm was described in detail. The algorithm preserves only the light stripe region of the image as far as possible. At the It can be seen from Figure 16 that the algorithm presented in this paper has effective results, wide applicability and a certain degree of anti-noise.

Conclusions
In this paper, an adaptive bidirectional gray-scale center of gravity extraction algorithm for laser stripes was proposed, and the core of the algorithm was described in detail. The algorithm preserves only the light stripe region of the image as far as possible. At the same time, the center point of the light stripe is also preserved by using the method of contour and gray barycenter so that the speed, precision and adaptability of the center extraction of the light stripe are improved. The experimental results show that the proposed algorithm is faster and more accurate than Steger, gray-scale barycenter, the improved gray-scale center of gravity and geometric centroid algorithms. The algorithm is suitable for the cases of the large slopes, broken lines, fold lines and curves of light stripes. The algorithm's speed is 2.82 times faster than that of Steger algorithm and 1.34 times faster than that of gray centroid method. The standard deviation of extraction accuracy reached 0.314051 pixels. The algorithm in this paper has high measurement accuracy and adaptability in practical detection.