Thresholding using Otsu's method (which chooses the threshold to minimize the intraclass variance of the black and white pixels!).
procedure OtsuThresholding(var Image: TImageData; Threshold: PInteger = nil);
ImagingBinary (at line 57)
If Threshold is nil Image is automatically converted to binary using computed threshold level. Otherwise computed threshold is stored in Threshold and Image is not modified (if you're just interesting in global threshold level).