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
- 叶片病害检测算法如何工作:从相机到决策
- How Leaf Disease Detection Algorithms Work: From Camera to Decision
- Smart Farming Lite:不依赖传感器的实用型数字农业
- Smart Farming Lite: Practical Digital Agriculture Without Sensors
- 为什么定制化MES更适合中国工厂
- Why Custom-Made MES Wins Where Ready-Made Systems Fail
- How to Build a Thailand-Specific Election Simulation
- When AI Replaces Search: How Content Creators Survive (and Win)
- 面向中国市场的再生资源金属价格预测(不投机、重决策)
- How to Predict Metal Prices for Recycling Businesses (Without Becoming a Trader)
- Smart Durian Farming with Minimum Cost (Thailand)
- 谁动了我的奶酪?
- Who Moved My Cheese?
- 面向中国的定制化电商系统设计
- Designing Tailored E-Commerce Systems
- AI 反模式:AI 如何“毁掉”系统
- Anti‑Patterns Where AI Breaks Systems
- 为什么我们不仅仅开发软件——而是让系统真正运转起来
- Why We Don’t Just Build Software — We Make Systems Work
- 实用的 Wazuh 管理员 Prompt Pack













