# DataMatrix Code Recognition Method Based on Coarse Positioning of Images
## 4 Binarization Method Based on Grayscale Change Trend
### Locally Weighted Linear Regression  
Locally weighted regression is a non-parametric regression method [51]. In traditional linear regression methods, it is often necessary to assume the distribution family of the data source and select an appropriate model for parameter estimation. However, in practical applications, the distribution of the data is unknown and cannot fully satisfy a single specified distribution as a whole. Due to the complexity of the overall data distribution, local statistics can be used to simplify it. Locally weighted regression can fit a complex global model with a simple local model assumption [52]. The basic idea of locally weighted regression is to assign different weights to training samples based on the distance between point \(x\) to be predicted and the training samples, so that training samples closer to point \(x\) have larger weights, thereby learning local information about the data differently. In  
Figure 11: A diagram of the grayscale change trend of the 7 th grid in the 8 th row of the 1 st subgraph of Figure 10 after adding abnormal points and repairing them through abnormal point detection. local regression methods, the most commonly used local model is still linear. Local ridge regression adds a regularization term based on local linear regression, making the model more robust to noise, but it does not help improve reconstruction accuracy [53]. Therefore, this paper proposes to use a locally weighted linear regression algorithm to predict the gray value of the grid based on this gray change trend and binarize it to convert the DM code image into a data matrix. For dataset \(S=\{(x_{i},y_{i}),x_{i}\in\mathbb{R},y_{i}\in\mathbb{R},i=1,2,\cdots,N\}\), the general form of the locally weighted model can be represented by Equation (11) as a weighted minimization problem [51]:  
\[min{\sum_{i=1}^{N}L(f(x_{i},\beta),y_{i})K_{h}(d(x_{i},x))}, \tag{11}\]  
where \(x_{i}\) and \(y_{i}\) are the number of segments in the training set and their corresponding unit pixel gray values. \(x\) is the test sample to be predicted, and \(\beta\) is the local model parameter to be solved by the minimization problem. \(f(x,\beta)\) is the locally weighted prediction result of \(x\), \(d(x_{i},x)\) is the distance between data point \(x\) and test point \(x_{i}\), and the Euclidean distance is used in this paper. \(L(\cdot)\) is the loss function and \(K_{h}(\cdot)\) is the weighting or kernel function. In regression problems, squared loss functions or absolute value loss functions are commonly used. In practical problems, for ease of solution, squared loss functions are generally chosen. That is, Equation (11) can be specifically represented by Equation (12):  
\[min{\sum_{i=1}^{N}(f(x_{i},\beta)-y_{i})^{2}K_{h}(d(x_{i},x))}. \tag{12}\]  
In this paper, for the discrete dataset S, the Wang-Ryzin kernel function is used to calculate the weights. The kernel function \(K_{h}(d(x_{i},x))\) is denoted as \(\omega_{i}(x)\), and the expression of the Wang-Ryzin kernel function [54] is shown in Equation (13):  
\[\omega_{i}(x)=\left\{ \begin{aligned} 1-h,& x=x_{i}\\ \frac{1-h}{2}h^{|x_{i}-x|},& x\
eq x_{i}\end{aligned} ,\right. \tag{13}\]  
where \(h\) represents the bandwidth, which controls the speed of weight decay. Choosing an appropriate bandwidth is crucial for the performance of the kernel function. In this paper, the optimal bandwidth parameter is selected through leave-one-out cross-validation. The calculation formula of the leave-one-out cross-validation function [55] is shown in Equation (14):  
\[CV(h)=\frac{1}{n}{\sum_{i=1}^{N}(y_{i}-\hat{g}_{-i}(x_{i}))^{2}}, \tag{14}\]  
where \(\hat{g}_{-i}(x_{i})\) is the estimated value of \(g(x_{i})\) obtained based on the leave-one-out method.