3. Image preprocessing and augmentation

Deep learning models for visual inspection are only as good as the pixels they receive. A camera may deliver a perfectly focused image, yet the network still fails because illumination drifts, sensor noise varies with exposure time, or the part sits at a slightly different angle than in training. Preprocessing and augmentation are the two levers that close this gap: preprocessing makes each image consistent at inference time, while augmentation makes the training set diverse enough to survive real-world variation. This chapter covers normalization, denoising, geometric transforms, and domain-specific augmentation strategies that are actually used on production inspection lines.

Normalization: Fixing the Input Distribution

Normalization rescales pixel values so that the network sees a stable input distribution. For a grayscale inspection image with pixel intensities in [0, 255], the two common forms are min–max scaling and standardization:

# Min–max scaling to [0, 1]
x_norm = (x - x.min()) / (x.max() - x.min())

# Standardization (per-channel mean/std)
x_norm = (x - mean) / std
# ImageNet-style defaults: mean=[0.485,0.456,0.406], std=[0.229,0.224,0.225]

For inspection tasks, per-dataset statistics usually outperform ImageNet defaults because industrial images have very different contrast profiles. Compute the mean and standard deviation over your own training set and freeze them for inference. A subtle but important point: if you normalize each image independently by its own min/max, a dark image and a bright image of the same part will map to the same range, which can hide genuine brightness defects. For defect detection, prefer fixed dataset-level statistics so that abnormal brightness remains visible to the model.

Always apply the same normalization at training and inference. A mismatch here is one of the most common causes of a model that performs well in validation but fails on the line.

Denoising: Suppressing Sensor and Environmental Noise

Industrial cameras introduce noise from sensor readout, heat, and short exposure times. Three practical denoisers dominate:

  • Gaussian blur — fast, removes high-frequency noise, but also blurs fine defects such as hairline cracks. Use small kernels (3×3 or 5×5) and only when defect scale is much larger than the kernel.
  • Median filter — excellent for salt-and-pepper noise and preserves edges better than Gaussian. A 3×3 median is a safe default for most inspection pipelines.
  • Bilateral / non-local means — edge-preserving but computationally heavier. Suitable for offline training-set preparation or high-value inspections where defect edges matter.

A useful rule: denoise before augmentation, not after. Augmentation should simulate realistic variation, and applying noise then denoising can create artifacts the model will never see at inference.

Geometric Transforms

Geometric augmentation teaches the model that a defect is defined by its appearance, not its absolute position. The standard set includes:

TransformTypical rangeInspection use case
Rotation±15° to ±180°Parts fed at arbitrary orientation
Translation±10% of image sizeFixture placement tolerance
Scale0.9×–1.1×Standoff distance variation
Horizontal/Vertical flip—Only if the defect is orientation-invariant
Shear / Perspective±5°Camera tilt or lens distortion

Two cautions are critical in inspection. First, flips and large rotations can create physically impossible defects — a scratch that always runs along the extrusion direction should not appear vertical. Second, bounding boxes and masks must be transformed with the image. If you rotate an image but not its annotation, you are training the model on wrong labels. Libraries such as Albumentations handle this automatically, but custom pipelines frequently get it wrong.

Domain-Specific Augmentation for Inspection

Generic augmentation (flip, rotate, crop) is necessary but not sufficient. Inspection models face failure modes that natural-image augmentation does not address:

  • Illumination variation — multiply pixel values by a random factor (0.8–1.2) or add a smooth gradient to simulate uneven ring lighting.
  • Exposure and gain drift — apply gamma correction with γ ∈ [0.8, 1.2] to mimic camera auto-exposure changes.
  • Blur and defocus — apply Gaussian blur with σ ∈ [0.5, 2.0] to simulate focus drift or vibration.
  • Sensor noise — add Gaussian noise (σ up to 5% of dynamic range) or Poisson noise to match low-light conditions.
  • Defect synthesis — paste synthetic scratches, stains, or missing-material patches onto good parts. This is the most powerful technique when defect samples are scarce, but the synthetic defects must match the real defect statistics (width, contrast, edge profile).
Do not augment the test set. Augmentation is a training-time regularizer. Evaluating on augmented images inflates metrics and hides real-world failure modes.

Putting It Together: A Reproducible Pipeline

Consider a metal surface inspection task with 2,000 good images and 80 scratch images. A practical pipeline:

  1. Convert to grayscale, apply 3×3 median denoising.
  2. Normalize using dataset mean = 0.52, std = 0.18 (computed from the training split only).
  3. Training augmentation: random rotation ±10°, translation ±5%, brightness ×[0.85, 1.15], Gaussian blur σ ∈ [0, 1.5], and synthetic scratch overlay on 30% of good images.
  4. Validation: only normalization and denoising — no random transforms.

After training, verify by running the model on a held-out set of images captured under a different lighting setup. If recall drops sharply, the augmentation range was too narrow; widen illumination and blur ranges and retrain. If precision drops, synthetic defects may be unrealistic — inspect a few overlaid samples visually and compare their edge profiles against real scratches.

Inspection Preprocessing & Augmentation Pipeline Raw Camera Image noise, drift Denoise median 3×3 Gaussian σ≤1.5 Normalize mean=0.52 std=0.18 Augmentation (train only) rotate ±10° · translate ±5% · brightness ×[0.85,1.15] blur σ∈[0,1.5] · synthetic scratch overlay 30% Training Set augmented + normalized 2,000 good + 80 scratch Validation / Test Set denoise + normalize only no random transforms CNN / Detector train → validate → deploy widen ranges if recall drops

The diagram above summarizes the flow: raw images pass through denoising and normalization, then split into an augmented training branch and a clean validation branch. The dashed feedback loop is the practical tuning step — if validation recall drops under a new lighting condition, widen the illumination and blur augmentation ranges and retrain. This closed loop, rather than any single transform, is what makes an inspection model robust on the factory floor.