## 3 Materials and Methods
### Software Features and Algorithm
#### 3.1.6 Position and Identity  
In step 1.6 of the algorithm, the animal contours (the pixels contained in the animal body) are identified. The contours are identified without establishing hierarchies while retaining only the extreme points of the contour line segments. The contour measurements,such as the area, length, and the ratio between the area and length, are calculated to restrict the contours that are identified based on the user's inputs.  
When the number of contours identified is smaller than the number of individuals specified by the user, the nonhierarchical clustering k-means algorithm is applied to separate merged individuals. In this unsupervised machine learning algorithm, the number of groups (\(k\)) in which the set of pixels will be grouped is equal to the number of individuals specified by the user. The initial \(k\) centroids are randomly defined among the set of data points. Then, the next set of centroids are chosen according to the probability of spreading between the centers [32]. The contour points are compared with each centroid and are allocated to the group where the Euclidean distance is minimal. Considering the inputs for the algorithm \(X=\{x_{1},\ldots,x_{n}\}\) of \(n\) data points, this algorithm runs interactively to find a set \(C=\{c_{1},\ldots,c_{k}\}\) that minimizes the function \(\varphi_{x}(C)\) as follows:  
\[\varphi_{x}(C)=\sum_{x\in X}d(x,C)^{2}, \tag{9}\]  
where \(d(x,C)^{2}\) is the distance from \(x\) to the closest center in \(C\). To choose centroids in the k-means algorithm, the first set of centers \(C_{0}\) are randomly selected from the dataset. Then, this step is repeated for \(2\leqslant i\leqslant k\): \(c_{i}\) is chosen to be equal to a data point \(x_{n}\) according to the probability [32]:  
\[\frac{d(x_{0},C)^{2}}{\varphi_{x}(C)}. \tag{10}\]  
A combinatorial optimization algorithm [33] is applied to maintain the identity of individuals, which provides the optimal identity assignment among the centroids of animal contours. This is based on the Euclidean distance between the set of centroids of the objects in the \(\textit{frame}_{i+1}=\{a_{1},a_{2},\ldots,a_{n}\}\) and the set of centroids in the \(\textit{frame}_{i}=\{b_{1},b_{2},\ldots,b_{n}\}\). Considering that each \(a_{n}\) is assigned to only one \(b_{n}\), the goal is to minimize the total cost of assignments about the distance matrix (\(D\)) between each \(a_{n}\) and \(b_{n}\):  
\[D=\left[\begin{array}{c}d_{1,1}d_{1,2}\cdots d_{1,n}\\ d_{2,1}d_{2,2}\cdots d_{2,n}\\ \vdots\vdots\vdots\\ d_{n,1}d_{n,2}\cdots d_{n,n}\end{array}\right]. \tag{11}\]  
Figure 2: Diagram depicting the operations involved in instance segmentation used in Ethoflow for animal behavior assessments in heterogeneous environments. RPN: region proposal network; ROI: box surrounding the object of interest.  
The mathematical model [33] for the assignments is given as \(\mathit{Minimum}:\sum\limits_{x=1}^{n}\sum\limits_{j=1}^{n}d_{ij}\), where \(d_{ij}\) is the cost (Euclidean distance) from centroid \(a_{n}\) to centroid \(b_{n}\). There are \(n!\) ways to assign \(a_{n}\) to \(b_{n}\) and achieve the optimal assignment, interactively, with the following steps:  
1. The minimum of each row is subtracted from the entire row.
2. The minimum of each column is subtracted from the entire column.
3. All zeros in the matrix are crossed with the minimum possible lines. _If_ crossing lines = \(n\), then the optimal assignment is found. _Else:_ To determine the smallest entry not crossed by any line, Subtract this entry from each uncrossed row and add it to each crossed column. Proceed to step 3.