Image segmentation threshold algorithm