Adding adaptive thresholding