Filtering & convolution

Image analysis

About this lecture

Last time we moved pixels. Today we change their values, using the pixels nearby.

  • A kernel is a small grid of weights. Each output pixel is the weighted sum of its neighbourhood.
  • This is convolution: linear and the same everywhere in the image.
  • Blur, sharpen and edge detection are all just different weights.
  • Derivatives of an image are differences of neighbours: Taylor tells us which.
  • What the sum of the weights tells you, and what to do at the border.

A piece of the electron micrograph and three filtered versions of it: Gaussian (smoother), sharpen (crisper, noisier), and Sobel x (vertical edges light or dark on a mid-grey background).

Slides titled The math give the formulas and proofs; the ideas are complete without them.

A kernel slides over the image

  • Centre the 3 \times 3 kernel on a pixel, multiply each of the nine pixels by the weight on top of it, and add the nine products. That sum is the output pixel.
  • Move one pixel on and repeat. The window always reads the input, never earlier outputs.
  • The same nine weights are used everywhere.

A 5 × 5 input with values 1, 5 and 9 along a diagonal edge; the orange 3 × 3 window around the centre pixel is multiplied by the Gaussian weights 1 2 1 / 2 4 2 / 1 2 1 divided by 16, giving 7.5 in the output; the other interior outputs are 2.5, 5, 7.5, 8.75 and 9, the outer ring is left empty.

Two properties make this special:

  • Linear: double the input, and the output doubles; filter the sum of two images, and you get the sum of the two filtered images.
  • Shift-invariant: shift the input, and the output shifts the same way.
  • So filtering twice is again a filter: two kernels can be combined into one.

One output pixel, by hand

Neighbourhood and two kernels:

\begin{bmatrix} 1 & 5 & 9\\ 5 & 9 & 9\\ 9 & 9 & 9 \end{bmatrix} \qquad \text{Gaussian } \tfrac1{16}\begin{bmatrix} 1 & 2 & 1\\ 2 & 4 & 2\\ 1 & 2 & 1 \end{bmatrix} \qquad \text{Laplacian } \begin{bmatrix} 0 & 1 & 0\\ 1 & -4 & 1\\ 0 & 1 & 0 \end{bmatrix}

  • Gaussian: (1 + 10 + 9 + 10 + 36 + 18 + 9 + 18 + 9)/16 = 120/16 = 7.5.
  • Laplacian: 5 + 5 + 9 + 9 - 4\cdot 9 = -8. Negative! Edge filters need a display with 0 as mid-grey.

Sum of the weights: on a flat region of value c, the output is c times the sum of the weights.

  • Sum = 1: brightness is kept (blurs, sharpen).
  • Sum = 0: flat regions give 0; the filter only sees change (edges).

Filtering as pseudocode

for every pixel (x, y):
    sum = 0
    for dy in −1, 0, 1:
        for dx in −1, 0, 1:
            sum = sum + w[dy, dx] · I(x + dx, y + dy)    # outside the image: border rule
    out(x, y) = sum
  • The result goes into a new image out: the window always reads the input, never pixels it has already changed.
  • Nine multiplications per pixel. A k \times k kernel needs k^2, so large kernels get slow; the Fourier view (next lecture) and separable kernels help.
  • I(x + dx, y + dy) can fall outside the image at the edge: the border rule decides what is read there (last slide of this lecture).

The math: correlation and convolution

For a 3\times3 kernel w, the output at pixel (i, j) is

I'[i, j] = \sum_{k=-1}^{1}\sum_{l=-1}^{1} w[k, l]\; I[i + k,\, j + l].

Strictly, this is cross-correlation. Convolution flips the kernel, and is the discrete version of the integral you know from calculus:

(f * g)(x) = \int f(s)\, g(x - s)\, ds \qquad\longleftrightarrow\qquad (I * w)[i,j] = \sum_{k,l} I[k,l]\, w[i-k,\, j-l].

  • Linear: (aI_1 + bI_2) * w = a(I_1 * w) + b(I_2 * w). On an image with n pixels, filtering is a (huge, sparse) n \times n matrix.
  • Shift-invariant: if J[i, j] = I[i - a, j - b], then (J * w)[i, j] = (I * w)[i - a, j - b].
  • Every operator with both properties is a convolution with some kernel. And (I * w_1) * w_2 = I * (w_1 * w_2): filters combine into one.
  • On a flat region, I \equiv c: \;(I * w)[i, j] = c \sum_{k,l} w[k,l].

Derivatives are differences of neighbours

The slope of the brightness at a pixel: right neighbour minus left neighbour, divided by 2. The curvature: right + left − 2 × centre.

pixel values 0 0 3 6 6
slope, kernel \tfrac12\,[-1\;\;0\;\;1] 1.5 3 1.5
curvature, kernel [1\;\;-2\;\;1] 3 0 −3

The slope peaks in the middle of the edge; the curvature changes sign there (a zero crossing).

  • Sobel x = \begin{bmatrix}1\\2\\1\end{bmatrix}[-1\;\;0\;\;1]: a slope in x, smoothed over three rows. Sobel y likewise.
  • Together they give the gradient: it points across the edge, and its length is the edge strength.
  • Laplacian: the curvature [1\;-2\;1] in x plus the same in y gives \begin{bmatrix} 0 & 1 & 0\\ 1 & -4 & 1\\ 0 & 1 & 0 \end{bmatrix}.

Stem plots of the five pixel values 0, 0, 3, 6, 6, of the slope 1.5, 3, 1.5 (peak in the middle of the edge) and of the curvature 3, 0, −3 (zero crossing at the middle pixel).

The math: derivatives from Taylor

Taylor: f(x \pm h) = f(x) \pm h f'(x) + \tfrac{h^2}{2} f''(x) \pm \tfrac{h^3}{6} f'''(x) + \dots With pixel spacing h = 1:

f'(x) \approx \tfrac12\big[f(x+1) - f(x-1)\big] \quad \to \quad \tfrac12\,[-1\;\;0\;\;1] f''(x) \approx f(x+1) - 2f(x) + f(x-1) \quad \to \quad [1\;\;-2\;\;1]

  • Subtracting the two expansions cancels the even terms: the central difference is accurate to \mathcal{O}(h^2); the one-sided f(x+1) - f(x) only to \mathcal{O}(h).
  • Adding them cancels the odd terms: the second difference is accurate to \mathcal{O}(h^2).
  • The gradient \nabla I = (I_x, I_y) \approx (\text{Sobel x}, \text{Sobel y}); the edge strength is \|\nabla I\|.
  • The Laplacian \nabla^2 I = I_{xx} + I_{yy}: the sum of the two second differences.

Try it: 3 × 3 kernels

Noise and edges

Derivatives amplify noise: noise is exactly “a pixel that differs from its neighbours”.

  • So smooth first, then differentiate. Blur-then-derivative is again a single filter: one combined kernel.
  • Laplacian of Gaussian (Gaussian blur, then Laplacian): a blob and edge detector at a chosen scale \sigma, the width of the Gaussian.
  • The scale of the Gaussian decides the scale of the structures you find (the edges day and scale space develop this).

Three panels: the micrograph with added noise of standard deviation 30 grey levels; its Laplacian, which is pure speckle; and the Laplacian after blurring twice with the 3 × 3 Gaussian, where the outline of the nucleus shows as a thin line.

What happens at the border?

At the edge, part of the window falls outside the image. Common choices:

  • Zeros: simple, but the border darkens.
  • Copy the edge (replicate): pixels outside take the value of the nearest edge pixel.
  • Mirror: reflect the image at its edge. Fewer artefacts for smooth images.
  • Wrap around (periodic): what the Fourier transform assumes, as we will see next time.

There is no correct answer; just be aware that the outer pixels are less trustworthy.

A 64 × 64 piece of the micrograph, outlined in orange, extended by 16 pixels on every side in four ways: black (zeros), smeared edge pixels (copy the edge), a mirror image (mirror), and the opposite side of the piece (wrap around).

Summary

  • Filtering = weighted sum of a neighbourhood = convolution: linear and shift-invariant.
  • Sum of weights 1 keeps brightness; 0 responds only to change.
  • Derivative kernels are differences of neighbours: Sobel (slope, gradient), Laplacian (curvature). Taylor tells us which differences are accurate.
  • Smooth before differentiating; mind the border. Edges and blobs in depth: the edges day.
  • Next: the Fourier view, where convolution becomes multiplication.