Quiz: image filters with 3×3 kernels
Short questions to test your understanding of 3 × 3 kernels. Try each on paper first, then open the answer. Throughout, a kernel is written row by row (for example 1 2 1 / 2 4 2 / 1 2 1), and an output pixel is the weighted sum of the 3 × 3 neighbourhood: each pixel times the weight on top of it, added up. The kernel is not flipped.
Q1. A pixel and its neighbours are \[\begin{bmatrix} 4 & 4 & 4\\ 4 & 13 & 4\\ 4 & 4 & 4 \end{bmatrix}.\] What is the output of the box blur (all weights 1, then ÷ 9) at the centre? And of the Laplacian 0 1 0 / 1 −4 1 / 0 1 0?
For the box blur, add all nine and divide by 9. For the Laplacian, add the four direct neighbours and subtract 4 times the centre.
Box blur: \((8 \cdot 4 + 13)/9 = 45/9 = 5\). The bright pixel is mostly averaged away. Laplacian: \(4 + 4 + 4 + 4 - 4 \cdot 13 = 16 - 52 = -36\). A single bright pixel gives a large response: edge kernels react strongly to isolated noisy pixels.
Q2. Without computing a whole image: which of these kernels keep the overall brightness of an image, and which give 0 on every flat region? (a) 1 2 1 / 2 4 2 / 1 2 1, then ÷ 16 (b) −1 0 1 / −2 0 2 / −1 0 1 (c) 0 −1 0 / −1 5 −1 / 0 −1 0 (d) 1 1 1 / 1 −8 1 / 1 1 1
On a flat region every pixel has the same value, so the output is that value times the sum of the weights.
Sums: (a) \(16/16 = 1\), (b) 0, (c) \(5 - 4 = 1\), (d) \(8 - 8 = 0\). So (a) the Gaussian blur and (c) sharpen keep the brightness; (b) Sobel x and (d) an 8-neighbour Laplacian give 0 on flat regions and only respond to changes, i.e. edges.
Q3. You build a blur from all ones, 1 1 1 / 1 1 1 / 1 1 1, but forget to divide by 9. What does the output image look like, and how do you fix it?
What is the sum of the weights?
The sum of the weights is 9, so every output pixel is about 9 times the local average: the image becomes 9 times too bright and, shown on a 0–255 scale, mostly white. Dividing by the sum of the weights, here 9, makes it an average again, with sum 1.
Q4. The image is dark on the left and bright on the right, with a vertical boundary in the middle. What sign does Sobel x, −1 0 1 / −2 0 2 / −1 0 1, give on the boundary? What does Sobel y, −1 −2 −1 / 0 0 0 / 1 2 1, give there?
Sobel x is right column minus left column; Sobel y is bottom row minus top row.
Sobel x is positive: the right column is brighter than the left column. Sobel y is 0: along a vertical boundary every row of the neighbourhood looks the same, so bottom minus top cancels. Sobel x finds vertical edges (changes from left to right), Sobel y horizontal ones.
Q5. What does the kernel 0 0 0 / 0 0 0 / 0 1 0 do to an image?
Only one weight is not zero. Which input pixel does each output pixel copy?
Each output pixel copies the pixel just below it, so the whole image moves one pixel up. The sum of the weights is 1, so brightness is kept; nothing is blurred. (With a true, flipped convolution the image would move down instead.)
Q6. An edge detector applied to a noisy micrograph shows speckle everywhere and the cell outlines are hard to see. What would you do, and what is the price?
What does an edge kernel see in a single noisy pixel?
Blur first (for example one or two passes of the Gaussian), then apply the edge detector. Noise pixels differ from their neighbours, which is what edge kernels look for, so blurring removes much of the speckle while the large outlines survive. The price: edges become wider and less precise, and thin lines or small spots fade along with the noise.
Q7. You apply a box blur 10 times to an image, using zeros for the pixels outside the image. What do you expect to see near the border? What is a better choice?
What do the missing pixels count as?
A dark frame around the image: near the border the average includes zeros (black), and every pass carries the darkening further inwards. Copying the edge pixels outward (replicate) avoids this, because a flat region near the border then stays flat. A third option is to compute only where the window fits, which makes the output 2 pixels smaller in each direction.
Q8. In Fourier terms, is the Gaussian blur a low-pass or a high-pass filter? And the Laplacian? How does the sum of the weights fit in?
Frequency 0 is the overall brightness, that is a flat image.
The Gaussian blur is a low-pass filter: it keeps broad features and suppresses fine detail. The Laplacian is a high-pass filter: it removes the flat parts and keeps fine detail and edges. At frequency 0 (a flat image) the kernel multiplies the brightness by the sum of its weights: 1 for the Gaussian (passed), 0 for the Laplacian (blocked). Sliding a kernel over an image is the same as multiplying its spectrum by the kernel’s frequency response.