CSE 4227 · Chapter 3 · Deck 07

Sharpening Spatial Filters, from zero

Every slide you need, in plain words, with the derivative graphs drawn and every Laplacian and Sobel question solved the way you write it in the answer script.

★ Must study on this page

Start here: what sharpening is for

slides 3–6Came in past papers
In plain wordsSmoothing (deck 06) averages neighbours, so differences between pixels shrink and the image blurs. Sharpening does the opposite: it measures how fast the brightness changes and boosts those changes. Edges and fine details are places where brightness changes fast, so they get stronger.

Slide 3. Smoothing filters remove fine details to blur the image. Sharpening spatial filters seek to highlight fine details:

Slide 4. Applications: photo enhancement, medical image visualization, industrial defect detection, electronic printing, autonomous guidance in military systems.

Slide 5. Image derivatives give information about spatial variations in an image by calculating the rate of change of pixel intensity. They are fundamental to image analysis, enhancement and feature extraction.

Slide 6. Sharpening filters are based on first- and second-order derivatives. Differentiation measures the rate of change of a function, so sharpening is based on spatial differentiation. For a digital image, derivatives are defined in terms of differences between neighbouring pixels.

SP23 Q2(f)i · SP25 Q1(a) first part2 marks

What are the objectives of sharpening spatial filters?

Sharpening spatial filters highlight fine detail in an image. Their objectives are: (1) to remove blurring from images; (2) to highlight edges; (3) to emphasize transitions (sudden changes) in image intensity. They work by spatial differentiation (first- or second-order derivatives defined by differences), which responds strongly where intensity changes and gives zero in flat areas. Uses: photo enhancement, medical visualization, industrial defect detection, electronic printing, military guidance.

Simulator: any image, any sharpening filter

slides 19–53Came in past papers
In plain wordsPick an image and a filter, then press Next step. Each pixel takes 4 steps: place the window, multiply, add (and for sharpening, add the result back to f), write. Gradient filters show Gx and Gy side by side. ← → keys step; click a pixel to jump; Play runs it. Smaller simulators sit next to each method below.

First and second derivatives

Must studyslides 7–18Came in past papers
In plain wordsThe 1st derivative asks: how much does the brightness change from this pixel to the next? The 2nd derivative asks: is that change itself changing, i.e. is the slope bending? On a constant slope (a ramp), the 1st derivative is constant and the 2nd is zero, except where the ramp starts and stops.

1st derivative (slide 7)

The difference of two consecutive pixels:

∂f/∂x = f(x+1) − f(x)  (forward)  ·  f(x) − f(x−1)  (backward)  ·  f(x+1) − f(x−1)  (central)

It is just the difference between subsequent values, and it measures the rate of change of the function.

Slides 10–12, as 2-D kernels (horizontal one row, vertical one column):

typeformula (x direction)horizontal kernelformula (y direction)vertical kernel
forwardf(x+1,y) − f(x,y)[0 −1 1]f(x,y+1) − f(x,y)[0; −1; 1]
backwardf(x,y) − f(x−1,y)[−1 1 0]f(x,y) − f(x,y−1)[−1; 1; 0]
centralf(x+1,y) − f(x−1,y)[−1 0 1]f(x,y+1) − f(x,y−1)[−1; 0; 1]

2nd derivative (slide 13)

It takes into account the values both before and after the current one: {f(x+1) − f(x)} − {f(x) − f(x−1)}.

∂²f/∂x² = f(x+1) + f(x−1) − 2f(x)    or    2f(x) − f(x−1) − f(x+1)

Slides 15–16, as kernels: forward (positive) [1 −2 1] (and vertically [1; −2; 1]); backward (negative) [−1 2 −1] (and [−1; 2; −1]).

Slides 8–9, 14 · the image stripworked

Gray levels along the line A–B of the slide image, with both derivatives worked out (forward 1st difference, central 2nd difference):

x0123456789101112131415161718192021222324
f(x)5543210006000013100007777
1st: f(x+1) − f(x)-1-1-1-1-1006-600012-2-10007000
2nd: f(x+1) + f(x−1) − 2f(x)-10000106-1260011-411007-700
048121620240367xvalue
Image strip A–B: ramp (x 0–6), isolated point (x 9), thin line (x 14–16), flat segment, step (x 21).
04812162024-606xvalue
1st derivative: nonzero along the entire ramp, zero in flat areas, a single response at the step.
04812162024-12-606xvalue
2nd derivative: nonzero only at the onset and end of the ramp, a double response (+, −) at the point and the step.

Example cells: at x = 9 (the isolated point 6), the 1st derivative is f(10) − f(9) = 0 − 6 = −6 and the 2nd derivative is f(10) + f(8) − 2f(9) = 0 + 0 − 12 = −12.

Try it, step by step: both derivatives along the whole strip, one position at a time. Switch the Operation menu for the 2nd derivative.

SP24 Q1(b)3 + 2 marks

Define the first-order and second-order discrete derivatives of a 1-D digital function f(x). What relation can be obtained between first- and second-order derivatives for an edge that is a ramp of constant slope?

First-order derivative (difference of consecutive pixels): ∂f/∂x = f(x+1) − f(x).

Second-order derivative (difference of differences): ∂²f/∂x² = [f(x+1) − f(x)] − [f(x) − f(x−1)] = f(x+1) + f(x−1) − 2f(x).

Relation along a ramp of constant slope (strip values 5 5 4 3 2 1 0 0):

  • The 1st derivative is nonzero along the entire ramp (−1 at every step of the ramp) and zero in flat areas, so it produces thick edges.
  • The 2nd derivative is zero along the ramp and nonzero only at its onset and end, with opposite signs (−1 at the start, +1 at the end). So it produces a double edge one pixel thick, separated by zeros, and the sign change (zero crossing) marks the edge location.
  • The 2nd derivative also responds much more strongly to fine detail such as an isolated point (6, −12, 6 against −6 for the 1st), which is why it's preferred for sharpening.

[Draw the strip and the two derivative graphs above.]

Slide 17. Two families of sharpening filters: (1) Laplacian: uses the 2nd derivative; (2) Sobel / Prewitt (gradient operators): use the 1st derivative.

Slide 18. The 2nd derivative is more useful for image enhancement than the 1st derivative: stronger response to fine detail. The first sharpening filter is the Laplacian.

The Laplacian: derivation and the four masks

slides 19–22Came in past papers
In plain wordsThe Laplacian is the 2nd derivative done in both directions at once (x and y) and added together. The result is one 3×3 mask: the centre pixel compared against its 4 (or 8) neighbours.
SP25 Q1(a)4 marks

What are the objectives of sharpening spatial filters? Derive the Laplacian operator using image derivatives and draw four Laplacian masks.

Objectives: highlight fine detail, remove blurring, highlight edges and emphasize intensity transitions (see above).

Derivation. The Laplacian of f(x,y) is the sum of the second derivatives in x and y:

∇²f = ∂²f/∂x² + ∂²f/∂y²

Using the forward 2nd differences:

∂²f/∂x² = f(x+1,y) + f(x−1,y) − 2f(x,y)

∂²f/∂y² = f(x,y+1) + f(x,y−1) − 2f(x,y)

Adding:

∇²f = f(x+1,y) + f(x−1,y) + f(x,y+1) + f(x,y−1) − 4f(x,y)

As masks: the horizontal [1 −2 1] plus the vertical [1; −2; 1] give

000
1-21
000
x part
010
0-20
010
y part
010
1-41
010
= positive Laplacian

This is the positive Laplacian, from forward derivatives. With the backward form 2f(x) − f(x−1) − f(x+1) we get ∇²f = 4f(x,y) − f(x−1,y) − f(x+1,y) − f(x,y−1) − f(x,y+1), the negative Laplacian:

0-10
-14-1
0-10
negative Laplacian

Four Laplacian masks (Figure 3.39): two 4-neighbour and two that include the diagonal neighbours:

010
1-41
010
(a) 4-nbr, centre −4
111
1-81
111
(b) 8-nbr, centre −8
0-10
-14-1
0-10
(c) 4-nbr, centre 4
-1-1-1
-18-1
-1-1-1
(d) 8-nbr, centre 8

In all cases the sum of the values in the filter is zero, so a flat area gives 0 and only changes produce a response.

Try it, step by step: the four Laplacian masks on the slide 26 image. Notice the positive and negative masks give the same number with opposite sign.

Slide 22. Applying the Laplacian to an image gives a new image that highlights edges and other discontinuities (moon picture). Flat regions become 0 (gray when scaled for display).

Sharpening with the Laplacian

Must studyslides 23–30Came in past papers

Slide 23. The result of Laplacian filtering is not an enhanced image; it is an edge map. To get the enhanced image, the Laplacian image is added to or subtracted from the original, depending on the sign of the mask centre w5:

g(x,y) =
f(x,y) − ∇²f
if w5 < 0 (positive Laplacian, centre −4)
f(x,y) + ∇²f
if w5 > 0 (negative Laplacian, centre +4)

Rule of thumb: the sign is chosen so that the centre pixel always ends up with a positive weight. Edges and fine detail are then much more obvious in the final sharpened image (slide 24).

Slide 26 · negative Laplacian on the pixel 154worked
0000000
01531571561531550
01591561581561590
01551581541561600
01541571581601600
01571571571561550
0000000
image; shaded = window, outlined = pixel 154

Step 1, filter: negative Laplacian [0 −1 0; −1 4 −1; 0 −1 0] at 154. Up = 158, left = 158, right = 156, down = 158.

∇²f = 4×154 − 158 − 158 − 156 − 158 = 616 − 630 = -14. This is the filtered value.

Step 2, sharpen: the mask centre is positive, so g = f + ∇²f = 154 + (-14) = 140.

The pixel was darker than its neighbours (154 against about 157) and became even darker (140), so the local contrast went up.

Try it, step by step: slide 26 sharpening: the two-step way (Laplacian, then add or subtract) and the one-pass masks. All give 140 at the 154.

One pass instead of two (slides 27–30)

The two steps can be combined into a single filtering operation:

g = f − ∇²f = f − [f(x+1,y) + f(x−1,y) + f(x,y+1) + f(x,y−1) − 4f(x,y)] = 5f(x,y) − f(x+1,y) − f(x−1,y) − f(x,y+1) − f(x,y−1)
0-10
-15-1
0-10
single-pass mask (4-nbr)
-1-1-1
-19-1
-1-1-1
single-pass variant (8-nbr)

Check on slide 26: 5×154 − 158 − 158 − 156 − 158 = 770 − 630 = 140, the same as the two-step answer.

The 8-neighbour variant comes the same way from [1 1 1; 1 −8 1; 1 1 1]: g = f − ∇²f gives centre 1 + 8 = 9 and −1 everywhere else.

SP23 Q3(f)iii2 marks

Derive a positive Laplacian filter mask that enhances an image in a single filtering operation.

Positive Laplacian: ∇²f = f(x+1,y) + f(x−1,y) + f(x,y+1) + f(x,y−1) − 4f(x,y), mask [0 1 0; 1 −4 1; 0 1 0]. Its centre is negative (w5 = −4), so the enhanced image is g = f − ∇²f:

g(x,y) = f(x,y) − [f(x+1,y) + f(x−1,y) + f(x,y+1) + f(x,y−1) − 4f(x,y)]

= 5f(x,y) − f(x+1,y) − f(x−1,y) − f(x,y+1) − f(x,y−1)

0-10
-15-1
0-10
single-pass enhancement mask

Convolving the image with this one mask gives the sharpened image directly. Its coefficients add to 1, so flat areas keep their brightness.

Gradient operators: Roberts, Sobel, Prewitt

Must studyslides 31–37Came in past papers
In plain wordsThe gradient uses the 1st derivative in two directions: gx (change going down the rows) and gy (change going across the columns). Its size M says how strong the edge is. In practice you add the two absolute values instead of taking a square root.

Slide 31. The gradient of f at (x,y) is the vector

∇f = grad(f) = [gx, gy] = [∂f/∂x, ∂f/∂y]
M(x,y) = mag(∇f) = √(gx² + gy²) ≈ |gx| + |gy|

Slide 32. Ways to compute it: 1-D forward differences, Roberts cross-gradient, 1-D central differences, Prewitt, Sobel.

Pixel names in a 3×3 window:

z1z2z3
z4z5z6
z7z8z9
pixel arrangement

Slide 33. 1-D forward differences: M(x,y) ≈ |z8 − z5| + |z6 − z5|.

Slide 34. Roberts cross-gradient: Gx = z9 − z5, Gy = z8 − z6; ∇f ≈ |z9 − z5| + |z8 − z6|. They are the simplest approximations, using 2×2 masks:

-10
01
Roberts Gx
0-1
10
Roberts Gy

Slide 35. Sobel: provides differencing and detects vertical and horizontal edges. Two 3×3 kernels; Gy is Gx rotated by 90°. The centre row/column has weight 2, which also smooths a little.

-1-2-1
000
121
∂f/∂y = Sobel Gy (slide: extract vertical edges)
-101
-202
-101
∂f/∂x = Sobel Gx (slide: extract horizontal edges)
|G| = |Gx| + |Gy| ≈ |(z7 + 2z8 + z9) − (z1 + 2z2 + z3)| + |(z3 + 2z6 + z9) − (z1 + 2z4 + z7)|

Slide 36. Prewitt: used for detecting edges horizontally and vertically. Same as Sobel with weight 1 instead of 2.

-1-1-1
000
111
∂f/∂y = Prewitt Gy (vertical edges)
-101
-101
-101
∂f/∂x = Prewitt Gx (horizontal edges)
∇f ≈ |(z7 + z8 + z9) − (z1 + z2 + z3)| + |(z3 + z6 + z9) − (z1 + z4 + z7)|

All gradient masks add up to 0: a flat area gives no response.

Try it, step by step: Gx, Gy and |G| for Sobel, Prewitt and Roberts, step by step.

Slides 37–41. Examples: coins (edges along x and along y), a contact lens enhanced with Sobel so that defects become obvious ("Sobel filters are typically used for edge detection"), and Einstein: Gy gives the vertical edges, Gx the horizontal edges (absolute values).

Slide 42. Laplacian vs Sobel on a chessboard: the Laplacian gives thin, double edges and also picks up fine texture and noise; Sobel gives thicker, stronger edges along the board lines.

Difference (emboss) filters and unsharp masking

slides 43–48On study list

Slides 43–44. Difference filters, also called emboss filters, enhance the details in the direction specific to the mask selected. The four primary masks, for edges in the vertical, horizontal and two diagonal directions:

010
010
0-10
horizontal
000
11-1
000
vertical
100
010
00-1
diagonal 1
001
010
-100
diagonal 2

The filtered image looks like a gray relief (embossed). Added back to the original, it gives a contrast-enhanced picture (Jimi Hendrix poster).

In plain wordsUnsharp masking sounds backwards: you sharpen by using a blurred copy. Blur the image, subtract the blur from the original, and what's left is only the detail (edges). Add that detail back on top of the original and the edges stand out more.

Slide 45. Unsharp masking is a classic sharpening technique. It sharpens by subtracting a blurred version from the original image. Steps:

  1. Blur the image (usually with a Gaussian filter).
  2. Subtract the blurred image from the original: this isolates the high-frequency details (edges).
  3. Scale the difference by a factor (called the amount).
  4. Add it back to the original image: edges and fine details become more pronounced.
gmask = f − f̄     g = f + k · gmask

k = 1: unsharp masking. k > 1: high-boost filtering (Figure 3.40e, an even stronger result).

Slide 46. The 1-D demo (Figure 3.39), with real numbers

pixel0123456789101112
(a) original10101010103050709090909090
(b) blurred10101010173050708390909090
(c) mask = a − b0000-700070000
(d) sharpened = a + c1010101033050709790909090
high-boost, k = 210101010-430507010490909090
0481205090pixelvalueoriginalblurred
(a) original edge and (b) its blurred version (3-point average)
04812-707pixelvalue
(c) unsharp mask = original − blurred: a dip before the edge and a bump after it
0481205090pixelvalueoriginalsharpened
(d) sharpened = original + mask: undershoot before and overshoot after the edge, so the edge looks crisper

Try it, step by step: unsharp masking on every pixel: blur, mask = original − blur, sharpened = original + mask.

Slide 47. Applications of unsharp masking: document scanning (enhances faint text), medical imaging (highlights subtle tissue boundaries), photography (improves clarity and detail), satellite imaging (sharpens roads and rivers).

Slide 48. DIP-XE example: (a) original, (b) blurred with a Gaussian, (c) unsharp mask, (d) unsharp masking result, (e) high-boost result.

Combining enhancement methods (bone scan)

slides 49–52Know in 2 lines

Slide 49. Successful enhancement is usually not achieved with a single operation; we combine a range of techniques. The example enhances a whole-body bone scan:

  1. (a) original bone scan;
  2. (b) Laplacian of (a);
  3. (c) sharpened image, combining (a) and (b);
  4. (d) Sobel gradient of (a);
  5. (e) (d) smoothed with a 5×5 averaging filter;
  6. (f) the product of (c) and (e), used as a mask: strong edges kept, noise in flat areas suppressed;
  7. (g) sharpened image, the sum of (a) and (f);
  8. (h) power-law (gamma) transformation applied to (g), to bring out the dark detail.

Why mix them: the Laplacian brings out fine detail but also noise; the smoothed Sobel image is strong at real edges and weak at noise, so their product keeps detail only where there are edges. Gamma finally expands the dark range. Slide 52 compares the original and final images.

Past questions and class work, solved in full

slides 19–53Came in past papers

Coordinates: (row, column) from 0 at the top-left. Write the window, the mask, every product, and the sum.

Slide 53 · Class workall parts

3-bit 4×4 image. Find the filtered output using the negative Laplacian, a 3×3 mean, a 3×3 median, a 3×3 Sobel and a 3×3 Prewitt operator. Ignore the border pixels and put zero in the output border.

0267
1164
4527
1260
input image

Only the four inner pixels (shaded) have a full 3×3 window.

(1) Negative Laplacian [0 −1 0; −1 4 −1; 0 −1 0]: 4×centre − up − down − left − right.

pixelworkingvalue
(1,1)4×1 − 2 − 5 − 1 − 6-10
(1,2)4×6 − 6 − 2 − 1 − 411
(2,1)4×5 − 1 − 2 − 4 − 211
(2,2)4×2 − 6 − 6 − 5 − 7-16

(2) 3×3 mean

pixelsum of window÷ 9rounded
(1,1)273.003
(1,2)404.444
(2,1)283.113
(2,2)333.674

(3) 3×3 median

pixelsorted windowmedian
(1,1)0, 1, 1, 2, 2, 4, 5, 6, 62
(1,2)1, 2, 2, 4, 5, 6, 6, 7, 75
(2,1)1, 1, 1, 2, 2, 4, 5, 6, 62
(2,2)0, 1, 2, 2, 4, 5, 6, 6, 74

(4) Sobel: Gx = (z3 + 2z6 + z9) − (z1 + 2z4 + z7), Gy = (z7 + 2z8 + z9) − (z1 + 2z2 + z3), |G| = |Gx| + |Gy|.

pixelGxGy|G|
(1,1)14620
(1,2)13-518
(2,1)628
(2,2)5-38

(5) Prewitt: the same with weights 1.

pixelGxGy|G|
(1,1)9312
(1,2)10-111
(2,1)819
(2,2)3-36

Output images (border = 0):

0000
0-10110
011-160
0000
negative Laplacian
0000
0340
0340
0000
mean
0000
0250
0240
0000
median
0000
020180
0880
0000
Sobel |G|
0000
012110
0960
0000
Prewitt |G|

Worked Sobel at (1,1): window [0 2 6; 1 1 6; 4 5 2]. Gx = (6 + 2×6 + 2) − (0 + 2×1 + 4) = 20 − 6 = 14. Gy = (4 + 2×5 + 2) − (0 + 2×2 + 6) = 16 − 10 = 6. |G| = 20.

Try it, step by step: the slide 53 class work: switch filters, the border is ignored as the question says.

SP22 Q6(b)i4 marks

3×3 image [3 7 6; 2 5 8; 3 4 7]. Find the Sobel gradient-magnitude image and the edge direction of the central pixel.

Centre pixel: z1…z9 = 3, 7, 6, 2, 5, 8, 3, 4, 7.

Gx = (z3 + 2z6 + z9) − (z1 + 2z4 + z7) = (6 + 16 + 7) − (3 + 4 + 3) = 29 − 10 = 19

Gy = (z7 + 2z8 + z9) − (z1 + 2z2 + z3) = (3 + 8 + 7) − (3 + 14 + 6) = 18 − 23 = -5

|∇f| ≈ |Gx| + |Gy| = 19 + 5 = 24

Direction (deck 08 slide 23): gradient angle α = tan⁻¹(Gy/Gx) = tan⁻¹(-5/19) = -14.74°. The edge runs perpendicular to the gradient: edge direction = α − 90° = -104.74°.

Whole magnitude image with zero padding (same formulas at every pixel):

376
258
347
input
1912-19
2119-21
1314-13
Gx
92021
-3-5-1
-9-20-21
Gy
283240
242422
223434
|Gx| + |Gy|

Try it, step by step: SP22: the full Sobel magnitude image with zero padding.

SP25 Q2(d)i2 marks

3×3 patch [10 10 10; 10 50 10; 10 10 10]. Apply the Sobel operator in the horizontal direction. What is the gradient value at the centre pixel?

101010
105010
101010
patch
-101
-202
-101
Sobel Gx

Gx = (z3 + 2z6 + z9) − (z1 + 2z4 + z7) = (10 + 20 + 10) − (10 + 20 + 10) = 40 − 40 = 0.

(With the other mask, Gy = (10 + 20 + 10) − (10 + 20 + 10) = 0 too.) The gradient is 0: the left and right columns are equal, so there is no horizontal change across the centre. The Sobel masks have a 0 centre weight, so the bright point 50 itself is not seen. A Laplacian would respond strongly: 4×50 − 4×10 = 160.

Try it, step by step: SP25 (d)(i): Sobel gives 0 at the centre; the Laplacian does not.

F21 B4(a)iii2 marks
000000
071050
063620
064410
015700
000000
window at (1,1)
710
636
644
window
010
1-41
010
positive Laplacian

Σ w·z = (1)(1) + (1)(6) + (-4)(3) + (1)(6) + (1)(4) = 5

Laplacian response at (1,1) = 5.

Try it, step by step: F21 (iii) at (1,1).

SP25 Q2(b)ii Task 2 · negative Laplacian at (3,2)2 marks
000000000
077777770
077727770
075000570
051101150
055000550
033323330
077777770
000000000
window at (3,2)
500
110
500
window
0-10
-14-1
0-10
negative Laplacian

Σ w·z = (-1)(0) + (-1)(1) + (4)(1) + (-1)(0) + (-1)(0) = 3

Negative Laplacian response at (3,2) = 3.

Try it, step by step: SP25 Task 2 at (3,2).

SP24 Q2(b)ii Task 2 · positive Laplacian at (3,3)2 marks
000000000
077777770
077727770
075000570
051101150
055000550
033323330
077777770
000000000
window at (3,3)
000
101
000
window
010
1-41
010
positive Laplacian

Σ w·z = (1)(0) + (1)(1) + (-4)(0) + (1)(1) + (1)(0) = 2

Positive Laplacian response at (3,3) = 2.

Try it, step by step: SP24 Task 2 at (3,3).

The smoothing parts of these mixed questions (max, mean, median, the 4-neighbour mask) are on the deck 06 page.

Traps and one-minute sheet

slides all
operatormaskderivativeuse
positive Laplacian[0 1 0; 1 −4 1; 0 1 0]2ndedge/detail map
negative Laplacian[0 −1 0; −1 4 −1; 0 −1 0]2ndedge/detail map
single-pass sharpen[0 −1 0; −1 5 −1; 0 −1 0]2ndsharpened image in one step
8-nbr single pass[−1 −1 −1; −1 9 −1; −1 −1 −1]2ndstronger sharpening
Roberts[−1 0; 0 1], [0 −1; 1 0]1stsimplest gradient
SobelGy [−1 −2 −1; 0 0 0; 1 2 1], Gx its 90° turn1stedge detection
Prewittweights 1 instead of 21stedge detection
unsharp / high-boostg = f + k(f − f̄)—k = 1 unsharp, k > 1 high-boost