One of the thresholding method out there is Otsu’s thresholding. What’s interesting about this method, is that, it assumes there’s two probability distribution from the image’s histogram, and these distributions represents the background and foreground of the image.
This method tries to find weighted variances for background and foreground classes, and then try to find the minimum the sum between those two weighted variances, or to find the minimum intra class variance. And since total variance = intra class variance + inter class variance, it also means that we could find the same threshold by finding the maximum variance between classes.
I’ve also created a sample python code that implements Otsu’s thresholding algorithm over here: https://github.com/subokita/Sandbox/blob/master/otsu.py. The sample code shows how the original algorithm (without any optimization) that searches for the minimum inter-class variance works.