ENع
←Images: blur and edges
Lesson S2 · Special topics

Images as 2-D signals: blur and edges

18 min11 steps5 questionsAdvanced
About this lesson

A grey image is a signal with two indexes, x[m, n]. Every idea from 1-D filtering carries over: slide a small kernel over the image, multiply and add, and you have 2-D convolution. Follow a 6×6 image through a 3×3 box blur (a 2-D moving average, so a low-pass filter) and the Sobel edge kernel (a smoothed difference, so a high-pass filter), see why separable kernels are fast, and do both in Python with numpy and SciPy. Build on the lessons on discrete convolution, the moving average and frequency response first.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig S2 · Interactive
01 / 11

An image is a 2-D signal

A grey image is a grid of numbers. Pixel x[m, n] holds the brightness at row m, column n. Our 6×6 image is dark (0) with a bright (9) square at the bottom right. Two indexes instead of one time.

x[m, n]   m = row (0..5), n = column (0..5)

        n=0 1 2 3 4 5
  m=0    0 0 0 0 0 0
  m=1    0 0 0 0 0 0
  m=2    0 0 9 9 9 9
  m=3    0 0 9 9 9 9
  m=4    0 0 9 9 9 9
  m=5    0 0 9 9 9 9

brightness on a 0..9 scale for easy sums
(8-bit images use 0..255)
Loading figure…
or ← →
Read the lesson step by step
  1. An image is a 2-D signal

    A grey image is a grid of numbers. Pixel x[m, n] holds the brightness at row m, column n. Our 6×6 image is dark (0) with a bright (9) square at the bottom right. Two indexes instead of one time.

    x[m, n]   m = row (0..5), n = column (0..5)
    
            n=0 1 2 3 4 5
      m=0    0 0 0 0 0 0
      m=1    0 0 0 0 0 0
      m=2    0 0 9 9 9 9
      m=3    0 0 9 9 9 9
      m=4    0 0 9 9 9 9
      m=5    0 0 9 9 9 9
    
    brightness on a 0..9 scale for easy sums
    (8-bit images use 0..255)
  2. Each row is a 1-D signal

    Read row 3 from left to right: 0, 0, 9, 9, 9, 9. That is an ordinary discrete signal x[n], here a step. Every row and every column is a signal; the whole grid is a function of two variables.

    row 3:  x[3, n] = 0, 0, 9, 9, 9, 9
               n = 0, 1, 2, 3, 4, 5
    
    a step: dark, then bright from n = 2
    
    1-D signal  x[n]      one index
    2-D signal  x[m, n]   two indexes
    (a video adds a third: time)
  3. A kernel: a small grid of weights

    An image filter is a small grid of weights, the kernel h[k, l]. The 3×3 box kernel has nine weights of 1/9, so each output pixel will be the average of a 3×3 patch of the image.

    box kernel h = (1/9) ×
       1 1 1
       1 1 1
       1 1 1
    
    nine weights, sum = 1
    so a flat patch keeps its brightness:
      nine 9s × 1/9 = 9
  4. Slide, multiply, add

    Lay the kernel on a patch, multiply each pixel by its weight and add the nine products: one output pixel. Slide one column and repeat. The kernel fits wholly in 16 places, so the output is 4×4.

    y[m, n] = Σk Σl h[k, l] · x[m − k, n − l]
    
    at y[3, 2] the patch is
       0 9 9
       0 9 9
       0 9 9
    six 9s and three 0s:
       (6 × 9) × 1/9 = 6
    
    "valid": only places with no padding
    (6 − 3 + 1) × (6 − 3 + 1) = 4×4
  5. Blur: the step becomes a ramp

    Row 3 of the output reads 3, 6, 9, 9. The jump from 0 to 9 now climbs over three pixels. Averaging neighbours smooths sharp changes: that is blur, a 2-D moving average.

    blur y[m, n], centres m, n = 1..4
      m=1   1  2  3  3
      m=2   2  4  6  6
      m=3   3  6  9  9
      m=4   3  6  9  9
    
    row 3: x = 0 9 9 9  (n = 1..4)
           y = 3 6 9 9
    the corner is soft in both directions
  6. Blur is a low-pass filter

    Along a row the box averages three neighbours. Its gain |1 + 2cos ω| / 3 is 1 for slow change (ω = 0), 0 at ω = 2π/3, and 1/3 for the fastest pixel-to-pixel flicker (ω = π). Fine detail fades.

    along a row: h1 = [1, 1, 1] / 3
    H(ω) = (e^jω + 1 + e^−jω) / 3
         = (1 + 2 cos ω) / 3
    
    |H(0)|     = 1      flat areas kept
    |H(2π/3)|  = 0      period-3 pattern gone
    |H(π)|     = 0.33   0 9 0 9 ... shrinks 3×
    
    ω in radians per pixel (a spatial frequency)
  7. Separable: rows, then columns

    The box kernel is a column [1 1 1]/3 times a row [1 1 1]/3. So blur every row, then every column: 6 multiplies a pixel instead of 9. Each output here is a(m)·a(n), with a = 1, 2, 3, 3.

    (1/9) ones(3, 3) = [1 1 1]ᵀ/3 · [1 1 1]/3
    
    row pass, then column pass:
      3 + 3 = 6 multiplies per pixel
      instead of 3 × 3 = 9
    (for a k×k kernel: 2k instead of k²)
    
    here y[m, n] = a(m)·a(n),  a = 1, 2, 3, 3
      y[3, 2] = 3 × 2 = 6
  8. Edges: the Sobel kernel

    Swap in the Sobel kernel. On the image it weighs the right column +1, +2, +1 and the left column −1, −2, −1, so it measures how fast brightness rises from left to right. Slide it again.

    Sobel (as written for convolution)
       +1  0  −1
       +2  0  −2
       +1  0  −1
    convolution flips it 180°, so on the image:
       −1  0  +1
       −2  0  +2
       −1  0  +1
    
    = [1 2 1]ᵀ (smooth down)
      × [1 0 −1] (difference across)
  9. Flat areas vanish, edges light up

    Where a patch is flat, right minus left is 0. Across the edge the sum is big: 9 + 18 + 9 = 36 in row 3. The output lights up exactly where the image jumps from dark to bright.

    edges y[m, n], centres m, n = 1..4
      m=1    9   9   0   0
      m=2   27  27   0   0
      m=3   36  36   0   0
      m=4   36  36   0   0
    
    y[3, 2]: patch columns 0 | 9 | 9
      (9 − 0)·1 + (9 − 0)·2 + (9 − 0)·1 = 36
    row 1 sees only one bright row: 9
    flat right half: 0
  10. Edges are a high-pass filter

    Along a row Sobel takes x[n+1] − x[n−1]. Its gain 2|sin ω| is 0 for flat areas (ω = 0) and peaks at 2 for ω = π/2: it keeps change and drops the constant, the opposite of blur.

    along a row: h2 = [−1, 0, 1]
    H(ω) = −e^jω + e^−jω = −2j sin ω  (centred)
    |H(ω)| = 2 |sin ω|
    
    |H(0)|   = 0   flat areas -> 0
    |H(π/2)| = 2   fastest useful change
    |H(π)|   = 0   (the 0 9 0 9 flicker is blocked too)
    
    down a column [1 2 1] is a gentle blur:
      |H| = 2 + 2 cos ω
  11. Blur and edges in Python

    SciPy's convolve2d does the slide, multiply and add; "valid" keeps the 16 full-fit places. Convolution flips the kernel, so Sobel is written np.outer([1, 2, 1], [1, 0, −1]) and lands as −1 0 +1.

    import numpy as np
    from scipy.signal import convolve2d
    img = np.zeros((6, 6)); img[2:, 2:] = 9
    box = np.ones((3, 3)) / 9
    blur = convolve2d(img, box, "valid")
    sx = np.outer([1, 2, 1], [1, 0, -1])
    edge = convolve2d(img, sx, "valid")
    
    >>> blur
    array([[1., 2., 3., 3.],
           [2., 4., 6., 6.],
           [3., 6., 9., 9.],
           [3., 6., 9., 9.]])
    >>> edge
    array([[ 9.,  9.,  0.,  0.],
           [27., 27.,  0.,  0.],
           [36., 36.,  0.,  0.],
           [36., 36.,  0.,  0.]])
    
    (Python 3 with numpy and SciPy)

Quiz Unlocks after the last step.