# Extraction and Detection of Surface Defects in Particleboards by Tracking Moving Targets
## 2 Algorithm Theory  
### The KCF Target Tracking Algorithm  
The kernel correlation filter has been widely used in the field of moving target detection and tracking since it was first proposed in 2014 [20]. KCF is a discriminative tracking method that mainly uses the given samples to train a discriminative classifier, which can distinguish between targets and backgrounds. Circulant matrices are used to translate and scale the samples, and the discrete Fourier transform (DFT) is used to accelerate the algorithm. Considering an \(n\times 1\) vector and \(x\) representing a line of the target area image as the base sample, in order to get more samples to train the classifier, we can use a cyclic shift operator \(P\) to perform one-dimensional translations of \(x\). \(P\) is a \(n\times n\) matrix. \[P=\left[ \begin{array}{cccc}0&0&0&\dots&1\\ 1&0&0&\dots&0\\ 0&1&0&\dots&0\\ \vdots&\vdots&\ddots&\ddots&\vdots\\ 0&0&\dots&1&0\end{array} \right] \tag{1}\]  
Due to its cyclic property, all samples obtained after transformation can be expressed by the following equation:  
\[\{\,P^{n}x|u=0,\dots,n-1\} \tag{2}\]  
Equation (2) can be written in matrix form as follows:  
\[X=C(x)=\left[ \begin{array}{ccccc}x_{1}&x_{2}&x_{3}&\dots&x_{n}\\ x_{n}&x_{1}&x_{2}&\dots&x_{n-1}\\ x_{n-1}&x_{n}&x_{1}&\dots&x_{n-2}\\ \vdots&\vdots&\vdots&\ddots&\vdots\\ x_{2}&x_{3}&x_{4}&\dots&x_{1}\end{array} \right] \tag{3}\]  
Equation (3) represents the form of circulant matrices [21]. The elements in X depend on vector x, while DFT makes the matrices diagonal [22]. Equation (3) can be expressed as follows:  
\[X=F\;diag(\hat{x})F^{H} \tag{4}\]  
where \(F\) is the DFT constant matrix, and \(\hat{x}\) denotes the DFT of \(x\), i.e., \(\hat{x}=F(x)\). Circulant matrices combined with DFT can generate a large number of samples for classifier training in a stable and effective way, thus ensuring the accuracy of tracking results. The \"kernel trick\" can be used to turn a linear mapping problem to a nonlinear kernel space and turn the calculation of low-dimensional space mapping to high-dimensional kernel space. The inseparable problem in low-dimensional space will become linearly separable in high-dimensional space, and we can realize an efficient training of the classifier [23]. We can define the \(n\times n\) kernel matrix as follows:  
\[K^{z}=C(k^{xz}) \tag{5}\]  
where \(k^{xz}\) is the kernel correlation of the base sample \(x\) and base patch \(z\). According to the regression function, \(f(z)\) can be given as follows:  
\[f(z)=w^{T}z=\sum_{i=1}^{n}\alpha_{i}k(z,x_{i}) \tag{6}\]where \(\alpha\) is the regression coefficient; the value range of \(i\) is \([1,n]\); and \(k(z,x_{i})\) is the element of a \(n\times n\) matrix \(K\), which denotes the dot products between \(x\) and \(z\). We can compute the regression function for all candidate patches with the following equation:  
\[f(z)={(K^{z})}^{T}\alpha \tag{7}\]  
Diagonalizing Equation (7), we obtain the following:  
\[\hat{f}(z)=\hat{k}^{xz}\odot\hat{\alpha} \tag{8}\]  
It is obvious that \(f(z)\) is a linear combination of the neighboring kernel values from \(k^{xz}\), weighted by the learned coefficients \(\alpha\). As this is a filtering process, it can be better expressed in the Fourier domain. When the kernel function training is complete, the new sample will map to the kernel space directly. Using the trained function to calculate the value for all positions, the location of the target can then be quickly detected. Although KCF can perform well in both tracking effect and tracking speed, it also has a limitation in that it is not free to change the size of the target tracking boxes; this means tracking can be easily disturbed when the target is covered. However, for the detection of surface defects in particleboards, it is almost impossible that the defect targets will be deformed or covered, thus the imperfection of the algorithm will not have a negative impact the final tracking effect.