CSE 4227 · Chapter 3 · Deck 06

Smoothing Spatial Filters, from zero

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.

★ Must study on this page

Start here: what a filter does

slides 3–10On study list
In plain wordsSo far every new pixel depended only on its own old value (deck 04) or on the image's histogram (deck 05). Filtering is the third way: the new pixel depends on the pixel and its neighbours. Put a small grid of numbers (the kernel) on top of the image, multiply each kernel number by the pixel under it, add everything up, and write that sum as the new centre pixel. Then slide the kernel one pixel along and repeat.

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()):

  1. Multiply the corresponding elements (kernel value × pixel under it).
  2. Add them all: the sum of products.
  3. Repeat for every pixel of the image.
Slide 8 · correlation exampleworked

Kernel [−1 0 1; 2 1 2; 1 −2 0] placed on the top-left 3×3 block of the input:

526
434
392
pixels under the kernel
-101
212
1-20
kernel

(−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:

abc
def
ghi
3×3 neighbour pixels
rst
uvw
xyz
kernel / filter
eprocessed = v·e + r·a + s·b + t·c + u·d + w·f + x·g + y·h + z·i

The process is repeated for every pixel of the original image to generate the processed image.

Simulator: any image, any filter

slides 8–54Came in past papers
In plain wordsPick an image and a filter, then press Next step. Each pixel takes 4 steps: place the window, multiply (or sort), add (or pick), write. Nothing is skipped. ← and → keys step too; click any input pixel to jump there; Play runs it. Smaller simulators also sit next to each method below.

Correlation vs convolution (the equations)

slides 11–14Know in 2 lines

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:

w(s,t) ☆ f(x,y) = Σs=−aa Σt=−bb w(s,t) · f(x+s, y+t)

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:

w(s,t) ★ f(x,y) = Σ Σ w(s,t) · f(x−s, y−t)

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

Slides 14, 57–63 · filtering an impulseworked

Take an image that is all 0 except one pixel = 1 (an impulse) and a kernel with entries a…i.

00000
00000
00100
00000
00000
impulse F
abc
def
ghi
kernel H
00000
0ihg0
0fed0
0cba0
00000
correlation output: kernel flipped
00000
0abc0
0def0
0ghi0
00000
convolution output: kernel itself

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.

Types of filter and the two averaging masks

slides 15–18On study list

Slide 15. Two types of filtering:

  1. Smoothing spatial filters (low pass): used for blurring and for noise reduction.
  2. Sharpening spatial filters (high pass): used for edge detection and sharpness. Deck 07.

Slide 16. Smoothing filters include linear and nonlinear filters:

kindfiltershow the output is made
linearstandard average (mean), weighted averagesum of products with fixed weights
nonlinear (order statistics)median, min, maxsort the window, pick by rank

Slide 17. Weighted averaging formula for an M×N image with an m×n filter:

g(x,y) = Σ Σ w(s,t) f(x+s, y+t) / Σ Σ w(s,t),   m = 2a+1, n = 2b+1

Dividing by the sum of the weights keeps the brightness the same.

Slide 18. Two smoothing averaging masks:

111
111
111
1/9 × · Mean filter: all elements the same
121
242
121
1/16 × · Weighted average: different weights

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.

Mean (box) filter and weighted average

Must studyslides 19–35Came in past papers
In plain wordsThe mean filter replaces each pixel by the average of itself and its 8 neighbours. A lone bright speck gets averaged with 8 normal pixels, so it fades. That's noise reduction. A sharp edge also gets averaged with pixels from the other side, so it smears. That's blurring. You always get both together.
Slide 19 · standard averaging at the pixel 94worked
000000
0110120901300
09194982000
09091991000
0829685900
000000
image; shaded = 3×3 window, outlined = centre

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.

Slide 35 · weighted average at the same pixelworked

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

TrapThe slide prints 97.25 ≈ 97 here, but the nine terms add to 1565, not 1556, so the correct value is 97.8125 ≈ 98. Show your addition in the exam so the method marks are safe either way.
Slides 20–27 · mean filter over a whole 10×10 imageworked

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.

0000000000
0000000000
000909090909000
000909090909000
000909090909000
00090090909000
000909090909000
0000000000
00900000000
0000000000
input f (a 90-valued box with one dark hole and one stray 90)
010203030302010
020406060604020
030609090906030
030508080906030
030508080906030
020305050604020
1020303030302010
10101000000
output g (8×8)

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.

What happens at the border?

slides 28–31On study list

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:

0000000
0172418150
0235714160
0461320220
01012192130
0111825290
0000000
kernel centred on 8 in the top row: the top 3 cells fall outside
  1. Zero padding (slide 29): outside pixels are assumed to be 0. Easy, but the image gets a black border after filtering (the averages near the edge are pulled down).
  2. Border padding / pixel replication (slide 30): outside pixels copy the nearest boundary pixel.
  3. Discard (slide 31): don't compute the border pixels at all. The output is smaller: a 512×512 input gives a 510×510 output with a 3×3 mask.
000
2418
5714
window with zero padding
1815
1815
71416
window with replication

Try it, step by step: change the Border menu and watch the dashed cells change: zeros, copied border pixels, or not used at all.

Slide 31 · "Think for mask size 5×5 or 7×7?"solved

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.

  • 3×3: 1 per side → 512×512 becomes 510×510.
  • 5×5: 2 per side → 508×508.
  • 7×7: 3 per side → 506×506.

Gaussian filter

slides 36–40Know in 2 lines
In plain wordsA Gaussian filter is a weighted average whose weights follow a bell curve: biggest at the centre, smaller further away. The 1/16 [1 2 1; 2 4 2; 1 2 1] mask is the 3×3 Gaussian.
  • Performs a weighted average of surrounding pixels based on the Gaussian distribution (slide 36).
  • Used to remove Gaussian noise.
  • Good for reducing sharpness caused by random pixels; gives better performance than the average filter.
  • Discrete kernels: 3×3 (÷16), 5×5 (÷273), 7×7 (÷1003).
  • Gives less weight to pixels further from the centre of the window (slide 37).
−4−20240distance from centreweightσ = 1σ = 2
Gaussian weights. Small σ: tall and narrow, little smoothing. Large σ: low and wide, more smoothing.
Gσ(x,y) = 1/(2πσ²) · e−(x² + y²)/(2σ²)

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.

Order-statistic filters: median, min, max

Must studyslides 41–46Came in past papers
In plain wordsOrder-statistic filters don't multiply anything. They sort the 9 values in the window and pick one by its rank: the smallest (min), the largest (max) or the middle one (median, the 5th of 9). A noise spike is extreme, so it lands at an end of the sorted list and the median never picks it.

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.

Slide 42 · median at the pixel 94worked
000000
0110120901300
09194982000
09095991000
0829685900
000000
image; shaded = window

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.

Slide 43 · median of a spikeworked
101520
239027
333130
the 90 is a spike

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

Why median keeps edges sharper than the mean

0246810120105090pixel position (one row)gray valueinputmean (3)median (3)
One image row with a noise spike (position 5) and an edge (position 8). The mean smears the spike into a bump and turns the edge into a ramp. The median deletes the spike and keeps the edge a clean step.
Slide 45 data · median vs mean on salt-and-pepperworked

The noisy image has random 0 (pepper) and 255 (salt) pixels. Below, both 3×3 filters are applied to the inner 5×5 pixels:

494925548484949
470484949490
474849492555049
49255494905050
484848484849255
4804950505049
255505051515050
noisy image
4748494949
4849495050
4949494950
4848484849
4849505050
original (inner 5×5)
6666947266
6666666761
7171666690
6666434467
6644495072
3×3 mean
4849494949
4849494949
4849494950
4849494950
4849505050
3×3 median

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.

Coefficient masks: predict the output

slides 47–54Came in past papers

Slide 47. A 3×3 mask with coefficients w1 … w9 covers pixels z1 … z9. The response is the sum of products:

z = z1w1 + z2w2 + … + z9w9 = Σ ziwi
SP23 Q2(f)ii · slides 48–493 marks

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.

1020304050
1121314151
1222324252
1323334353
1424344454
test image
000
010
000
mask
213141
223242
233343
output (inner 3×3) = same values
Slides 50–51 · mask [0 0 0; 0 0 1; 0 0 0]worked

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.

1020304050
1121314151
1222324252
1323334353
1424344454
test image
000
001
000
mask
314151
324252
334353
output (inner 3×3)

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.

Slides 52–54 · [0 0 0; 0 2 0; 0 0 0] − 1/9 [all 1]worked

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.

404070
404070
404070
dark-side pixel 40
407070
407070
407070
bright-side pixel 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.

More applications: template matching

slides 55–56Know in 2 lines

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.

Past questions, solved in full

Must studyslides 17–46Came in past papers

Coordinates: (row, column), counted from 0 at the top-left, as in the slides' origin. Zero padding unless the question says otherwise.

F23 Q2(b) · aust.png6 marks

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)

0000000
001200230
0255145200110170
025516090250150
00255130185150
015202021210
0000000
window at (1,1); zero padding outside

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.

SP23 Q3(f) i–ii · SP22 Q1(a)ii3 + 3 marks

(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:

00000
02580
05130
04720
00000
zero-padded input
pixelsum of the 9 values in its windowaveragerounded
(0,0)0 + 0 + 0 + 0 + 2 + 5 + 0 + 5 + 113/9 = 1.441
(0,1)0 + 0 + 0 + 2 + 5 + 8 + 5 + 1 + 324/9 = 2.673
(0,2)0 + 0 + 0 + 5 + 8 + 0 + 1 + 3 + 017/9 = 1.892
(1,0)0 + 2 + 5 + 0 + 5 + 1 + 0 + 4 + 724/9 = 2.673
(1,1)2 + 5 + 8 + 5 + 1 + 3 + 4 + 7 + 237/9 = 4.114
(1,2)5 + 8 + 0 + 1 + 3 + 0 + 7 + 2 + 026/9 = 2.893
(2,0)0 + 5 + 1 + 0 + 4 + 7 + 0 + 0 + 017/9 = 1.892
(2,1)5 + 1 + 3 + 4 + 7 + 2 + 0 + 0 + 022/9 = 2.442
(2,2)1 + 3 + 0 + 7 + 2 + 0 + 0 + 0 + 013/9 = 1.441
258
513
472
input
132
343
221
smoothed output (rounded)

(ii) Only the 4 inner pixels have a full window:

0210
5238
0134
3072
input
pixelsorted windowmedian
(1,1)0, 0, 1, 1, 2, 2, 3, 3, 52
(1,2)0, 1, 1, 2, 2, 3, 3, 4, 82
(2,1)0, 0, 1, 2, 3, 3, 3, 5, 73
(2,2)0, 1, 2, 2, 3, 3, 4, 7, 83
0000
0220
0330
0000
median output (border = 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.

SP22 Q1(a) i, iii2 + 3 marks

(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.]

F21 B4(a)8 marks

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)

000000
071050
063620
064410
015700
000000
window at (2,2); zero padding outside

3×3 mean: (3 + 6 + 2 + 4 + 4 + 1 + 5 + 7 + 0) / 9 = 32/9 = 3.56 ≈ 4

(ii)

000000
071050
063620
064410
015700
000000
window at (0,0); zero padding outside

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)

000000
071050
063620
064410
015700
000000
window at (1,1); zero padding outside
710
636
644
window
010
1-41
010
positive Laplacian

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.

SP25 Q2(b)ii · Tasks 1 and 32 + 2 marks

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

000000000
077777770
077727770
075000570
051101150
055000550
033323330
077777770
000000000
window at (3,2); zero padding outside

3×3 max: sorted 0, 0, 0, 0, 0, 1, 1, 5, 5 → largest = 5

Task 2 (negative Laplacian, deck 07):

000000000
077777770
077727770
075000570
051101150
055000550
033323330
077777770
000000000
window at (3,2); zero padding outside
500
110
500
window
0-10
-14-1
0-10
negative Laplacian

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:

010
101
010
1/4 ×
g(x,y) = ¼ [ f(x−1,y) + f(x+1,y) + f(x,y−1) + f(x,y+1) ]

Try it, step by step: SP25 at (3,2): max, negative Laplacian, and the 4-neighbour mask.

SP24 Q2(b)ii · same image, point (3,3)6 marks

At (3,3): Task 1, 3×3 Max filter; Task 2, 3×3 positive Laplacian; Task 3, the 4-neighbour averaging mask.

Task 1

000000000
077777770
077727770
075000570
051101150
055000550
033323330
077777770
000000000
window at (3,3); zero padding outside

3×3 max: sorted 0, 0, 0, 0, 0, 0, 0, 1, 1 → largest = 1

Task 2

000000000
077777770
077727770
075000570
051101150
055000550
033323330
077777770
000000000
window at (3,3); zero padding outside
000
101
000
window
010
1-41
010
positive Laplacian

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

F22 A1(a)3 + 6 marks

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

  • Salt-and-pepper noise is random black (0) and white (255) dots. In each 3×3 window these extremes sort to the two ends of the list, so the median, a real neighbouring value, replaces them.
  • Unlike the mean filter, the median does not smear the dots into gray blotches, and it keeps the edges of faces, eyes and hair sharp, because it never creates new in-between values.
  • It introduces no new values and is very effective for impulse noise (slides 43–44). If the noise is dense, a second pass or a 5×5 median removes what's left, at the cost of a little extra blur.
  • Result: clean skin and background, sharp facial features, and natural-looking group photos.
SP24 Q3(a)i4 marks

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.

Traps and one-minute sheet

slides all
filtermask / rulegood forside effect
mean (box)1/9 [all 1]general noiseblurs edges
weighted average1/16 [1 2 1; 2 4 2; 1 2 1]noise, gentlerless blur
Gaussianbell-shaped weights, σ sets spreadGaussian noiseblur grows with σ
median5th of 9 sortedsalt-and-pepper, impulsekeeps edges, slight blur
min / maxsmallest / largestsalt / pepper onlyspreads 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