AI Hints
Home›Computer Vision Tutorial›Vision Projects Two
Computer Vision · Lesson 82 of 111

Finding Flaws in Fabric

When the pattern is the clue.

⏱ 2 min read▶ Video at 2:08:38🎓 Free
Watch this lesson in the Computer Vision courseWatch on YouTube ↗Full Computer Vision course

The farm's nets are woven from fine mesh, and a tear lets the birds in. Inspecting woven material is one of the oldest factory vision jobs. Here's a beautiful trick. A woven pattern repeats perfectly. So if you remove the repeating part, the only thing that's left is the flaw.

Finding Flaws in Fabric
From the video: Finding Flaws in Fabric
  • Woven nets tear
  • An old factory job
  • Remove the repeating part

Subtracting the pattern

We make a repeating cloth pattern, and punch a small hole in it. A repeating pattern appears as a few very strong frequencies, just as our stripes did. So we keep only those strongest frequencies, and rebuild the image. That gives us a perfect cloth, with no hole, because a hole isn't a repeating thing. Then the difference between the real image and the rebuilt one points to the flaw. Run it. The method says that the hole is near two seventy three, one fifty two, and the truth is two eighty, one fifty. No training, and no examples of holes. We used only the fact that a healthy weave repeats. Knowing your problem is worth more than any model.

fabric.pypython
import numpy as np
import cv2

rng = np.random.default_rng(0)
x, y = np.meshgrid(np.arange(400), np.arange(400))
cloth = 128 + 40 * np.sin(x * 2 * np.pi / 10) * np.sin(y * 2 * np.pi / 10) + rng.normal(0, 4, (400, 400))
cv2.circle(cloth, (280, 150), 14, 128, -1)

f = np.fft.fft2(cloth)
peaks = np.abs(f) > np.quantile(np.abs(f), .999)
rebuilt = np.fft.ifft2(np.where(peaks, f, 0)).real
gap = cv2.GaussianBlur(np.abs(cloth - rebuilt), (15, 15), 0)

where = np.unravel_index(gap.argmax(), gap.shape)
print("Hole found near (x, y):", (int(where[1]), int(where[0])), "truth (280, 150)")
Output
Hole found near (x, y): (273, 152) truth (280, 150)

Think before you train

These last few projects share a lesson. QR codes, perspective, repeating patterns, and fixed cameras. Each one uses something that you already know about the problem. Deep learning is at its best when you've got no such knowledge. But if you've got a strong structure, use it. You'll need less data, and your system will fail in ways that you can understand.

Use what you knowstructure beats data
Less data neededno labelling
Understandable failureseasy to debug
Deep learningwhen structure's missing

Summary

Use the structure in your problem. Next, some small technical details that quietly ruin models.

Key takeaways
  • Patterns are few frequencies
  • Rebuild, then subtract
  • Use what you know
Next up: Resizing Properly — the bug hidden in one line