Every slide you need, in plain words, with each window drawn and every past-paper filtering question solved the way you write it in the answer script.
Slide 3. Until now we used two methods: (1) transformation functions, (2) histogram processing.
Slide 4. The third method is convolution, written two ways: g(x,y) = h(x,y) * f(x,y), the mask convolved with an image; or g(x,y) = f(x,y) * h(x,y), the image convolved with a mask.
Slide 5. Convolution/correlation is a neighbourhood operation. The kernel is also called a filter / mask / window / box, and it is written as a 2-D matrix.
Slide 6. Convolution/correlation is the process of moving a kernel over the image.
Slide 7. Steps (MATLAB's im2conv()):
Kernel [−1 0 1; 2 1 2; 1 −2 0] placed on the top-left 3×3 block of the input:
| 5 | 2 | 6 |
| 4 | 3 | 4 |
| 3 | 9 | 2 |
| -1 | 0 | 1 |
| 2 | 1 | 2 |
| 1 | -2 | 0 |
(−1×5) + (0×2) + (1×6) + (2×4) + (1×3) + (2×4) + (1×3) + (−2×9) + (0×2) = −5 + 0 + 6 + 8 + 3 + 8 + 3 − 18 + 0 = 5.
5 is written in the output at the position of the centre pixel (3). Then the kernel moves one step right.
Try it, step by step: the slide 8 kernel visiting every position of the 8×8 source.
Slide 9. The filtering here is called correlation/convolution. A spatial filter consists of (i) a neighbourhood and (ii) a predefined operation (the filter/mask/kernel).
Slide 10. With neighbourhood pixels a…i around centre e and kernel r…z:
| a | b | c |
| d | e | f |
| g | h | i |
| r | s | t |
| u | v | w |
| x | y | z |
The process is repeated for every pixel of the original image to generate the processed image.
Slides 11–12. Spatial correlation of a filter w(s,t) of size m×n with an image f(x,y) of size M×N:
with m = 2a+1 and n = 2b+1. For a 3×3 kernel, a = b = 1, so s and t run over −1, 0, 1: exactly "multiply the 9 pairs and add".
Slide 13. Convolution is the same operation except the filter is flipped both horizontally and vertically (rotated 180°) before it is applied:
For symmetric kernels (mean, weighted average, Gaussian, Laplacian) flipping changes nothing, so correlation and convolution give the same answer. That's why exam questions just say "apply the filter".
Take an image that is all 0 except one pixel = 1 (an impulse) and a kernel with entries a…i.
| 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 |
| a | b | c |
| d | e | f |
| g | h | i |
| 0 | 0 | 0 | 0 | 0 |
| 0 | i | h | g | 0 |
| 0 | f | e | d | 0 |
| 0 | c | b | a | 0 |
| 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 |
| 0 | a | b | c | 0 |
| 0 | d | e | f | 0 |
| 0 | g | h | i | 0 |
| 0 | 0 | 0 | 0 | 0 |
Correlation with an impulse gives the kernel flipped horizontally and vertically. Convolution flips the kernel first, so the output equals the filter kernel. Slide 14 (Figure 3.30) shows the same thing with kernel 1…9: correlation gives 9 8 7 / 6 5 4 / 3 2 1, convolution gives 1 2 3 / 4 5 6 / 7 8 9.
Slide 15. Two types of filtering:
Slide 16. Smoothing filters include linear and nonlinear filters:
| kind | filters | how the output is made |
|---|---|---|
| linear | standard average (mean), weighted average | sum of products with fixed weights |
| nonlinear (order statistics) | median, min, max | sort the window, pick by rank |
Slide 17. Weighted averaging formula for an M×N image with an m×n filter:
Dividing by the sum of the weights keeps the brightness the same.
Slide 18. Two smoothing averaging masks:
| 1 | 1 | 1 |
| 1 | 1 | 1 |
| 1 | 1 | 1 |
| 1 | 2 | 1 |
| 2 | 4 | 2 |
| 1 | 2 | 1 |
The sum of all the elements should be 1 (9 × 1/9 = 1; 16/16 = 1). A flat area then stays exactly the same.
In the weighted mask the centre counts 4 times, the edge neighbours 2 times and the corners once. Pixels closer to the centre matter more, so there's less blurring than with the mean.
| 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 110 | 120 | 90 | 130 | 0 |
| 0 | 91 | 94 | 98 | 200 | 0 |
| 0 | 90 | 91 | 99 | 100 | 0 |
| 0 | 82 | 96 | 85 | 90 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 |
g = (110 + 120 + 90 + 91 + 94 + 98 + 90 + 91 + 99) / 9 = 883/9 = 98.11 ≈ 98.1
Try it, step by step: slide 19 (mean) and slide 35 (weighted) on every pixel. It opens on the 94.
Mask 1/16 [1 2 1; 2 4 2; 1 2 1]. Multiply each pixel by its weight:
(1×110 + 2×120 + 1×90 + 2×91 + 4×94 + 2×98 + 1×90 + 2×91 + 1×99)/16
= (110 + 240 + 90 + 182 + 376 + 196 + 90 + 182 + 99)/16 = 1565/16 = 97.81 ≈ 98
The mask moves from point to point; at each point (x,y) the response is calculated. Border pixels are skipped (discard method), so the output is 8×8.
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 90 | 90 | 90 | 90 | 90 | 0 | 0 |
| 0 | 0 | 0 | 90 | 90 | 90 | 90 | 90 | 0 | 0 |
| 0 | 0 | 0 | 90 | 90 | 90 | 90 | 90 | 0 | 0 |
| 0 | 0 | 0 | 90 | 0 | 90 | 90 | 90 | 0 | 0 |
| 0 | 0 | 0 | 90 | 90 | 90 | 90 | 90 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 90 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 10 | 20 | 30 | 30 | 30 | 20 | 10 |
| 0 | 20 | 40 | 60 | 60 | 60 | 40 | 20 |
| 0 | 30 | 60 | 90 | 90 | 90 | 60 | 30 |
| 0 | 30 | 50 | 80 | 80 | 90 | 60 | 30 |
| 0 | 30 | 50 | 80 | 80 | 90 | 60 | 30 |
| 0 | 20 | 30 | 50 | 50 | 60 | 40 | 20 |
| 10 | 20 | 30 | 30 | 30 | 30 | 20 | 10 |
| 10 | 10 | 10 | 0 | 0 | 0 | 0 | 0 |
First few positions, as on the slides: g = 1/9 × (0+0+0+0+0+0+0+0+90) = 10, then 20, 30, 30. At the hole: 1/9 × (0+0+0+0+90+90+90+90+90) = 50. Fully inside the box: 1/9 × (9×90) = 90.
Notice: the hard edge of the box became a ramp (0, 10, 20, 30 …), the dark hole was filled in (50 instead of 0), and the stray 90 spread into a faint 10-valued patch. That's blurring and noise reduction at the same time.
Try it, step by step: the whole slides 20–27 walk-through, all 64 positions.
Slide 32. What the mean (box) filter does: replaces each pixel with an average of its neighbourhood; achieves a smoothing effect (removes sharp features); adds a "softer" look to the image.
Slide 33. Gross representation of objects: a Hubble image filtered with a 15×15 averaging mask, then thresholded. Small faint objects blend into the background and disappear; only the large bright objects survive as blobs.
Slide 34. Averaging effects: blurring + reducing noise. Box filters of 3×3, 5×5, 9×9, 15×15 and 35×35: the bigger the mask, the stronger the blur, and fine detail begins to disappear.
At the edge of the image, part of the 3×3 kernel hangs outside. Those outside cells have no values. Slide 28 gives three solutions:
| 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 17 | 24 | 1 | 8 | 15 | 0 |
| 0 | 23 | 5 | 7 | 14 | 16 | 0 |
| 0 | 4 | 6 | 13 | 20 | 22 | 0 |
| 0 | 10 | 12 | 19 | 21 | 3 | 0 |
| 0 | 11 | 18 | 25 | 2 | 9 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 |
| 24 | 1 | 8 |
| 5 | 7 | 14 |
| 1 | 8 | 15 |
| 1 | 8 | 15 |
| 7 | 14 | 16 |
Try it, step by step: change the Border menu and watch the dashed cells change: zeros, copied border pixels, or not used at all.
An m×m mask has (m − 1)/2 rows hanging over each side. Discarding removes that many rows and columns on every side, i.e. m − 1 in total.
Slides 39–40. σ is the "scale" / "width" / "spread" of the kernel and controls the amount of smoothing. "What happens if you increase σ?" The bell gets wider, far pixels get more weight, and the image gets more blurred.
Slide 41. The output is based on the order of gray levels in the masked area; the centre pixel is replaced with the value chosen by the ranking.
| 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 110 | 120 | 90 | 130 | 0 |
| 0 | 91 | 94 | 98 | 200 | 0 |
| 0 | 90 | 95 | 99 | 100 | 0 |
| 0 | 82 | 96 | 85 | 90 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 |
Step 1, sort ascending: 90, 90, 91, 94, 95, 98, 99, 110, 120. Step 2, replace the original value by the median: 95.
Try it, step by step: sort and pick on every pixel. Switch to min or max to compare.
| 10 | 15 | 20 |
| 23 | 90 | 27 |
| 33 | 31 | 30 |
sorted: 10, 15, 20, 23, 27, 30, 31, 33, 90 → 5th value = 27. The 90 is replaced by 27.
Slide 43. Properties of the median filter:
Slide 44. Median effect: blurring + reducing noise. It is very effective for removing salt-and-pepper noise (pepper image).
The noisy image has random 0 (pepper) and 255 (salt) pixels. Below, both 3×3 filters are applied to the inner 5×5 pixels:
| 49 | 49 | 255 | 48 | 48 | 49 | 49 |
| 47 | 0 | 48 | 49 | 49 | 49 | 0 |
| 47 | 48 | 49 | 49 | 255 | 50 | 49 |
| 49 | 255 | 49 | 49 | 0 | 50 | 50 |
| 48 | 48 | 48 | 48 | 48 | 49 | 255 |
| 48 | 0 | 49 | 50 | 50 | 50 | 49 |
| 255 | 50 | 50 | 51 | 51 | 50 | 50 |
| 47 | 48 | 49 | 49 | 49 |
| 48 | 49 | 49 | 50 | 50 |
| 49 | 49 | 49 | 49 | 50 |
| 48 | 48 | 48 | 48 | 49 |
| 48 | 49 | 50 | 50 | 50 |
| 66 | 66 | 94 | 72 | 66 |
| 66 | 66 | 66 | 67 | 61 |
| 71 | 71 | 66 | 66 | 90 |
| 66 | 66 | 43 | 44 | 67 |
| 66 | 44 | 49 | 50 | 72 |
| 48 | 49 | 49 | 49 | 49 |
| 48 | 49 | 49 | 49 | 49 |
| 48 | 49 | 49 | 49 | 50 |
| 48 | 49 | 49 | 49 | 50 |
| 48 | 49 | 50 | 50 | 50 |
The median output is within 1 of the original everywhere. The mean output carries the noise along: a 255 raises its whole neighbourhood to about 70, and a 0 pulls it down.
Try it, step by step: median vs mean on the salt-and-pepper image, pixel by pixel.
Slide 46. Common types of noise:
Filtering is useful for noise reduction: median for salt-and-pepper/impulse, Gaussian (or mean) for Gaussian noise.
Slide 47. A 3×3 mask with coefficients w1 … w9 covers pixels z1 … z9. The response is the sum of products:
An image is convolved with the 3×3 mask [0 0 0; 0 1 0; 0 0 0]. What is the relation between the original and the modified image?
At every pixel, g(x,y) = 0·z1 + … + 1·z5 + … + 0·z9 = z5 = f(x,y). Only the centre coefficient is 1, and it multiplies the pixel itself.
So g(x,y) = f(x,y): the output is identical to the original (no change). This mask is the identity, also called the unit impulse. Its coefficients add to 1, so brightness is kept, and no neighbour is mixed in, so nothing is blurred or shifted.
| 10 | 20 | 30 | 40 | 50 |
| 11 | 21 | 31 | 41 | 51 |
| 12 | 22 | 32 | 42 | 52 |
| 13 | 23 | 33 | 43 | 53 |
| 14 | 24 | 34 | 44 | 54 |
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 0 | 0 | 0 |
| 21 | 31 | 41 |
| 22 | 32 | 42 |
| 23 | 33 | 43 |
Now the 1 sits to the right of centre, so g(x,y) = f(x, y+1): each output pixel takes the value of its right-hand neighbour. The picture shifts left by 1 pixel.
| 10 | 20 | 30 | 40 | 50 |
| 11 | 21 | 31 | 41 | 51 |
| 12 | 22 | 32 | 42 | 52 |
| 13 | 23 | 33 | 43 | 53 |
| 14 | 24 | 34 | 44 | 54 |
| 0 | 0 | 0 |
| 0 | 0 | 1 |
| 0 | 0 | 0 |
| 31 | 41 | 51 |
| 32 | 42 | 52 |
| 33 | 43 | 53 |
The output at the position of 21 is 31 (the value to its right): every value moved one column to the left.
Try it, step by step: identity, shift and the 2δ − box sharpener on the test image.
2 × (identity) − (mean) gives g = 2f − f̄ = f + (f − f̄), where f̄ is the local average.
f − f̄ is the detail (how much a pixel differs from its neighbourhood). Adding it back makes differences bigger: a sharpening filter that "emphasizes differences with the local average". Its coefficients still add to 2 − 1 = 1, so flat areas keep their brightness.
| 40 | 40 | 70 |
| 40 | 40 | 70 |
| 40 | 40 | 70 |
| 40 | 70 | 70 |
| 40 | 70 | 70 |
| 40 | 70 | 70 |
Dark side: mean = (6×40 + 3×70)/9 = 450/9 = 50, so g = 2×40 − 50 = 30. Bright side: mean = (3×40 + 6×70)/9 = 540/9 = 60, so g = 2×70 − 60 = 80.
The edge 40 | 70 became 30 | 80: the jump is bigger, so the edge looks sharper. This is the idea behind unsharp masking in deck 07.
Slide 56. Cross-correlation is useful for template matching, i.e. locating a given pattern in an image. Slide the template H over the image F; wherever the image patch looks like the template, the sum of products is large. The highest value yields the location of the pattern in the image.
Coordinates: (row, column), counted from 0 at the top-left, as in the slides' origin. Zero padding unless the question says otherwise.
aust.png (5×5) has random 0 and 255 pixels; zero padding. (i) Which noise affects aust.png? (ii) Apply a 3×3 mean and a 3×3 median filter at point (1,1). (iii) Is it possible to remove the noise using only a Max or a Min filter? Justify. [1+3+2]
(i) The image has random black (0) and white (255) pixels scattered among normal values: salt-and-pepper (impulse) noise.
(ii)
| 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 12 | 0 | 0 | 23 | 0 |
| 0 | 255 | 145 | 200 | 110 | 17 | 0 |
| 0 | 255 | 160 | 90 | 250 | 15 | 0 |
| 0 | 0 | 255 | 130 | 185 | 15 | 0 |
| 0 | 15 | 20 | 20 | 21 | 21 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 |
3×3 mean: (0 + 12 + 0 + 255 + 145 + 200 + 255 + 160 + 90) / 9 = 1117/9 = 124.11 ≈ 124
3×3 median: sorted: 0, 0, 12, 90, 145, 160, 200, 255, 255 → 5th value = 145
The mean (124) is dragged by the two 255s and the 0s; the median (145) is a real neighbouring value, and the extremes end up at the two ends of the sorted list.
(iii) No. A max filter picks the largest value, so it removes pepper (0) but spreads salt (255) to every neighbour of a white dot. A min filter removes salt but spreads pepper. Salt-and-pepper noise contains both at once, so either filter alone makes half of the noise worse. The median filter removes both, because both extremes are pushed to the ends of the sorted list.
Try it, step by step: F23 aust.png. Opens at (1,1); try all four filters.
(i) Smooth the 3×3 image [2 5 8; 5 1 3; 4 7 2] with a 3×3 average filter, zero padding; show the calculations. (ii) Apply a 3×3 median filter to the 4×4 image; ignore the border and put zero in the output border.
(i) Pad with a ring of zeros, then average every 3×3 window:
| 0 | 0 | 0 | 0 | 0 |
| 0 | 2 | 5 | 8 | 0 |
| 0 | 5 | 1 | 3 | 0 |
| 0 | 4 | 7 | 2 | 0 |
| 0 | 0 | 0 | 0 | 0 |
| pixel | sum of the 9 values in its window | average | rounded |
|---|---|---|---|
| (0,0) | 0 + 0 + 0 + 0 + 2 + 5 + 0 + 5 + 1 | 13/9 = 1.44 | 1 |
| (0,1) | 0 + 0 + 0 + 2 + 5 + 8 + 5 + 1 + 3 | 24/9 = 2.67 | 3 |
| (0,2) | 0 + 0 + 0 + 5 + 8 + 0 + 1 + 3 + 0 | 17/9 = 1.89 | 2 |
| (1,0) | 0 + 2 + 5 + 0 + 5 + 1 + 0 + 4 + 7 | 24/9 = 2.67 | 3 |
| (1,1) | 2 + 5 + 8 + 5 + 1 + 3 + 4 + 7 + 2 | 37/9 = 4.11 | 4 |
| (1,2) | 5 + 8 + 0 + 1 + 3 + 0 + 7 + 2 + 0 | 26/9 = 2.89 | 3 |
| (2,0) | 0 + 5 + 1 + 0 + 4 + 7 + 0 + 0 + 0 | 17/9 = 1.89 | 2 |
| (2,1) | 5 + 1 + 3 + 4 + 7 + 2 + 0 + 0 + 0 | 22/9 = 2.44 | 2 |
| (2,2) | 1 + 3 + 0 + 7 + 2 + 0 + 0 + 0 + 0 | 13/9 = 1.44 | 1 |
| 2 | 5 | 8 |
| 5 | 1 | 3 |
| 4 | 7 | 2 |
| 1 | 3 | 2 |
| 3 | 4 | 3 |
| 2 | 2 | 1 |
(ii) Only the 4 inner pixels have a full window:
| 0 | 2 | 1 | 0 |
| 5 | 2 | 3 | 8 |
| 0 | 1 | 3 | 4 |
| 3 | 0 | 7 | 2 |
| pixel | sorted window | median |
|---|---|---|
| (1,1) | 0, 0, 1, 1, 2, 2, 3, 3, 5 | 2 |
| (1,2) | 0, 1, 1, 2, 2, 3, 3, 4, 8 | 2 |
| (2,1) | 0, 0, 1, 2, 3, 3, 3, 5, 7 | 3 |
| (2,2) | 0, 1, 2, 2, 3, 3, 4, 7, 8 | 3 |
| 0 | 0 | 0 | 0 |
| 0 | 2 | 2 | 0 |
| 0 | 3 | 3 | 0 |
| 0 | 0 | 0 | 0 |
(Part iii, the single-pass Laplacian, is on the deck 07 page.)
Try it, step by step: SP23 (i): all 9 pixels with zero padding.
Try it, step by step: SP23 (ii): the 4 inner pixels, border ignored.
(i) Explain with an example how a spatial filter affects an image. (iii) Why does the median filter preserve more edge sharpness than the average filter?
(i) A spatial filter consists of a neighbourhood and a predefined operation. The mask is moved from pixel to pixel; at each position the response, the sum of products of mask coefficients and the pixels under them, becomes the new value of the centre pixel. Example: with the 1/9 mean mask, the pixel 94 in the slide-19 image becomes (110+120+90+91+94+98+90+91+99)/9 = 98.1. Applied everywhere, sharp features and noise are averaged out and the image looks smoother (blurred).
(iii) At an edge the 3×3 window holds pixels from both sides. The average mixes them, so it outputs an in-between value that did not exist in the image: the step becomes a ramp and the edge blurs. The median outputs one of the actual pixel values: whichever side has the majority (5 or more of the 9 pixels) wins, so pixels stay on their own side and the step stays a step. It also ignores a single extreme value instead of averaging it in. [Draw the 1-D step figure above.]
3-bit 4×4 image, Laplacian [0 1 0; 1 −4 1; 0 1 0], zero padding. (i) 3×3 mean filter at (2,2) (ii) 3×3 median at (0,0) (iii) Laplacian at (1,1) (iv) Why does the median preserve more edge sharpness than the mean?
(i)
| 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 7 | 1 | 0 | 5 | 0 |
| 0 | 6 | 3 | 6 | 2 | 0 |
| 0 | 6 | 4 | 4 | 1 | 0 |
| 0 | 1 | 5 | 7 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 |
3×3 mean: (3 + 6 + 2 + 4 + 4 + 1 + 5 + 7 + 0) / 9 = 32/9 = 3.56 ≈ 4
(ii)
| 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 7 | 1 | 0 | 5 | 0 |
| 0 | 6 | 3 | 6 | 2 | 0 |
| 0 | 6 | 4 | 4 | 1 | 0 |
| 0 | 1 | 5 | 7 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 |
3×3 median: sorted: 0, 0, 0, 0, 0, 1, 3, 6, 7 → 5th value = 0
Five of the nine values are padding zeros, so the median is 0 (this is why zero padding darkens corners).
(iii)
| 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 7 | 1 | 0 | 5 | 0 |
| 0 | 6 | 3 | 6 | 2 | 0 |
| 0 | 6 | 4 | 4 | 1 | 0 |
| 0 | 1 | 5 | 7 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 |
| 7 | 1 | 0 |
| 6 | 3 | 6 |
| 6 | 4 | 4 |
| 0 | 1 | 0 |
| 1 | -4 | 1 |
| 0 | 1 | 0 |
Laplacian response: (1)(1) + (1)(6) + (-4)(3) + (1)(6) + (1)(4) = 5
(iv) Same answer as SP22 (iii): the median picks a real value from the majority side of the edge, while the mean blends both sides into a new in-between value.
Try it, step by step: F21: opens at (2,2) for the mean; click (0,0) for the median, (1,1) for the Laplacian.
At point (3,2) of the reconstructed 7×7 image: Task 1, perform a 3×3 Max filter. Task 3, design a 3×3 spatial mask that averages the 4 neighbours of a point (x,y) but excludes the point itself.
Original image (rebuilt from bit planes; see the deck 04 page):
Task 1
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 7 | 7 | 7 | 7 | 7 | 7 | 7 | 0 |
| 0 | 7 | 7 | 7 | 2 | 7 | 7 | 7 | 0 |
| 0 | 7 | 5 | 0 | 0 | 0 | 5 | 7 | 0 |
| 0 | 5 | 1 | 1 | 0 | 1 | 1 | 5 | 0 |
| 0 | 5 | 5 | 0 | 0 | 0 | 5 | 5 | 0 |
| 0 | 3 | 3 | 3 | 2 | 3 | 3 | 3 | 0 |
| 0 | 7 | 7 | 7 | 7 | 7 | 7 | 7 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
3×3 max: sorted 0, 0, 0, 0, 0, 1, 1, 5, 5 → largest = 5
Task 2 (negative Laplacian, deck 07):
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 7 | 7 | 7 | 7 | 7 | 7 | 7 | 0 |
| 0 | 7 | 7 | 7 | 2 | 7 | 7 | 7 | 0 |
| 0 | 7 | 5 | 0 | 0 | 0 | 5 | 7 | 0 |
| 0 | 5 | 1 | 1 | 0 | 1 | 1 | 5 | 0 |
| 0 | 5 | 5 | 0 | 0 | 0 | 5 | 5 | 0 |
| 0 | 3 | 3 | 3 | 2 | 3 | 3 | 3 | 0 |
| 0 | 7 | 7 | 7 | 7 | 7 | 7 | 7 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 5 | 0 | 0 |
| 1 | 1 | 0 |
| 5 | 0 | 0 |
| 0 | -1 | 0 |
| -1 | 4 | -1 |
| 0 | -1 | 0 |
Laplacian response: (-1)(0) + (-1)(1) + (4)(1) + (-1)(0) + (-1)(0) = 3
Task 3. The 4 neighbours are up, down, left and right. Give each weight 1, the centre 0, the corners 0, and divide by 4 so the weights add to 1:
| 0 | 1 | 0 |
| 1 | 0 | 1 |
| 0 | 1 | 0 |
Try it, step by step: SP25 at (3,2): max, negative Laplacian, and the 4-neighbour mask.
At (3,3): Task 1, 3×3 Max filter; Task 2, 3×3 positive Laplacian; Task 3, the 4-neighbour averaging mask.
Task 1
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 7 | 7 | 7 | 7 | 7 | 7 | 7 | 0 |
| 0 | 7 | 7 | 7 | 2 | 7 | 7 | 7 | 0 |
| 0 | 7 | 5 | 0 | 0 | 0 | 5 | 7 | 0 |
| 0 | 5 | 1 | 1 | 0 | 1 | 1 | 5 | 0 |
| 0 | 5 | 5 | 0 | 0 | 0 | 5 | 5 | 0 |
| 0 | 3 | 3 | 3 | 2 | 3 | 3 | 3 | 0 |
| 0 | 7 | 7 | 7 | 7 | 7 | 7 | 7 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
3×3 max: sorted 0, 0, 0, 0, 0, 0, 0, 1, 1 → largest = 1
Task 2
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 7 | 7 | 7 | 7 | 7 | 7 | 7 | 0 |
| 0 | 7 | 7 | 7 | 2 | 7 | 7 | 7 | 0 |
| 0 | 7 | 5 | 0 | 0 | 0 | 5 | 7 | 0 |
| 0 | 5 | 1 | 1 | 0 | 1 | 1 | 5 | 0 |
| 0 | 5 | 5 | 0 | 0 | 0 | 5 | 5 | 0 |
| 0 | 3 | 3 | 3 | 2 | 3 | 3 | 3 | 0 |
| 0 | 7 | 7 | 7 | 7 | 7 | 7 | 7 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 |
| 1 | 0 | 1 |
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | -4 | 1 |
| 0 | 1 | 0 |
Laplacian response: (1)(0) + (1)(1) + (-4)(0) + (1)(1) + (1)(0) = 2
Task 3: the same mask 1/4 [0 1 0; 1 0 1; 0 1 0] as above.
Try it, step by step: SP24 at (3,3).
(i) Explain how you can smooth an image. If you subtract the smoothed image from the original, what happens if you add the result back to the original? (ii) Group selfies of batch "Integer 43" suffer from salt-and-pepper noise. Apply a de-noising process: which filtering is most effective, and how does it improve the photos?
(i) Smooth by convolving the image with a low-pass mask whose coefficients add to 1, e.g. the 3×3 mean 1/9 [all 1] or the weighted 1/16 [1 2 1; 2 4 2; 1 2 1]. Each pixel becomes the (weighted) average of its neighbourhood, which blurs and reduces noise.
Original − smoothed = the detail (mask) image: it holds only edges and fine detail, since the smooth parts cancel out. Adding this detail back to the original, g = f + (f − f̄), sharpens the image: edges and fine detail are emphasized. This is unsharp masking (with a weight k > 1 on the detail, high-boost filtering; see deck 07). It is the same as the slide 52–54 mask 2δ − box.
(ii) Use a 3×3 median filter.
An image is corrupted by salt-and-pepper noise in the object part only (not the background). Give the best way to denoise it and explain.
Use a median filter (3×3), applied to the object region. Salt-and-pepper noise consists of isolated extreme values (0 and 255). The median of each window ignores them, since they sort to the ends, and replaces each noisy pixel with a real value from its neighbourhood. It keeps the object's edges sharp, unlike the mean, which would blur the object boundary and smear the dots.
Because the background is clean, the filter can be restricted to the object (e.g. a mask from thresholding), so the background stays untouched and no extra blur is added there. Part (ii), morphological removal, is on the morphology page.
| filter | mask / rule | good for | side effect |
|---|---|---|---|
| mean (box) | 1/9 [all 1] | general noise | blurs edges |
| weighted average | 1/16 [1 2 1; 2 4 2; 1 2 1] | noise, gentler | less blur |
| Gaussian | bell-shaped weights, σ sets spread | Gaussian noise | blur grows with σ |
| median | 5th of 9 sorted | salt-and-pepper, impulse | keeps edges, slight blur |
| min / max | smallest / largest | salt / pepper only | spreads the other |
| identity | [0 0 0; 0 1 0; 0 0 0] | — | no change |
| shift | [0 0 0; 0 0 1; 0 0 0] | — | moves image left 1 pixel |