# A Background Subtraction Algorithm in Complex Environments Based on Category Entropy Analysis
## 3 Materials and Methods
The color information of each pixel in the third and later images was clustered with a color distance classifier as explained below. First, we used Equation (2) to calculate the Euclidean distance \(D_{(n)}^{x,y}\) between the input pixel and all the color categories at its position, where \(\Delta R_{(n)}^{x,y}\), \(\Delta G_{(n)}^{x,y}\), and \(\Delta B_{(n)}^{x,y}\) represent the color difference between the input pixel and the red, green, and blue color values in the \(n\)th color category at the location, respectively. \[D_{(n)}^{x,y}=\sqrt{\Delta R_{(n)}^{x,y}+\Delta G_{(n)}^{x,y}+\Delta B_{(n)}^{ x,y}}, \tag{2}\]  
\[\Delta R_{(n)}^{x,y}=I_{R}^{x,y}-R_{(n)}^{x,y},\]  
\[\Delta G_{(n)}^{x,y}=I_{G}^{x,y}-G_{(n)}^{x,y},\]  
\[\Delta B_{(n)}^{x,y}=I_{B}^{x,y}-B_{(n)}^{x,y}.\]Second, we determined the color category \(S_{\min}^{x,y}\), which has the smallest color difference among input pixels, and recorded its index \(N_{\min}^{x,y}\) and distance \(D_{\min}^{x,y}\) as shown by Equations (3)-(5). Third, we used a color classifier to define a color difference threshold value \(\mathrm{TH}_{D}\) to be compared with \(D_{min}^{x,y}\). If \(D_{min}^{x,y}>\mathrm{TH}_{D}\), it means that the input pixel does not belong to any existing color category. We added a new color category with the color information of the pixel and determined its position as a foreground. If \(D_{min}^{x,y}\leq\mathrm{TH}_{D}\), it means that the input pixel belongs to the \(N_{\min}^{x,y}\)-th color category and we increased the number of samples in this color category according to \(C_{(N_{\min}^{x,y})}^{x,y}=C_{(N_{\min}^{x,y})}^{x,y}+1\). In the meantime, we used Equation (6) to update the color information of this category in a progressive manner. Compared with the method of directly calculating the average value of all samples in the color categories, our method improves the computation speed and saves storage space. \[D_{\min}^{x,y}=\min_{\forall n}D_{(n)}^{x,y}, \tag{3}\]  
\[N_{\min}^{x,y}=\arg\min_{\forall n}D_{(n)}^{x,y}, \tag{4}\]  
Figure 1: Flowchart of the proposed algorithm. \[S_{\min}^{x,y}=\Big{\{}S_{(n^{\prime})}^{x,y}\Big{|}n^{\prime}=N_{\min}^{x,y}\Big{\}}, \tag{5}\]  
\[\text{\emph{If}}\ R_{(n^{\prime})}^{x,y}<I_{R}^{x,y}\text{\emph{then}}\ R_{(n^{ \prime})}^{x,y}=\ R_{(n^{\prime})}^{x,y}+1, \tag{6}\]  
\[\text{\emph{else}}\ R_{(n^{\prime})}^{x,y}=R_{(n^{\prime})}^{x,y}-1,\]  
\[\Delta R_{(n)}^{x,y}=I_{R}^{x,y}-R_{(n)}^{x,y},\]0  
\[\Delta R_{(n)}^{x,y}=I_{R}^{x,y}-R_{(n)}^{x,y},\]1  
\[\Delta R_{(n)}^{x,y}=I_{R}^{x,y}-R_{(n)}^{x,y},\]2  
\[\Delta R_{(n)}^{x,y}=I_{R}^{x,y}-R_{(n)}^{x,y},\]3  
Given that the background color does not change in a short time, when \(D_{min}^{x,y}\leq\text{TH}_{D}\), this position \((x,\ y)\) is represented as a color class \(S_{\min}^{x,y}\) similar to the input pixel \(I^{x,y}\). If the probability of the occurrence of \(S_{\min}^{x,y}\) is higher, it is more likely that input pixel \(I^{x,y}\) is the background. However, the background will be subject to varying degrees of disturbance depending on image quality in a real environment, as shown in Figure 2a. If the degree of color disturbance is greater than the color difference threshold \(\text{TH}_{D}\), then the background colors cannot be classified in the same category. As shown by the color classification results in Figure 2b, the color distribution at the upper right cannot fall into a single-color category. In addition, the color category established by the color distance classifier has a spherical color-distribution range with a radius of \(\text{TH}_{D}\), while the distribution of background colors is irregular depending on the camera quality, as shown in Figure 2a. The increase of \(\text{TH}_{D}\) alone will cause large color errors in the background category. Therefore, a joint category approach is proposed to adapt to the irregular color distribution of a real background.