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)
- Acquire encoder-triggered lines (camera SDK; fixed IP; jumbo frames if Ethernet).
- Tile: stack 256–512 lines with small overlap.
- Preprocess: dark/flat-field, optional denoise/CLAHE.
- Detect:
- Classical CV for structure/continuity/holes.
- ML (YOLO-style or small U-Net) for complex defects; export to ONNX → TensorRT.
- 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
Related Posts
- From Zero to OCPP: Launching a White-Label EV Charging Platform
- How to Build an EV Charging Network Using OCPP Architecture, Technology Stack, and Cost Breakdown
- Wazuh 解码器与规则:缺失的思维模型
- Wazuh Decoders & Rules: The Missing Mental Model
- 为制造工厂构建实时OEE追踪系统
- Building a Real-Time OEE Tracking System for Manufacturing Plants
- The $1M Enterprise Software Myth: How Open‑Source + AI Are Replacing Expensive Corporate Platforms
- 电商数据缓存实战:如何避免展示过期价格与库存
- How to Cache Ecommerce Data Without Serving Stale Prices or Stock
- AI驱动的遗留系统现代化:将机器智能集成到ERP、SCADA和本地化部署系统中
- AI-Driven Legacy Modernization: Integrating Machine Intelligence into ERP, SCADA, and On-Premise Systems
- The Price of Intelligence: What AI Really Costs
- 为什么你的 RAG 应用在生产环境中会失败(以及如何修复)
- Why Your RAG App Fails in Production (And How to Fix It)
- AI 时代的 AI-Assisted Programming:从《The Elements of Style》看如何写出更高质量的代码
- AI-Assisted Programming in the Age of AI: What *The Elements of Style* Teaches About Writing Better Code with Copilots
- AI取代人类的迷思:为什么2026年的企业仍然需要工程师与真正的软件系统
- The AI Replacement Myth: Why Enterprises Still Need Human Engineers and Real Software in 2026
- NSM vs AV vs IPS vs IDS vs EDR:你的企业安全体系还缺少什么?
- NSM vs AV vs IPS vs IDS vs EDR: What Your Security Architecture Is Probably Missing













