Adaptive thresholding in image processing