← Projects

Hazelnut surface-defect segmentation with OpenCV

Otsu, Canny, Top-Hat and morphological pipelines for crack, hole, print and cut defects in MVTec Hazelnut images.

Role
Development team member · two-person project
Organization
Kocaeli University
Period
2025
Stack
Python · OpenCV · NumPy · scikit-image · Matplotlib
A hazelnut with a hole, its Otsu object mask, defect mask and annotated result.

Problem

A natural surface line and a crack can look similar. Our two-person team approached each defect through its visual structure: connected edges for cracks, a dark center for holes, brightness changes for print and contour damage for cuts. We implemented four OpenCV pipelines on MVTec AD Hazelnut images.

The user selects the defect type and the program runs the corresponding pipeline. This version does not infer an unknown defect category automatically.

Masks and detection methods

Otsu thresholding separates the object from the background. Morphological closing/opening and largest-component selection clean the mask. Contour filling and erosion produce an inner ROI to reduce boundary artifacts.

  • Crack: bilateral filtering, Canny edges and morphological closing.
  • Hole: dark-center thresholding combined with Top-Hat output.
  • Print: Gaussian blur, Top-Hat, intensity statistics and component-area filtering.
  • Cut: differences between filled/original masks and morphological gradients.
Cracked hazelnut showing the input, object mask, Canny-based defect mask and annotated result
Cracked hazelnut showing the input, object mask, Canny-based defect mask and annotated result

A defect-mask area above 200 pixels triggers the defective label. Thresholds and kernels live in config.py; segmentation, detection and visualization are separate modules.

Top-Hat and intensity thresholds highlight bright print regions on the surface.
Print: isolating brightness changes Top-Hat and intensity thresholds highlight bright print regions on the surface.

Evaluation and limitations

The report presents qualitative examples for the four defect types, but no independent IoU/Dice or precision/recall benchmark. The good-category branch directly creates an empty mask, so it cannot establish a false-positive rate.

Next steps are a shared detector that does not require a known defect type and evaluation against ground-truth masks across all images. Fixed thresholds also need testing under different lighting and low contrast.

Mask differences and morphological gradients examine damage to the object contour rather than texture alone.
Cut: inspecting object shape Mask differences and morphological gradients examine damage to the object contour rather than texture alone.

Result

The application produces source images, object masks, defect masks and overlays for four defect-specific pipelines. It demonstrates which visual structures classical segmentation and morphology capture through inspectable intermediate outputs.