image thresholding refactored