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.
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.
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
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).
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.
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.