Image preprocessing for defect detection