Stain-Detection and Anti-Stain Algorithms Based on Dual Detector Data Interchange in Image-Type Displacement Measurement Technology

Displacement measurement technology based on image recognition algorithms represents a novel high-performance technique. In such measurements, when there are stains contaminating the measuring light path, it causes recognition errors from the linear array image sensor on the grating, resulting in inaccurate measurement results. Therefore, this report proposes stain-detection and anti-stain algorithms based on image recognition. The principle of image-type displacement measurement (IDM) technology is described, and the influence of stains on displacement measurements is analyzed according to four criteria in order to judge whether there are stains present. Then, an anti-stain algorithm based on the data exchange of dual-image sensors is proposed. Finally, experimental verifications are carried out. The experimental results show that the proposed algorithm effectively identifies the errors in the displacement measurements, when the experimental device is contaminated, the developed algorithms can accurately self-correct for the stain’s interference. The results presented herein provide a theoretical basis for developing highly-reliable displacement measurement devices.


I. INTRODUCTION
D IGITAL displacement measurement technologies mainly involve capacitive grating [1], magnetic grating [2], [3], optical grating [4], [5], and time grating [6], [7] measurement. Meanwhile, optical grating displacement measurement technology is widely used because of its strong anti-interference feature, simple structure, and ability to easily perform largerange measurements [8]. With the development of optical grating measurement technology, high-performance displacement measurements based on image recognition have emerged as a new high-performance approach [9]. Image-type displacement measurement (IDM) is a new grating measurement technology, it uses linear image sensors to detect the pattern of a calibration grating and recognize the coding lines on the grating to decode and subdivide the coded pattern on the grating. By applying an image recognition algorithm and a sub-pixel subdivision algorithm, IDM technology demonstrates high adaptability and fault tolerance. Therefore, IDM technology is crucial for the next generation of high-performance displacement measurement methods.
In the research of IDM, Sugiyam et al. [10] studied absolute displacement measurement technology employing an area array detector and obtained 14-bit angular resolution on a 30-mm-diameter circular grating based on coded positioning recognition in 2008. Later, Xu et al. [11] proposed a method of superimposing image arrays to form virtual moiré fringes, which achieved a resolution of 0.1 µm and measurement accuracy better than 0.4 µm. Das and Sarkar [12] subsequently proposed a linear displacement measurement method based on gray gradient patterns and achieved a resolution of 1 mm. Kim et al. [13] obtained 13-bit coding recognition with a measurement accuracy of 0.044 • on a 41.72-mm-diameter circular grating by applying a phase-shifting coding mode and a micro image detection system. Baji et al. [14] proposed a displacement measurement method based on color recognition, which could measure the rotation angle of coded disks coated with uniform color change; they performed rotation angle measurements with an error of 1 • . Lashmanov et al. [15] used a camera to collect and measure the scale ruler with scale lines and achieved a resolution of 0.02 µm and a measurement accuracy of 1.65 µm. More recently, Mu et al. [16] used a CMOS linear array image sensor to recognize the pseudorandom single-channel code and achieved up to 20-bit code recognition. Meanwhile, Fu et al. [17] proposed an image grating technique that achieved ±0.3 µm measurement accuracy within a 50 mm range and ±0.2 µm measurement accuracy within a 25 mm range.
The displacement measurement output may be negatively affected if the key components of photoelectric displacement measurement devices are covered with dust, oil, sewage, or other pollutants, thus significantly limiting the applicability of such technologies for industrial measurements. To improve the anti-stain properties, the current mainstream method involves increasing the sealing performance of the measuring device; however, a complex manufacturing process is required to completely and effectively seal such equipment because of the influence of the sliding guide. In addition, although magnetic grating displacement measurement technology can greatly improve the anti-stain performance [18], the application range is limited because it is difficult to achieve high-precision measurements with a large magnetic distance, which makes it prone to electromagnetic interference. Some researchers use steel tape rulers with redundancy algorithms to obtain anti-stain properties. For example, the RESOLUTE series of absolute grating rulers produced by Renishaw Company uses a steel tape ruler combined with built-in redundancy code technology to correctly read data when it is slightly polluted by dust. Similarly, the ERA series of quasi-absolute displacement measuring equipment developed by Heidenhain Company uses an optical scanning module to generate a scanning signal, which ensures that the output signal has no offset and no phase change when the reference grating is slightly polluted. Although steel strips can resist the interference of slight stains (e.g., fingerprints, water stains), the measurement output will still be inaccurate when a large amount of oil covers a large area of the measuring element.
In IDM technology, the pixel set is used to replace the moiré fringes of traditional photoelectric measurement techniques, so IDM technology is highly robust and flexible. This effectively supports the identification of stains and the realization of anti-stain properties in photoelectric displacement measurement. Yuan et al. [19] proposed a high-precision subdivision algorithm with robust performance, which exhibited a precision of 1.6" and achieved anti-interference of small stains with an outer diameter of 79 mm. Yu et al. [20] proposed a robust displacement measurement algorithm based on quadratic function fitting, which achieved a measurement accuracy of 12.85" and demonstrated resistance to slight stains on a 38-mmdiameter grating. More recently [21], an anti-stain algorithm was developed for angular displacement measurements based on a single image sensor. When an 8-bit coded grating disk was used, anti-stain properties were obtained when the stain coverage area was less than four lines.
To further enhance the anti-stain performance, this study investigated stain-detection and anti-stain algorithms for IDMs. First, we describe displacement measurement technology based on linear array image recognition. Second, we analyzed the influence of different types of stains on displacement measurement (including the influence of different areas of stains, opaque stains, and translucent stains). Third, we propose four stain detection algorithms based on image feature recognition, which can distinguish most types of stains. Finally, after the stain determination, we propose an anti-stain method using dual-image sensor data exchange, to ensure the correctness of the measurement output when the field of view of one sensor is polluted. After experiments where the image field became covered with stains, the algorithm could effectively determine whether there were stains, and ensure the correct output with the help of a dual-image sensor. When the stain coverage area was larger than the field of the image sensor, correct measurement output was still achieved. This research lays a foundation for improving the reliability of displacement measurements.
The structure of this article is as follows: Section II introduces the principle of image displacement measurement. Section III analyzes the influence of stain on IDM, and proposes the method of stain detection algorithm; Section IV  presents an anti-stain method based on a dual image sensor; Section V is simulation, and the experiment is in Section VI.

II. PRINCIPLES OF IMAGE DISPLACEMENT MEASUREMENTS
IDM technology enables absolute displacement measurements based on image recognition algorithms, and the main principle is shown in Fig. 1.
The IDM device shown in Fig. 1 includes a spindle, linear image sensor, processing circuit, grating encoder, and light source. The grating disk is installed on the rotating spindle and rotates coaxially with the spindle. The linear image sensor and the light source are fixed on both sides of the grating encoder. When the grating disk rotates, the linear image sensor and the light source do not move. The circumference of the grating disk is engraved with 2 N coding lines for absolute position recognition. The light emitted by the light source passes through the coded lines on the grating and maps the pattern of the coded lines to the linear scan image sensor. To enable the linear image sensor to capture better images, it is necessary to ensure that the distance between the linear image sensor and the grating disk is less than 1 mm. After the grating image is collected by the linear image sensor, the current rotation displacement information can be obtained via coding recognition, decoding, displacement subdivision, and other operations.

A. Principle of Absolute Coding
The coding line pattern engraved on the circumference of the grating code disk is shown in Fig. 2.
There are 2 N coding lines along the circle of the grating encoder, and the spacing between all coding lines is the same. To perform absolute displacement measurements, we set the "wide" coding line to represent the coding element "1" and the "narrow" coding line to represent the coding element "0." Because there are 2 N code lines in the circle, N represents the binary bit, and adjacent N coding lines constitute a group of coding values. With the rotation of the grating disk, the linear image sensor can detect different groups of coding values. All coding lines are then sorted according to the M-sequence pseudorandom coding mode, so that each coding value composed of all adjacent N coding elements is unique. When N = 11, the initial 11 coding elements are set to {X i−10 , X i−9 , . . . , X i−1 , X i } = {0000 0000 001}, and these code elements could be calculated using the following equation: where "⊕" denotes an XOR operation. Using (1), a total of 2048 coding elements can be calculated; thus, 2048 groups of coding values are obtained as {X i−10 , X i−9 , . . . , X i−1 , X i }. As shown in Fig. 2, the coding value in the 1 coding recognition area (CRA) is {0100 1011 000}. When the grating rotates to the left, the CRA obtained by the linear image sensor becomes 2 CRA, and the coding value is {1001 0110 000}. If the grating is rotated 2 N times, 2 N different groups of coding values are obtained. Each coding value corresponds to a unique decoding value, which is denoted as A (N -bit).
According to calculations using (1), the decoding value, A, can correspond to the value of i. So the decoding value is shown as follows A = i.

B. Principle of Displacement Subdivision
According to the principle of IDM, the resolution of an angular displacement measurement only depends on the decoded value, 360 • /2 N . Currently, the resolution of displacement measurements is low and cannot meet industrial requirements.
Therefore, in practical applications, it is necessary to further subdivide adjacent coding lines. The subdivision operation further refines the displacement measurement in the displacement subdivision area (DSA). The DSA in the collected image is then enlarged, as shown in Fig. 3.
In Fig. 3, the x-axis represents the position of the pixels, and the y-axis represents the gray value of the pixels. In DSA, the centroids of two coding lines on both sides of the y-axis are x a and x b , respectively. The centroid values of x a and x b can be calculated using the following equation: After obtaining the values of x a and x b , the subdivision operation can be performed according to the ratio algorithm shown in the following equation: where η = 2 m represents the mapping value of the distance between adjacent coding lines (meaning that the subdivision algorithm realizes m-fold subdivision). The larger the η = 2 m value, the higher the resolution. Finally, after obtaining the decoding value A (N -bit) and subdivision value B (m-bit), the final angular displacement measurement (N + m bit) can be obtained, as shown in the following equation: The resolution of such angular displacement measurements is 360 • /2 N +m .

A. Stain Analysis
For displacement measurements based on images, it is crucial to obtain an effective image of the grating disk. According to the principle of IDM, the optical imaging system mainly comprises a light source, a grating disk, and a linear image sensor, as shown in Fig. 1; in order to reduce the volume of the imaging system, it does not include an imaging lens. When a stain covers any element in the imaging system, it causes measurement error because the light spot through the grating will change, thus affecting the imaging. According to practical engineering experience, this issue can be categorized into the following six situations.
Case 1: When the stain completely covers the imaging area, most of the light is blocked. Thus, the image collected by the linear image sensor does not contain any coding pattern, and the displacement measurement will be incorrect [ Fig. 4(a)].
Case 2: When the stain covers most of the imaging area, most of the light is blocked. As a result, the image collected by the linear image sensor cannot recognize a sufficient number of coded lines, so the displacement measurement is incorrect [ Fig. 4(b)].
Case 3: When the stain covers a small part of the imaging area, some coding lines on the grating encoder are blocked. Although the image, in this case, contains a sufficient number of coding lines, the coding recognition cannot be accurately detected, so the correct decoding value cannot be obtained [ Fig. 4(c)].
Case 4: When the stain covers a small area of a coding line. In this situation, the linear image sensor can collect images normally. However, because of the stain coverage, the coding line representing "1" may be recognized as "0" during coding recognition, which leads to decoding errors [ Fig. 4(d)].
Case 5: When there is a translucent stain, which only covers part of the imaging area. If there are translucent stains in the code recognition area, the "wide" code line may be recognized as the code element "0," or some coding lines may be lost. As a result, the displacement measurement will be incorrect [ Fig. 4(e)].
Case 6: When there is a large translucent stain. Although the linear image sensor can collect the image, the coding value cannot be recognized correctly because the gray contrast value of the coding line is too low [Fig. 4(f)].
Similarly, there will be other pollution situations. However, according to the principle of IDM, as long as the completion pattern of the coding line is ensured, it will not have a great impact on the measurement. Therefore, other pollution situations are not within the scope of this manuscript.

B. Stain-Detection Algorithm
According to the stain analysis presented in Fig. 4, the developed detection algorithm includes the number of coding lines, the distance between coding lines, the decoding value, and the image mean value.
In Fig. 4(a) and (b), it is only necessary to determine whether there are enough coding lines in the collected image. Let n be the number of coding lines in the image. The circumference of the grating contains 2 N coding lines, and the number bit of coding lines is N . To meet the requirements and ensure accurate coding recognition, it is necessary to verify that there are at least N coding lines in the image field. Therefore, when n < N , it is confirmed that there is a stain, as shown in the following equation: if (n < N ) then stains found.
Criterion b: The distance between coding lines (for cases, 3 and 4).
In Fig. 4(c) and (d), it is necessary to adopt a coding line spacing criterion. According to the system design, all the coding lines on the grating disk are arranged at equal intervals. Let the radius of the coding lines be R. When there are 2 N coding lines in the circle, the preset spacing is L = 2π R/2 N . To increase the fault tolerance, the recognition interval is set as L ± L/4, and the distance between the coding lines in the collected image is l. Therefore, when the spacing l is not in the range of L ± L/4, it is confirmed that there is a stain as follows: Criterion c: The decoding value (for cases, 4 and 5).
In the case of small stains, it is not easy to detect the change of space between coding lines. Therefore, it is necessary to use a decoding value criterion. Suppose the image contains small stains, as shown in Fig. 5.
In Fig. 5, according to the normal code recognition, the obtained code value is code 1 = {0100 1010 000}, and the corresponding decoding value is A 1 . To determine whether there are stains, code 2 is recognized; code 2 = {1001 0100 000}, and the corresponding decoding value of code 2 is A 2 . According to the preset M-sequence pseudorandom coding principle, under normal circumstances, the decoding difference between two adjacent groups of coding values should be "1," i.e., A 2 -A 1 = 1. If the difference between A 2 and A 1 is not "1," this indicates the presence of stains. The decision formula is shown in the following equation: if (A 2 − A 1 ̸ = 1) then stains found.
Criterion d: The image mean value (for cases, 5 and 6). When the image of the coding line is covered by translucent stains, the contrast in the image is reduced, as shown in Fig. 4(f). In this case, the image mean criterion must be considered.
To do so, the average gray value, P, of all pixels in the collected image is calculated. Additionally, the maximum gray range of the image is set as Y , so if it is less than Y /2, there is a stain. The relevant relationship is shown in the following equation: if P < Y /2 then stains found. (8) In summary, Criterion a−d can cover most pollution situations. By simultaneously employing (5)- (8), it is possible to determine whether there are stains in the current image acquisition.

IV. ANTI-STAIN ALGORITHM WITH DUAL-IMAGE SENSORS
When the image sensor accurately determines that there are stains in the current image, the two-image sensor exchange algorithm can be applied to improve the anti-stain ability. In this case, two image sensors are placed on the top of the circular grating, as depicted in Fig. 6.
In Fig. 6, 1 and 2 are two independent imaging systems, which can recognize and subdivide the coding lines on the grating disk simultaneously. Let the measured displacement value of sensor 1 be D 1 and that of sensor 2 be D 2 . The angle between the two image sensors is β. The absolute value of the included angle between 1 and 2 sensors shall reach the maximum. So the value is set as β = 180 • .
The flowchart describing the anti-stain algorithm with dualimage sensors is shown in Fig. 7.

A. Offset Calculation
Ideally, the difference between D 1 and D 2 should be β = 180 • . However, in practical application, the difference between the measured values of sensors 1 and 2 will be offset because of the installation position and other factors. Therefore, to ensure the continuity of the output after using the replacement algorithm, the offset of D 1 and D 2 must be calculated in real time.  Suppose that the measured value D 1 comprises the main measurement data, and the measured value D 2 represents secondary measurement data. In this case, the offset between the measured value of sensor 1 and sensor 2 is given by the following equation:

B. Dual Sensor Swap Algorithm
When sensor 1 detects the presence of stains, the data from sensor 1 is discarded, and only the unpolluted sensor 2 data (i.e., D 2 ) is used for measurements to ensure accurate output results. Adding the offset value to the measured value from sensor 2 provides the output after anti-stain correction, as shown in the following equation: When sensor 2 indicates that there is a stain, there is no need to switch, and sensor 1 continues to be used for measurement output. Based on the exchange algorithm in (10), when image sensor 1 detects a stain, sensor 2 can continue to ensure normal output.

V. EXPERIMENTS
To test the feasibility of the proposed anti-stain algorithm, we used an IDM device (Fig. 8), to carry out experiments. The relevant parameters of this device are compiled in Table I. Under normal conditions, a typical image of a grating disk collected by the image sensor is shown in Fig. 9(a). According  to the recognition of "wide" and "narrow" coding lines, the coding value is {0001 1100 100}. The curve generated from continuously collecting the output value (1000 times) is shown in Fig. 9(b). It is clear that the output values in Fig. 9(b) are continuous and error-free without stains.
To verify the stain-detection and anti-stain algorithm, opaque black tape and translucent white tape are used in place of stains. The experimental process is presented in Fig. 10.
The experimental methods used to verify the effectiveness of the developed stain-detection algorithm are described here. When the algorithm is not used, the stain causes a "numerical jump" to appear in the angular displacement measurement output. Therefore, when verifying the presence of stains, the output of the angle measurement value is set to "0." As a result, only the value "0" appears when judging the presence of stains, and no other numerical jump appears. If the angle output is not "0" but there is a "numerical jump" when there are stains, this indicates that the algorithm is inaccurate.
The experimental approach used to verify the effectiveness of the anti-stain algorithm is as follows. First, the output curve is analyzed to evaluate the performance of the antistain algorithm. When there is a stain, the angle output is continuous (i.e., does not "jump"), which indicates that the anti-stain algorithm is effective. If there is still a "numerical jump" after applying the anti-stain algorithm, the algorithm is deemed invalid.
In our tests, four kinds of stain coverage were tested, as shown in Fig. 11. Four kinds of stain coverage can cover most of the pollution.   Medium-sized stain test results: (a) output from contaminated image; (b) output curve of stain-detection algorithm; and (c) output curve with anti-stain algorithm.

A. Large Opaque Stains
To test the influence of large stains on the algorithm, a piece of adhesive tape larger than the length of the image sensor (8.12 mm) is placed on the grating. The experimental situation is shown in Fig. 11(a).
When the circular grating is rotated clockwise, the angular displacement output value increases. The plot in Fig. 12(a) shows the result when the stain is about to completely cover the vision of the sensor; Fig. 12(b) shows the case when the stain is about to leave the view of the sensor. It is clear that the angular displacement cannot be measured because a significant number of coded lines are blocked. Therefore, "criterion a" and (5) can be implemented.
The curve obtained when applying the stain-detection algorithm is shown in Fig. 12(c). In the No. 0-500 region, the output becomes "0" because of the presence of stains, and there is no "numerical jump." It is clear in this case that the stain-detection algorithm correctly judges the presence of stains.
The angular displacement output curve obtained after applying the anti-stain algorithm is shown in Fig. 12(d). This curve indicates that the displacement measurement output is continuous and error-free at the position of the stains. Therefore, when a large stain blocks the grating, the angular displacement can still be measured accurately.

B. Medium Opaque Stains
To evaluate the influence of medium-sized stains on the antistain algorithm, a stain with a length L ≈ 1/4 of the image sensor length is applied on the calibration grating, as shown in Fig. 11(b).
The output collected when the stain passes through the field of sensor 1 is shown in Fig. 13(a). In the pixel region of 120-180 pixels, the collected output lacks some corresponding coding lines because they are blocked by the stain; however, the image still contains enough coding lines. Based on Fig. 13(a), the space between the coding lines at 120 and 180 pixels is much larger than the preset space. Therefore, the presence of stains can be detected using (6) and "criterion b." The output curve obtained using the stain-detection algorithm is shown in Fig. 13(b). It contains some "0" value output [rectangular box in Fig. 13(b)], which suggests a lack of contamination. In addition, there is no other "numerical jump." In this case, the detection algorithm accurately verifies the presence of stains.
When the covered area of the adhesive tape passes through the sensor's field of view, the output curve of the angular displacement measurement can be obtained after applying the anti-stain algorithm [ Fig. 13(c)]. In this case, the anti-stain algorithm is effective because continuous, error-free values are output at the position of the stain pollution.

C. Small Opaque Stains
To determine the effect of small stains on the algorithm, tape that is less than the length of a "wide" coding line is applied on the grating so it covers half of a "wide" coding line, as shown in Fig. 11(c).
The output acquired in this experiment is shown in Fig. 14(a). The coding line at the position of 220 pixels in Fig. 14(a) is clearly blocked and mistakenly identified as the coding element "0" (it should have been "1"). In this case, "criterion c" and (7) should be used to judge the stain.   In Fig. 14(a), the 11-bit encoding value recognized in rectangle 1 is {1001 1110 110}. According to Table II, the corresponding decoding value is A 1 = 1624. The coding value of the adjacent rectangle 2 is {0011 1101 100}, and its corresponding decoding value is A 2 = 868. Because A 2 -A 1 ̸ = 1, (7) indicates that there is a stain. The output curve obtained when applying the stain-detection algorithm is shown in Fig. 14(b). It is clear that there is no "numerical jump" except for the "0" value output in the rectangular box, which proves that the decision algorithm is effective.
The output curve for the angular displacement measurements after applying the anti-stain algorithm is shown in Fig. 14(c). The output value changes continuously without error, so the anti-stain algorithm is effective in this case.

D. Semitransparent Stains
To test the effectiveness of the anti-stain algorithm in the presence of semitransparent stains, white paper tape is used in place of a semitransparent stain. Paste the white paper tape on the calibration grating, as shown in Fig. 11(d).
The image collected by sensor 1 when semitransparent stains are present is shown in Fig. 15(a). The gray values of all code lines are reduced in this case, and "criterion d" with (8) can be used to detect the stain. Fig. 15(b) shows the output curve obtained when the stain-detection algorithm is used. When semitransparent stains appear in the field of view of the sensor, the output becomes "0," and there is no other "numerical jump." Therefore, the algorithm is deemed effective.
After applying the anti-stain algorithm, the angular displacement measurement output obtained is shown in Fig. 15(c). When there are translucent stains, the measurement output value is continuous, and there is no error. These results confirm that the algorithm is effective.

E. Comparative Analysis
According to the available literature, the anti-stain algorithms of IDM mainly include literature [19], [20], [21]. For comparison, we compare the algorithms in three works of literature with the algorithms proposed. The experimental results are shown in Table III. The first column in Table III is the six pollution situations mentioned in Section III.
It can be seen that literature works [19], [20] only have a certain effect on tiny stains. This is because the literature works [19], [20] focus on improving the measurement accuracy, and the anti-stain effect is limited. Literature [21] has a certain effect on small stains, but is not effective for larger stains.
Therefore, it can be seen that the anti-stain method proposed in this article is effective.

VI. CONCLUSION
IDM technology based on image recognition algorithms represents an emerging displacement measurement method.
Based on the applied digital image processing algorithm, the IDM method has higher robustness and fault tolerance. In studies related to IDM, the development of processing methods should pursue higher measurement resolution and accuracy, and the reliability of this strategy should be verified in practical applications. Currently, increasing attention is being paid to the anti-stain ability of displacement measuring equipment. Research regarding anti-stain algorithms has therefore become a key component for improving the performance of displacement measurements.
In this report, a stain-detection algorithm and an anti-stain algorithm based on dual linear image sensors were proposed. The main principles of IDM technology were described, and various image recognition algorithms were considered, leading to the proposal of four methods by which to judge whether stains are present (i.e., criteria a-d). Then, based on the stain-detection algorithm, an effective anti-stain algorithm was developed, and both algorithms were verified by experiments. The experimental results indicated that the proposed detection algorithm could accurately detect large, medium, small, opaque, and translucent stains; furthermore, the anti-stain algorithm could ensure an error-free displacement measurement output when stains were present. This work provides a theoretical basis for research and development of highlyreliable displacement measurement technologies.