Image segmentation for defect detection