AI Hints
Home›Computer Vision Tutorial›Classic Detectors
Computer Vision · Lesson 54 of 111

Measuring Texture

Smooth, rough, or scarred?.

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

A healthy apple's skin is smooth. A scabby one is rough. They could be exactly the same colour. So how would a computer tell them apart? You need a way of describing not the colour, but the pattern.

Measuring Texture
From the video: Measuring Texture
  • Smooth skin, scabby skin
  • Same colour
  • Describe the pattern

Local binary patterns

Local binary patterns is a clever trick. For each pixel, compare it with its eight neighbours, and record which are brighter. That gives a small code for the local pattern. A histogram of those codes is a texture fingerprint. We compare two smooth patches, and one rough one. Run it. Two smooth patches differ by about point zero two. A smooth and a rough patch differ by more than double that. Notice that this works on comparisons, rather than on brightness, so a darker room hardly changes the result.

texture.pypython
import numpy as np
from skimage.feature import local_binary_pattern

rng = np.random.default_rng(0)

def smooth():
    return (rng.normal(150, 4, (80, 80))).clip(0, 255).astype(np.uint8)

def rough():
    return (rng.normal(150, 40, (80, 80))).clip(0, 255).astype(np.uint8)

def signature(patch):
    lbp = local_binary_pattern(patch, 8, 1, "uniform")
    return np.histogram(lbp, bins=10, range=(0, 10), density=True)[0]

a, b, c = signature(smooth()), signature(smooth()), signature(rough())
print(f"Same texture difference:      {abs(a - b).sum():.2f}")
print(f"Different texture difference: {abs(a - c).sum():.2f}")
Output
Same texture difference:      0.02
Different texture difference: 0.05

Summary

Texture is about patterns of neighbours. Next, the feature that ruled computer vision before deep learning arrived.

Key takeaways
  • Compare each pixel with neighbours
  • Histograms are fingerprints
  • Comparisons resist lighting changes
Next up: HOG Features — the method before deep learning