# DataMatrix Code Recognition Method Based on Coarse Positioning of Images
## 4 Binarization Method Based on Grayscale Change Trend
### Locally Weighted Linear Regression
Considering that the local model in this paper is a linear model, \(f(x,\beta)\) can be represented by Equation (15):  
\[f(x,\beta)=\hat{g}^{T}x. \tag{15}\]  
Substituting Equations (15) and (13) into Equation (12), the weighted minimization problem is represented by Equation (16) [56]:  
\[min{\sum_{i=1}^{N}\omega_{i}(x)\Big{(}\beta^{T}x-y_{i}\Big{)}^{2}}. \tag{16}\]  
Equation (16) is rearranged into matrix form as shown in Equation (17):  
\[min(X\beta-Y)^{T}W(X\beta-Y), \tag{17}\]where X represents the matrix of input training data and Y is the estimated output vector. W is the diagonal matrix of weights, specifically shown in Equation (18) [57]:  
\[W= \begin{bmatrix}\omega_{1(x)}&0&\cdots&0\\ 0&\omega_{2}(x)&\cdots&0\\ \vdots&\vdots&\ddots&\vdots\\ 0&0&\cdots&\omega_{N}(x)\end{bmatrix} . \tag{18}\]  
Taking the partial derivative of the objective function in Equation (17) with respect to parameter \(\beta\) and setting the partial derivative to zero, the solution to the corresponding weighted least squares problem is as follows:  
\[\frac{\partial(X\beta-Y)^{T}W(X\beta-Y)}{\partial\beta}=2X^{T}WX\beta-2X^{T} WY=0, \tag{19}\]  
\[\beta=\left(X^{T}WX\right)^{-1}X^{T}WY. \tag{20}\]  
Therefore, for test sample x, its locally weighted linear regression prediction result is as follows:  
\[\hat{y}=Y^{T}WX\Big{(}X^{T}WX\Big{)}^{-1}x \tag{21}\]  
Taking Figure 11 as an example, after detecting and correcting outliers based on the gray change trend, the predicted gray values using the locally weighted linear regression algorithm are shown in Figure 12.  
After obtaining the predicted gray values, we set the threshold to T = 128 and binarize the grid to convert the DM code image into a data matrix, as depicted in Figure 13. The pseudocode of predicting gray values and converting the DM code into a data matrix is shown in Algorithm 2, and its computational complexity is shown in Table 2, where \(m\) is the number of grayscale trend lists and \(n\) is the length of each grayscale trend list. Figure 12: Grayscale value predicted by locally weighted linear regression in Figure 11.  
```
1:A list of the grayscale distribution trends GVLA from Algorithm 1. 2:A decoded data matrix DM. 3:Initialize an empty list
4:For each gray_trend_list in GVLA:
5:Create an array \(x\) of indices from 0 to len(gray_trend_list) - 1
6:Convert gray_trend_list to a numpy array \(y\)
7:Apply Local Outlier Factor (LOF) to detect outliers in \(y\)
8:Identify outlier_indices where LOF = \(-1\)
9:For each outlier_idx in outlier_indices:
10:If outlier_idx is at start or end, apply smoothing based on neighbors
11:End For
12:Fit Kernel Regression model on smoothed \(y\) with \(x\)
13:Calculate predicted gray value for next point
14:If predicted gray value > 128:
15:Set predicted value to 255 (black)
16:Else:
17:Set predicted value to 0 (white)
18:Append predicted value to list
19:End For# Post-processing after obtaining the predicted gray values
20:Reshape list into a 2D array
21:Modify the first row to have alternating 0 and 255 values
22:Modify the last column to have alternating 255 and 0 values
23:Set the first column and the last row to 0
24:Return DM
```  
**Algorithm 2**Grayscale prediction and output of data matrix