## 3 Methods
### Background Subtraction  
Background subtraction is a critical step in video processing, especially when tracking objects in dynamic environments. In our methodology, we employ the Mixture of Gaussians 2 (MOG2) algorithm, a widely used approach for robust background subtraction [13]. MOG2 operates on the principle of modeling each pixel as a mixture of Gaussian distributions, which allows for the accommodation of variations in the scene, such as moving backgrounds or changes in illumination.  
The MOG2 algorithm updates the background model by employing an online approximation to update the Gaussian mixtures. The probability of observing the current pixel value is given by Equation (1):  
\[P(X_{t})=\sum_{i=1}^{K}\omega_{i,t}\cdot\eta(X_{t};\mu_{i,t},\sum_{i,t}) \tag{1}\]  
where \(X_{t}\) is the pixel value at time \(t\), \(K\) is the number of Gaussian distributions in the mixture, \(\omega_{i,t}\) is the weight of the \(i\)th Gaussian in the mixture at time \(t\), and \(\eta\) is the Gaussian probability density function with mean \(\mu_{i,t}\) and covariance \(\sum_{i,t}\).  
The algorithm compares the pixel value with the distributions in the model to identify whether a pixel belongs to the foreground or background. If the pixel value does not match the background model well, the algorithm classifies it as part of the foreground. In our case, we are interested in detecting moving drosophilas, which would be considered part of the foreground.  
The application of the MOG2 algorithm for background subtraction in our study is driven by specific challenges encountered when tracking drosophila swarms. In the raw video footage, numerous background elements can interfere with accurate tracking, such as the operator's hand during the introduction of drosophilas, the container used for housing them, scratches on the container, and the container's edges. These elements need to be effectively separated from the drosophilas, which are our primary focus.  
The MOG2 algorithm is adept at distinguishing moving foreground objects from static or slow-changing backgrounds. By applying this algorithm, we efficiently isolate the drosophilas from irrelevant background elements, which is shown in Figure 2. This separation is crucial, especially considering the small size of individual drosophilas and the potential for numerous background distractions in the video.  
In addition, our primary interest lies in tracking the movement of drosophilas. Consequently, drosophilas that remain relatively stationary are considered to be at rest or inactive and are not the focus of our tracking efforts. The MOG2 algorithm aids in this regard by identifying and focusing on moving objects in the video frames. Stationary drosophilas are effectively categorized as part of the background, temporarily suspending their tracking.  
By removing background noise and distractions, the MOG2 algorithm significantly reduces the likelihood of false detections in the next detection process. This step is particularly important given the small size of the drosophilas and the potential for misidentifying background elements as targets. Additionally, by concentrating on the more active drosophilas, our method aligns with the biological interest in understanding their movement patterns and behaviors.  
This approach is particularly effective for our application, as it allows for the detection of drosophilas even in the presence of dynamic changes in the background. The MOG2 algorithm's ability to adapt to changes in lighting and shadowing ensures that the detected foreground consistently represents the drosophilas across different frames.