# Improved Mask R-CNN Combined with Otsu Preprocessing for Rice Panicle Detection and Segmentation
## 2 Dataset and Method
### Method
#### 2.2.3 Bounding Box Adjustment  
In the traditional Mask R-CNN algorithm, the candidate box is represented by a four-dimensional vector (x, y, w, h). By learning the deformation ratio of the real and prediction boxes, the final prediction boxes are obtained by step-by-step fine-tuning. During the training process, the outputs are all a series of probability distributions converging to a Gaussian distribution. The Mask R-CNN defines a multitask loss function consisting of three components, as shown in Equation (5), which is the specific calculation of the classification error and detection error.  
\[\text{L}=\text{L}_{\text{cls}}+\text{L}_{\text{reg}}+\text{L}_{\text{mask}}= \frac{1}{\text{N}_{\text{cls}}}\sum_{\text{i}}\text{L}_{\text{cls}}(\text{p} _{\text{i}},\text{p}_{\text{i}}^{*})+\lambda\frac{1}{\text{N}_{\text{reg}}} \sum_{\text{i}}\text{p}_{\text{i}}^{*}\text{L}_{\text{reg}}(\text{t}_{\text{i }},\text{t}_{\text{i}}^{*})+\text{L}_{\text{mask}} \tag{5}\]where \(\rm L_{cls}\) and \(\rm L_{reg}\) are the classification error and detection error, respectively, and \(\rm L_{mask}\) is the semantic segmentation error. The \(\rm N_{cls}\) and \(\rm N_{reg}\) are the parameters of standardization, \(\rm i\) is the index of anchor in a mini-batch, \(\rm p_{i}\) stands for the predicted value of an object, \(\rm p_{i}\)' stands for the true value of an object. The \(\rm t_{i}\) and \(\rm t_{i}\)' represent the coordinates of the prediction box and the real box, respectively.  
However, the bounding box may not be clear when the target is partially obscured, leading to inaccurate labelling and a blurred bounding box, which directly affects the detection error. To solve the problem of blurred bounding boxes, this paper uses the KL divergence loss function instead of the traditional loss function. The KL loss function still consists of three parts; the classification error and semantic segmentation error remain unchanged and the detection error is \(\rm Lreg\)', which is calculated using Equation (6). The KL divergence, called information gain or information divergence in statistical model inference, measures the difference between two probability distributions [39]. According to the KL divergence theorem, the boundary prediction box and the real box are modeled as Gaussian distributions and Dirac delta functions, respectively, and the KL loss function is the KL distance between the distribution area of the prediction box and that of the real box [36]. Therefore, it is only necessary to calculate the similarity between the Gaussian distribution of the prediction box and the probability distribution of the real box. When the KL divergence of the two distributions is smaller, the probability distributions of the two are closer; that is, the prediction box is closer to the real box.  
\[\rm L_{reg}\text{'}=e^{-a}(\,1\,x_{e}-x_{g}\,|\,-\,1/2)+\alpha/2 \tag{6}\]  
where \(\rm x_{g}\) is the estimated coordinate, \(\rm arg\)\(\rm x_{e}\) is the coordinate of the predicted box, and \(\alpha=\log(\sigma^{2})\), where \(\sigma\) is the standard deviation, which is used to measure the difference between the predicted box and the real box. As \(\sigma\to 0\), the predicted box approaches the real box, and the prediction is more accurate.