There are a couple of assumptions that can change the end result. One is that the histogram has one tail longer than the other and that the threshold has to be on the long tail side. The other is the definition of the end point of a histogram tail. In the code, this is set to 10000 (i.e. tails end when there are less than 10000 pixels in the corresponding histogram bin). Depending on the size of the image, this might not be an optimum choice.