Further reading: not part of the course. This material is not included in the course, but it is relevant if you want to learn more about the topic.

Image filters: 3×3 kernels

Further reading — Image filters: 3×3 kernels

Quantitative biology and integrative omics

About this lecture

In Transforming images with matrices, an image was a grid of numbers that we moved around. Today we change the numbers themselves, using the pixels nearby.

  • A kernel is a small 3 × 3 grid of weights.
  • Slide it over the image. Each output pixel is the weighted sum of the 3 × 3 pixels around it.
  • With different weights you get blur, sharpening, edge detection or emboss.
  • What the sum of the weights tells you, and what to do at the border.
  • Why this matters in bioimage analysis, and how it links to the Fourier view of images.

An image is a grid of numbers

A greyscale image is a matrix: one number per pixel, here from 0 (black) to 255 (white).

In the zoom, the dark column of numbers (42, 45, 61, …) is the nuclear envelope. To a computer, “an edge” is nothing more than numbers that change quickly from one pixel to the next.

A kernel: a small grid of weights that slides

  • Put the 3 × 3 kernel on top of the image, centred on one pixel.
  • Multiply every pixel by the weight lying on top of it, and add the nine products.
  • That sum is the output pixel. Then move one pixel on and repeat, until every pixel has been visited.

The output is a new image of the same size. The input is never changed along the way.

One output pixel, by hand

The same 3 × 3 neighbourhood with other kernels:

Kernel Weights Weighted sum Output
box blur all 1, then ÷ 9 65 65 ÷ 9 ≈ 7.2
Gaussian blur 1 2 1 / 2 4 2 / 1 2 1, then ÷ 16 120 7.5
sharpen 0 −1 0 / −1 5 −1 / 0 −1 0 45 − 28 17
Laplacian (edges) 0 1 0 / 1 −4 1 / 0 1 0 28 − 36 −8

Blurring averages neighbours and removes noise

Box blur: the plain average of the 9 pixels. \tfrac19\begin{bmatrix} 1 & 1 & 1\\ 1 & 1 & 1\\ 1 & 1 & 1 \end{bmatrix}

Gaussian blur: a weighted average, with most weight in the centre. \tfrac1{16}\begin{bmatrix} 1 & 2 & 1\\ 2 & 4 & 2\\ 1 & 2 & 1 \end{bmatrix}

  • Each pixel becomes more like its neighbours: noise is averaged away, and sharp steps become ramps.
  • The Gaussian is gentler and looks more natural: it is a small version of the bell curve e^{-\text{distance}^2}, the same shape in every direction.
  • Want more blur? Apply the kernel again. Two Gaussian passes act like one larger, wider Gaussian.

Sharpening: the original plus its edges

\underbrace{\begin{bmatrix} 0 & -1 & 0\\ -1 & 5 & -1\\ 0 & -1 & 0 \end{bmatrix}}_{\text{sharpen}} = \underbrace{\begin{bmatrix} 0 & 0 & 0\\ 0 & 1 & 0\\ 0 & 0 & 0 \end{bmatrix}}_{\text{identity: the original}} - \underbrace{\begin{bmatrix} 0 & 1 & 0\\ 1 & -4 & 1\\ 0 & 1 & 0 \end{bmatrix}}_{\text{Laplacian: the edges}}

  • The identity kernel copies the image unchanged: only the centre weight is 1.
  • Sharpening takes the original and removes a bit of the local average: where a pixel differs from its neighbours, the difference is made larger.
  • Flat regions are unchanged (weights sum to 5 − 4 = 1). Edges get a dark rim on the dark side and a bright rim on the bright side, so they look crisper.
  • Nothing new is created: sharpening cannot restore detail that was never recorded, and it makes noise stronger as well.

Edge kernels respond only where brightness changes

Laplacian \begin{bmatrix} 0 & 1 & 0\\ 1 & -4 & 1\\ 0 & 1 & 0 \end{bmatrix} neighbours minus centre: spots and outlines, in any direction

Sobel x \begin{bmatrix} -1 & 0 & 1\\ -2 & 0 & 2\\ -1 & 0 & 1 \end{bmatrix} right minus left: vertical edges

Sobel y \begin{bmatrix} -1 & -2 & -1\\ 0 & 0 & 0\\ 1 & 2 & 1 \end{bmatrix} below minus above: horizontal edges

  • On a flat region all three give 0. They answer only where the brightness changes.
  • The sign says which way: Sobel x is positive where the image gets brighter to the right, negative where it gets darker.
  • Edge strength in any direction: combine both Sobels like the length of a vector, \sqrt{(\text{Sobel x})^2 + (\text{Sobel y})^2}.

The sum of the weights tells you a lot

Sum of the weights On a flat region Examples Kind of filter
1 brightness kept identity, blurs, sharpen, emboss smoothing or enhancing
0 output 0 Laplacian, Sobel x, Sobel y detects change (edges)
anything else brightness × sum e.g. box of ones without ÷ 9 divide by the sum to fix

Try it: edit a kernel and watch the output

At the border, copying edge pixels is usually safest

At the edge of the image, part of the 3 × 3 window falls outside. Three common choices:

  • Zeros outside: simple, but the border darkens (the missing pixels count as black).
  • Copy the edge (replicate): pretend the outermost pixels continue. Usually the safest.
  • Skip the border: only compute where the window fits. The output is then 2 pixels narrower and 2 pixels shorter.

Blur before finding edges, but blurring loses detail

  • Edge kernels amplify noise. Noise is a pixel that differs from its neighbours, which is exactly what they look for.
  • So blur first, then look for edges. On this noisy cell the outline is 2.8 times stronger than the background with edges only, and 5.9 times after two Gaussian passes.
  • Blurring loses detail. Thin lines and small spots fade along with the noise, and two nearby objects can merge. Use the smallest blur that does the job.

In R: a filter in a few lines

filter3 <- function(img, k) {
  nr <- nrow(img); nc <- ncol(img)
  p <- img[c(1, 1:nr, nr), c(1, 1:nc, nc)]          # copy the edge pixels outward
  out <- matrix(0, nr, nc)
  for (i in 1:nr) for (j in 1:nc)
    out[i, j] <- sum(k * p[i:(i + 2), j:(j + 2)])    # weights × neighbourhood, added up
  out
}

img5  <- matrix(c(1, 1, 1, 1, 1,
                  1, 1, 5, 9, 9,
                  1, 5, 9, 9, 9,
                  1, 9, 9, 9, 9,
                  1, 9, 9, 9, 9), 5, byrow = TRUE)
gauss <- matrix(c(1, 2, 1,  2, 4, 2,  1, 2, 1), 3, byrow = TRUE) / 16
filter3(img5, gauss)[3, 3]
# [1] 7.5

k * p[...] multiplies entry by entry (not %*%), and sum() adds the nine products. The padded matrix p has one extra row and column on every side, so p[i:(i + 2), j:(j + 2)] is the neighbourhood of pixel [i, j].

Where 3 × 3 kernels are used in bioimage analysis

  • Denoising before segmentation. A Gaussian blur before a brightness threshold gives smoother, less ragged masks of nuclei or cells.
  • Outlines. The Sobel edge strength marks nucleus and cell boundaries; segmentation methods such as watershed often start from it.
  • Spots. The Laplacian of a blurred image picks out small bright dots, such as single-molecule FISH spots or vesicles.
  • Uneven illumination. A very wide blur (many passes) estimates the slowly varying background, which is then subtracted.
  • Neural networks. The first layers of a convolutional neural network (CNN) are exactly such small kernels, but their weights are learned from examples instead of chosen by hand.

The Fourier view: blur = low-pass, edges = high-pass

How strongly a kernel lets through each spatial frequency (centre = smooth parts, corners = fine detail):

  • Gaussian blur keeps the centre and suppresses the fine detail: a low-pass filter.
  • Laplacian is 0 in the centre and largest at fine detail: a high-pass filter.
  • The value in the centre is the sum of the weights: 1 and 0.

Convolution in image space is multiplication in frequency space: sliding a kernel over the image does the same as multiplying the image’s spectrum by the kernel’s response.

Summary

Idea In one line
Image a grid of numbers, here 0–255
Kernel a 3 × 3 grid of weights
Filtering each output pixel = weighted sum of its 3 × 3 neighbourhood
Sum of weights = 1 brightness kept: blurs, sharpen, emboss
Sum of weights = 0 responds only to change: Laplacian, Sobel
Border zeros, copy the edge, or skip
Noise edge kernels amplify it: blur first
Fourier blur = low-pass, edges = high-pass; convolution = multiplication of spectra

Same nine-number recipe, very different results: the weights are the whole story.