DIP+CV · Programming Exercise

EP06_07 — 🟣 Industrial Inspection Pipeline: Registration by Translation and Subtraction

6.14.7 EP06_07 🟣 Industrial Inspection Pipeline: Registration by Translation and Subtraction

On a production line, a fixed camera photographs each part as it passes on the conveyor belt, comparing it to a defect-free reference image. The problem: small vibrations in the conveyor shift the part relative to the reference position at each capture. If image subtraction is applied directly, without correction, the displacement alone already generates enormous differences — false positives that mask the real defects.

This is the most comprehensive exercise in the chapter: you must first register (geometrically align) the captured image using a known displacement \((dx, dy)\), provided by a position sensor on the conveyor, and only then apply subtraction with thresholding, exactly as described in the industrial inspection section.

6.14.7.1 📋 Implementation Guidelines

  1. Dimensions and parameters: Read \(L\), \(C\) (image dimensions), the known integer displacement \(dx, dy\) (which may be negative), and the detection threshold \(T\) (integer).
  2. Images: Read the reference matrix (ref, \(L\times C\), defect-free) and the captured matrix (cap, \(L\times C\), possibly shifted and with defects).
  3. Registration by translation: Construct the aligned image alin by applying the received displacement \((dx,dy)\): \[ \text{alin}(i,j) = \begin{cases} \text{cap}(i+dy,\; j+dx), & \text{if } (i+dy,\ j+dx) \in [0,L)\times[0,C) \\ 0, & \text{otherwise} \end{cases} \]
  4. Border filling: Positions that “leave” the captured image after the displacement are assigned the value 0 (zero-padding — outside the camera’s field of view; note that this exercise uses zero, unlike the border replication of EP06_06).
  5. Absolute difference: Compute, pixel by pixel, \[ \text{diff}(i,j) = |\text{ref}(i,j) - \text{alin}(i,j)| \]
  6. Thresholding: Define \(\text{mask}(i,j) = 1\) if \(\text{diff}(i,j) > T\); otherwise, \(\text{mask}(i,j) = 0\).
  7. Output: In this order — (a) the matrix alin (\(L\times C\)); (b) the defect mask (\(L\times C\)); (c) a final line with the total number of pixels classified as defective.

6.14.7.2 📌 Computational Constraints

  • Zero-padding, not replication: positions outside the bounds of the captured image, after the displacement, are exactly 0 — this is the point that most differentiates this exercise from EP06_06.
  • Strict comparison: \(\text{diff}(i,j) > T\).
  • Sign of \((dx,dy)\): the displacement may be positive or negative; the formula in step 3 must be applied literally, without inverting the signs.
  • All values are integers: there is no rounding at this stage.

6.14.7.3 🧠 Theoretical Foundation

Omitted step Consequence
Skipping geometric registration The entire image border (introduced by the displacement) is marked as “defect” — systematic false positive
Registration with incorrect \((dx,dy)\) Part and reference remain misaligned; subtraction detects shifted contours, not real defects
Threshold \(T\) too low Capture noise (variations of 1–2 gray levels) is mistaken for defects
Threshold \(T\) too high Subtle defects go undetected

Geometric registration and subtraction are complementary steps: the former ensures that both images represent exactly the same scene in the same spatial reference frame; the latter isolates what actually changed between them — ideally, only the defects.

6.14.7.4 📦 Input and Output Specification (VPL)

Input:

  • Line 1: Integer \(L\).
  • Line 2: Integer \(C\).
  • Line 3: Two integers \(dx\) and \(dy\), separated by a space.
  • Line 4: Integer \(T\).
  • Next \(L\) lines: integer elements of the ref matrix.
  • Next \(L\) lines: integer elements of the cap matrix.

Output:

  • \(L\) lines with the alin matrix.
  • \(L\) lines with the defect mask (0/1).
  • Last line: Total de pixels defeituosos: X.

6.14.7.5 📌 Examples

Input Output Remark
3
3
1 0
30
50 50 50
50 50 50
50 50 50
0 50 50
0 50 90
0 50 50
50 50 0
50 90 0
50 50 0
0 0 1
0 1 1
0 0 1
Total de pixels defeituosos: 4
\(dx=1\) shifts the reading one column to the right; the last column of alin has no correspondence
(becomes 0) and is systematically marked; the real defect (90) is also detected.
2
2
0 0
20
10 10
10 10
10 10
10 60
10 10
10 60
0 0
0 1
Total de pixels defeituosos: 1
No displacement (\(dx=dy=0\)): alin is identical to cap; only the real defect (60) is detected.
🎮 Simulator EP06_07: Translation Registration + Subtraction |ref − align(dx,dy)| > T
Adjust the conveyor shift (dx) and threshold T. Watch how the "ghost" edge disappears when dx = 0.
ref
align (registered)
mask
–
Figure 6.27: EP06_07 Simulator: Pipeline Inspection — Registration by Translation and Subtraction
%%writefile EP06_07.py
# Python code
Overwriting EP06_07.py
TestSuite("EP06_07.py").run()
✔️ EP06_07.cases already exists in casos/
📋 5 case(s) loaded from casos/EP06_07.cases

🔍 Testing Python: EP06_07.py
⚠️ EP06_07.py: Empty file (fewer than 3 lines). Tests skipped.