Automatic Thresholding For Defect Detection