# From Classic to Cutting-Edge: A Near-Perfect Global Thresholding Approach with Machine Learning
## 2 Materials and Methods  
### Features  
The following algorithms were directly used in the proposed solution as features in the model that we trained to predict the optimal global threshold. They operate directly on the image histogram, optimizing various metrics, and can be computed in constant time (\(O(1)\)) in relation to the image size. Although some of these algorithms are iterative, they converge rapidly. [PERSON] [23] proposed a popular global thresholding algorithm. For a given threshold k, the pixels are divided into two classes: the 0 class, composed of pixels with intensities lower than or equal to k, and the 1 class, composed of pixels with intensities greater than k. Between-class variance is defined as the weighted sum of the variances of the two classes. [PERSON]'s threshold is defined as the threshold with the largest between-class variance, defined as follows:  
\[\sigma_{01}^{2}=W_{0}(t)W_{1}(t)(\mu_{1}(t)-\mu_{0}(t)), \tag{5}\]  
where \(W_{0}(t)\) is the ratio of pixels with intensities lower than or equal to t; \(W_{1}(t)\) is the ratio of pixels with intensities greater than \(t\); and \(\mu_{0},\ \mu_{1}\) are the mean values of the 0 class and 1 class pixel intensities, respectively. [PERSON] and [PERSON] [23] proposed a computationally efficient solution for minimum error thresholding. They assumed that the two pixel classes follow normal distributions, splitting the probability density function \(p(i)\) into two components, \(p_{0}(i)\) and \(p_{1}(i)\), normally distributed with means \(\mu_{0}\) and \(\mu_{1}\), standard deviations \(\sigma_{0}\) and \(\sigma_{1}\), and a priori probabilities \(P_{0}(i)\) and \(P_{1}(i)\). They choose a threshold that minimizes the following criterion function:  
\[J(t)=1+2(P_{0}(t)\text{log}\sigma_{0}(t)+P_{1}(t)\text{log}\sigma_{1}(t))-2(P _{0}(t)\text{log}\,P_{0}(t)+P_{1}(t)\text{log}\,P_{1}(t)) \tag{6}\]  
The function is easy to compute, and finding its minimum is relatively simple, as the function is smooth. [PERSON] [23] proposed an iterative thresholding algorithm that minimizes the mean squared error between the original and the binarized images. In the first iteration, the average intensity is chosen as the threshold, and in further iterations, the threshold is updated via  
\[t_{i+1}=\frac{\mu_{1}(t_{i})+\mu_{0}(t_{i})}{2}+\frac{\sigma^{2}\text{log}(W_{ 0}(t_{1})/W_{1}(t_{1}))}{\mu_{1}(t_{i})-\mu_{0}(t_{i})}, \tag{7}\]where \(\sigma^{2}\) is the variance of the entire image. The algorithm converges when the final threshold is equal to the previous threshold. [PERSON] et al. (2014) argued that the within-class variance is more useful than the between-class variance as a selection criterion for the optimal threshold. The within-class variance is defined as  
\[\sigma_{0+1}^{2}=W_{0}(t)\sigma_{0}^{2}(t)+W_{1}(t)\sigma_{1}^{2}(t), \tag{8}\]  
where \(\sigma_{0}^{2},\sigma_{1}^{2}\) are the variances of each class. If there is more than one threshold for which the within-class variance is minimal, then they consider the average of those thresholds to be the optimal one. Their experiments show that their method performs better than [PERSON]'s on their synthetic dataset. [PERSON] and [PERSON] (2013) proposed an iterative thresholding algorithm very similar to Lloyd's. In the first iteration, the average intensity is chosen as the threshold, and in further iterations, the threshold is updated via  
\[t_{i+1}=\frac{\mu_{0}(t_{i})+\mu_{1}(t_{i})}{2}. \tag{9}\]  
The algorithm converges when the final threshold is equal to the previous threshold. [PERSON] and [PERSON] (2013) proposed an algorithm based on computing a measure of fuzziness, such as Shannon's entropy, for each possible threshold and choosing the lowest threshold for which the minimum fuzziness is reached.