Building a Real-Time Defect Detector with Line-Scan + ML (Reusable Playbook)

TL;DR

  • Use line-scan for moving/continuous stuff: film, foil, paper, textiles, wire, pipes, board edges, etc.
  • Decide early: field of view (FOV), smallest defect, speed, working distance (WD).
  • Lens: pick focal length so FOV at your WD matches product width; keep ≥3–5 px on the smallest defect.
  • Encoder on the motion path → square pixels and mm-accurate measurements.
  • Pipeline: camera → acquire → tile → preprocess → CV/ML → PLC/UI/logs. Start around f/5.6 (or f/8 if you need more depth of field).


When line-scan makes sense

  • Continuous webs (paper, plastic film, foil, textiles, glass, sheet metal).
  • Long parts (cables, rods, extrusions, tubes).
  • Narrow critical zones (seams, edges, labels).
    Why line-scan: uniform lighting across width, effectively unlimited image length, high resolution along motion.

Optics: the 60-second recipe

FOV ↔ focal length

\text{FOV} \approx \frac{\text{Sensor Width} \times \text{WD}}{\text{Focal Length}}
\quad\Rightarrow\quad
\text{Focal Length} \approx \frac{\text{Sensor Width} \times \text{WD}}{\text{FOV}}
  • Ensure the lens image circle ≥ sensor (e.g., 1.1″ lens for a 1.1″ sensor).
  • Start aperture at f/5.6; go f/8 if the surface isn’t perfectly flat and your light is strong; open to f/4 if exposure is tight.

Resolution target: smallest defect should span ≥3–5 px in both axes.


Motion sync (why the encoder matters)

A rotary incremental encoder on a driven roller (or a measuring wheel on the web) outputs pulses as the product moves. Configure the camera so 1 pulse (or every N pulses) = 1 line.

  • Keeps mm/px constant (no stretch/squash if speed drifts).
  • Gives true distances in mm for defect size/position.
  • Stabilizes ML (consistent aspect ratio).

Back-of-napkin sizing: if your optics give mm/line = 0.15, then lines/mm ≈ 6.67. Pick encoder pulses/mm ≈ 6.67 (use a programmable PPR or a divider in camera).


Lighting (pick by defect type)

  • Transmissive backlight: holes/tears/missing material (silhouette).
  • Diffuse top light: stains/print/color issues.
  • Grazing/dark-field: scratches/raised fibers/embossing.
  • Add polarizers to cut glare. Always perform dark/flat-field correction.

Throughput sanity check

  • Lines/s (square pixels): LPS = belt_speed_mm_s / mm_per_line
  • Data rate (8-bit): MB/s = pixels_per_line × 1 × LPS / 1e6
    1–5 GigE is plenty for most single-camera web lines; jump to 10 GigE/CXP if you push very high speeds or bit depths.

System architecture (Mermaid)

flowchart TD
  A["Moving product/web"] --> B["Backlight / Top light"]
  B --> C["Line-scan camera"]
  D["Rotary encoder on roller"] -->|"A/B pulses"| C
  C --> E["NIC / Frame grabber"]
  E --> F["Acquisition service"]
  F --> G["Tile builder\n(256–512 lines + overlap)"]
  G --> H["Preprocess\n(flat-field, denoise, contrast)"]
  H --> I["Detection"]
  I --> J["Classical CV"]
  I --> K["ML model (ONNX/TensorRT)"]
  J --> L["Decision"]
  K --> L
  L --> M["PLC / Marker / Alarms"]
  L --> N["UI dashboard"]
  L --> O["Logs / Database"]

Software pipeline (that holds up in production)

  1. Acquire encoder-triggered lines (camera SDK; fixed IP; jumbo frames if Ethernet).
  2. Tile: stack 256–512 lines with small overlap.
  3. Preprocess: dark/flat-field, optional denoise/CLAHE.
  4. Detect:
    • Classical CV for structure/continuity/holes.
    • ML (YOLO-style or small U-Net) for complex defects; export to ONNX → TensorRT.
  5. Act: I/O to PLC/marker, save crops, stream overlays to a lightweight UI, log metrics.

Commissioning checklist

  • Set WD, square/level the camera, tension/stabilize the product path.
  • Focus at object plane; lock focus & iris.
  • Wire encoder (prefer RS-422 differential); enable termination; set camera TriggerSource=Encoder.
  • Do dark & flat-field calibration and save the gain map.
  • Verify mm/px with a ruler pass; tweak encoder divider/scale.
  • Seed known defects; tune thresholds and alert logic.
  • NIC: fixed IPs, MTU 9000, power-saving off.

Good defaults

  • Aperture: start f/5.6 (→ f/8 if you need more DOF).
  • Motion blur limit: keep motion during exposure ≤ 0.3 × mm/line.
  • Pixels on smallest defect: ≥3–5 px.
  • Encoder: incremental quadrature, programmable PPR if you can.

Typical single-lane BOM (sketch)

  • Line-scan camera (4K–8K) + C-mount lens matched to FOV/WD (image circle ≥ sensor).
  • Lighting (backlight/top) + driver + diffuser + optional polarizers.
  • Incremental encoder + mount/wheel + shielded cable.
  • Industrial PC (8-core CPU, 16–32 GB RAM, NVMe) + GPU if ML.
  • Matching NIC (1G/5G/10G) or CXP grabber; shielded cabling.
  • 24 V I/O to PLC/markers; UPS; frame/mounts/enclosure.

Get in Touch with us

Chat with Us on LINE

iiitum1984

Speak to Us or Whatsapp

(+66) 83001 0222

Related Posts

Our Products