# Log Volume Measurement and Counting Based on Improved Cascade Mask R-CNN and Deep SORT
## 3 Methods
### Tracking Methodology  
In this work, we first utilize Mask R-CNN to detect the log ends in each frame of the input video. The output of Mask R-CNN provides the bounding boxes around the detected log ends, which are then passed to the Deep SORT multi-object tracking algorithm for tracking across consecutive frames.  
Deep SORT is an extension of the SORT (Simple Online and Realtime Tracking) algorithm, designed to handle the tracking of multiple objects by incorporating appearance features into the process. The algorithm improves the accuracy of long-term tracking, especially in cases wherein objects are occluded or leave and re-enter the frame.  
Deep SORT consists of three primary modules:  
Figure 5: Convolutional Block Attention module. It contains a channel attention module and a spatial attention module.  
1. Object Detection: In this initial module, bounding boxes for log ends are extracted from the output of Mask R-CNN. This step is crucial because accurate detection forms the foundation for effective tracking. The bounding boxes identified in each frame serve as inputs for the subsequent tracking module, enabling the algorithm to maintain object identities throughout the video.  
2. Trajectory Prediction and Update: This module is responsible for predicting the future positions of the detected log ends and updating their states. Using a Kalman filter, the algorithm estimates the trajectory of each log end based on its current state, which includes position, size, and velocity. The state vector \(\vec{x}=[c_{x},c_{y},r,h,v_{x},v_{y},v_{r},v_{h}]\) represents the position, size, and velocity of the log end's bounding box. The Kalman filter provides a robust mechanism for anticipating the next state of each log end, allowing for timely updates based on new detections.  
The prediction step is described by the following equations:  
\[\vec{x}=F_{k}\vec{x}k-1+B_{k}\vec{u}\vec{k} \tag{5}\]  
\[P_{k}=F_{k}Pk-1F_{k}^{T}+Q_{k} \tag{6}\]  
where \(F\) is the state transition matrix, \(B\) is the control matrix, \(\vec{u}_{k}\) is the control vector, and \(Q_{k}\) represents process noise.  
3. Data Association: The final module involves matching the updated object states from the Kalman filter with the detections from Mask R-CNN. This step is critical for correctly associating tracked objects across frames, especially when objects overlap or become occluded. Deep SORT utilizes a combination of Mahalanobis distance, which assesses the similarity between predicted and detected bounding boxes, and cosine distance, which evaluates the similarity of the appearance features extracted for each log end.  
The overall matching cost is defined as follows:  
\[c_{(i,j)}=\lambda d^{(1)}(i,j)+(1-\lambda)d^{(2)}(i,j) \tag{7}\]  
Here, \(d^{(1)}(i,j)\) represents the Mahalanobis distance, \(d^{(2)}(i,j)\) denotes the cosine distance between appearance features, and \(\lambda\) is a weighting factor that balances the contributions of both distances. By minimizing this matching cost, the algorithm effectively associates detections with existing trajectories, ensuring continuity in object tracking.  
Finally, the Hungarian algorithm is used to minimize the matching cost and associate the detections with the existing tracks.