Sharpening & edge basics
Smoothing averages. Sharpening differentiates. This page builds sharpening from first and second derivatives (Laplacian, unsharp masking, high-boost). Then it uses the same derivative idea to detect edges (gradient, Sobel, Prewitt, Roberts, Laplacian of Gaussian). Each method has a worked numerical.
Sharpening (high-pass) filters
Sharpening highlights transitions in intensity. It is used in printing, medical imaging, industrial inspection and autonomous guidance.
Smoothing is like integration (sum and average the neighbours). Sharpening is like spatial differentiation (measure how fast intensity changes). A derivative is large where intensity jumps and zero where it is flat. So differentiation enhances edges and other discontinuities (including noise). It de-emphasises regions with slowly varying intensity.
Key fact: the response of a derivative operator is proportional to the size of the intensity discontinuity at that point. A big jump gives a big response.
Discrete derivatives
An image is discrete, so a derivative is a difference between neighbouring pixels.
$f(x)$ is the intensity along a 1D scan line. The first difference compares a pixel with its next neighbour. The second difference is the difference of differences. It compares a pixel with the average of its two neighbours. For a 2D image $f(x,y)$, use partial derivatives along the two axes.
Behaviour of the derivatives
| Feature | First derivative | Second derivative |
|---|---|---|
| Constant intensity | Zero | Zero |
| Onset of a step or ramp | Non-zero | Non-zero |
| End of a step or ramp | - | Non-zero (opposite sign) |
| Along a ramp | Non-zero (constant) | Zero |
| Edges produced | Thick | Thin, double response with zero crossing |
| Fine detail, isolated points, noise | Weaker response | Stronger response |
Slide example. A 1D scan line has constant regions, a ramp down from 6 to 1 and a step up from 1 to 6.
| x | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| f | 6 | 6 | 6 | 6 | 5 | 4 | 3 | 2 | 1 | 1 | 1 | 1 | 1 | 1 | 6 | 6 | 6 | 6 |
| 1st | 0 | 0 | 0 | -1 | -1 | -1 | -1 | -1 | 0 | 0 | 0 | 0 | 0 | 5 | 0 | 0 | 0 | 0 |
| 2nd | 0 | 0 | 0 | -1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 5 | -5 | 0 | 0 | 0 |
Compute with $f'(x)=f(x+1)-f(x)$ and $f''(x)=f(x+1)+f(x-1)-2f(x)$ (equivalently $f''(x)=f'(x)-f'(x-1)$). At $x=13$: $f'=6-1=5$ and $f''=f(14)+f(12)-2f(13)=6+1-2=5$. At $x=14$: $f''=6+1-12=-5$. The slide's printed row is shifted by one position, because it uses a forward or backward difference convention. The pattern is the same.
Read-off: on the ramp, the first derivative is a constant $-1$ along the whole ramp (a thick edge). The second derivative is non-zero only at the start ($-1$) and the end ($+1$). At the step, the first derivative is a single large spike (5). The second derivative gives $+5$ then $-5$, a double edge. The sign change between them is the zero crossing.
A typical exam question asks: "Which derivative gives a thicker edge? Which responds more to fine detail and noise? Which has a zero crossing?" First derivative: thick edges. Second derivative: thin double edges, stronger response to fine detail and points (and noise), and a zero crossing at the edge.
Gradient and Laplacian
$\nabla$ (nabla) is the vector of partial derivative operators. The Laplacian is $\nabla\cdot\nabla$: the second derivatives in both directions added together. It is isotropic. Rotating the image rotates the response, so one mask finds edges in all directions.
The slide labels the gradient "linear operator" and the Laplacian "non-linear". Strictly, the discrete Laplacian is a linear filter (a weighted sum). The gradient magnitude $\sqrt{g_x^2+g_y^2}$ is the non-linear part. In an exam, use the slide labels. Know the strict answer in case a question asks about linearity.
Discrete Laplacian
Add the two 1D masks $[1,-2,1]$ (horizontal and vertical). The sum is the 4-neighbour Laplacian kernel.
| 0 | 1 | 0 |
| 1 | -4 | 1 |
| 0 | 1 | 0 |
The slide shows four standard Laplacian masks. These are the 4-neighbour mask above, the 8-neighbour mask below (it also includes the diagonals), and the negatives of both (positive centre).
| 1 | 1 | 1 |
| 1 | -8 | 1 |
| 1 | 1 | 1 |
| 0 | -1 | 0 |
| -1 | 4 | -1 |
| 0 | -1 | 0 |
| -1 | -1 | -1 |
| -1 | 8 | -1 |
| -1 | -1 | -1 |
The coefficients of every Laplacian mask sum to zero, so flat regions give zero output.
Sharpening with the Laplacian
The Laplacian alone shows only edges (on a dark background). To sharpen, add it to the original or subtract it from the original. This keeps the original content:
The sign depends on the mask. With a $-4$ or $-8$ centre, subtract. With a $+4$ or $+8$ centre, add. Both give the same result. For the 4-neighbour mask, the result equals one mask with centre 5 and four neighbours $-1$.
Worked example (extra practice). A vertical step edge. Apply the $-4$ mask at the two pixels next to the edge:
| 10 | 10 | 50 | 50 |
| 10 | 10 | 50 | 50 |
| 10 | 10 | 50 | 50 |
Dark side, pixel $(1,1)=10$: $\nabla^2 f=10+10+10+50-4(10)=80-40=+40$. Sharpened $g=f-\nabla^2 f=10-40=-30\to$ clip to 0 (darker).
Bright side, pixel $(1,2)=50$: $\nabla^2 f=50+50+10+50-4(50)=160-200=-40$. Sharpened $g=50-(-40)=90$ (brighter).
The dark side gets darker and the bright side gets brighter. The edge gets steeper (undershoot and overshoot). Flat areas do not change (the interior of a flat region gives $\nabla^2 f=0$, so $g=f$).
Also check a single bright dot with neighbours 5 and centre 9: $\nabla^2 f=4\times5-4\times9=-16$ and $g=9+16=25$. The filter amplifies isolated points strongly. This is why noise gets worse.
Unsharp masking
This is an old darkroom technique. Subtract a blurred ("unsharp") copy from the original to get the fine detail. Then add that detail back.
$f$ = original. $\bar f$ (written $f'$ on the slide) = blurred original (for example a Gaussian or box blur). Mask = what the blur removed, which is edges and detail. $g$ = sharpened result.
Steps: (1) read the original; (2) blur it; (3) mask = original minus blurred; (4) add the mask to the original. In the 1D figure on the slide, a ramp edge is blurred. The mask has a negative dip before the edge and a positive bump after it. Adding the mask back gives a sharper edge with undershoot and overshoot.
High-boost filtering
High-boost filtering is unsharp masking with the mask weighted by a factor $k$.
$k=1$ is unsharp masking. $k>1$ is high-boost (it emphasises the high frequencies more strongly). $k\lt1$ de-emphasises them. The slide states $k>1$ for high-boost.
Worked example (extra practice, 1D). The row is $f=10\ 10\ 10\ 50\ 50\ 50$. Blur with a 3-point average at the inner pixels:
| x | 1 | 2 | 3 | 4 |
| $f$ | 10 | 10 | 50 | 50 |
| blur $\bar f$ | 10 | 23.3 | 36.7 | 50 |
| Mask $=f-\bar f$ | 0 | -13.3 | 13.3 | 0 |
| Unsharp ($k=1$) | 10 | -3.3 $\to$ 0 | 63.3 | 50 |
| High-boost ($k=2$) | 10 | -16.7 $\to$ 0 | 76.7 | 50 |
Blur at $x=2$: $(10+10+50)/3=23.3$. Blur at $x=3$: $(10+50+50)/3=36.7$. Clip the results to $[0,255]$. The step now overshoots: the bright side goes above 50 and the dark side goes below 10 (clipped to 0). A bigger $k$ makes this stronger.
| Method | Formula | Note |
|---|---|---|
| Laplacian sharpening | $g=f-\nabla^2f$ (negative-centre mask) | Second derivative, isotropic, noisy |
| Unsharp masking | $g=f+(f-\bar f)$ | Original plus detail, $k=1$ |
| High-boost | $g=f+k(f-\bar f)$ | $k>1$ boosts edges more |
Edge detection
Edge detection finds the boundaries of objects in an image. Edges appear where pixel intensity changes suddenly. Detection algorithms highlight these discontinuities and give an edge map. Later stages use the edge map for object counting, measurement, feature extraction and segmentation.
There are two families of algorithm:
- Gradient-based methods find edges at the peaks of the first derivative of intensity (Sobel, Prewitt, Roberts).
- Gaussian-based methods smooth first. Then they use second-order derivatives (Laplacian of Gaussian).
Edge models
| Model | Profile | Derivative behaviour |
|---|---|---|
| Step edge | Abrupt change from one value to another in a single step | 1st: single spike. 2nd: +/- pair with a zero crossing |
| Ramp edge | Gradual transition over some distance | 1st: constant plateau along the ramp. 2nd: only at start and end |
| Roof edge | A peak or ridge: intensity rises to a maximum then falls (a thin line) | Needs a derivative of both signs at the peak |
Gradient-based edge detection
$g_x,g_y$ = derivatives along $x$ and $y$. $M$ = gradient magnitude (edge strength, rate of change). $\theta$ = gradient direction, which is perpendicular to the edge. The sum of absolute values is a cheaper approximation to the square root.
The general recipe has three steps: convolve with a pair of kernels $G_x,G_y$, compute $M$, and threshold the magnitude to decide edge or not edge.
Sobel
Sobel is gradient-based. It uses two 3x3 convolution kernels to get the gradient magnitude and direction at each pixel. The weight 2 on the centre row and column gives extra smoothing along the edge direction. This makes Sobel less noise-sensitive than Prewitt.
| -1 | 0 | 1 |
| -2 | 0 | 2 |
| -1 | 0 | 1 |
| -1 | -2 | -1 |
| 0 | 0 | 0 |
| 1 | 2 | 1 |
The first kernel is $G_x$ (changes in the x direction). The second is $G_y$ (changes in the y direction). The slide calls $G_x$ the "horizontal kernel detecting horizontal edges". Be careful: $G_x$ measures horizontal change in intensity, so it responds to vertical edges. $G_y$ responds to horizontal edges. Both conventions appear, so state which one you use.
Pipeline from the slide:
- Load the image.
- Convert it to grayscale.
- Smooth it with a Gaussian (optional).
- Apply the Sobel operator.
- Compute the gradient magnitude.
- Threshold: pixels above the threshold are edges.
- Normalise.
Worked example 1 (vertical edge). 3x3 neighbourhood:
| 10 | 10 | 50 |
| 10 | 10 | 50 |
| 10 | 10 | 50 |
$G_x=(-1)(10)+0+1(50)+(-2)(10)+0+2(50)+(-1)(10)+0+1(50)=40+80+40=160$.
$G_y=(-1)(10)+(-2)(10)+(-1)(50)+(1)(10)+(2)(10)+(1)(50)=-80+80=0$.
$M=\sqrt{160^2+0^2}=160$ and $\theta=\tan^{-1}(0/160)=0^\circ$. The gradient points along $+x$ and the edge runs vertically. With threshold 100, this pixel is an edge.
Worked example 2 (general patch).
| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 9 |
$G_x=(3-1)+2(6-4)+(9-7)=2+4+2=8$. $G_y=(7+16+9)-(1+4+3)=32-8=24$.
$M=\sqrt{8^2+24^2}=\sqrt{640}=25.3$; approximation $|G_x|+|G_y|=32$
$\theta=\tan^{-1}(24/8)=71.6^\circ$.
Prewitt
Prewitt has the same idea as Sobel but uses equal weights (plain averaging across the three rows or columns). It is simpler but slightly more noise-sensitive. Pipeline: load, grayscale, apply the Prewitt operator, gradient magnitude, threshold, normalise.
| -1 | 0 | 1 |
| -1 | 0 | 1 |
| -1 | 0 | 1 |
| -1 | -1 | -1 |
| 0 | 0 | 0 |
| 1 | 1 | 1 |
Worked example. Same 1-9 patch: $G_x=(3-1)+(6-4)+(9-7)=6$
$G_y=(7+8+9)-(1+2+3)=18$
$M=\sqrt{36+324}=\sqrt{360}=18.97$. Sobel gave $G_x=8$ and $G_y=24$ on the same patch. Sobel weights the centre row and column twice, so its outputs are larger.
Roberts cross
Roberts cross uses tiny 2x2 kernels. They measure the difference along the two diagonals. It is efficient and good for thin, prominent edges. It is very noise-sensitive. It also gives a half-pixel shift, because the kernel has no centre pixel.
| 1 | 0 |
| 0 | -1 |
| 0 | 1 |
| -1 | 0 |
Worked example. 2x2 window $\begin{bmatrix}a&b\\c&d\end{bmatrix}=\begin{bmatrix}10&50\\10&50\end{bmatrix}$. $G_x=a-d=10-50=-40$, $G_y=b-c=50-10=40$. $M=\sqrt{1600+1600}=56.6$, or $|G_x|+|G_y|=80$.
Operator comparison
| Roberts | Prewitt | Sobel | |
|---|---|---|---|
| Kernel size | 2x2 | 3x3 | 3x3 |
| Weights | +1/-1 on diagonals | Equal (1,1,1) | Centre weighted (1,2,1) |
| Noise sensitivity | Highest | Moderate | Lowest (built-in smoothing) |
| Edge thickness / localisation | Thin, sharp, half-pixel shift | Thicker | Thicker |
| Measures | Diagonal differences | Horizontal / vertical | Horizontal / vertical |
| Cost | Cheapest | Low | Low |
All gradient kernels sum to zero (flat regions give zero response). Derivative kernels are antisymmetric. Smoothing kernels are symmetric. Sobel = smoothing $[1,2,1]^T$ in one direction times derivative $[-1,0,1]$ in the other.
Gaussian-based edge detection (Laplacian of Gaussian)
The Laplacian alone is very sensitive to noise, because second derivatives amplify noise. So smooth with a Gaussian first. Then apply the Laplacian. Find edges at the zero crossings of the second derivative. Convolution is linear, so smoothing then the Laplacian equals one convolution with the Laplacian of Gaussian (LoG) kernel.
$G$ is the Gaussian with standard deviation $\sigma$. A larger $\sigma$ detects coarser edges but localises them less precisely. The shape is the "Mexican hat": negative centre, positive ring.
Steps:
- Smooth the image with a Gaussian.
- Apply a Laplacian mask.
- Find the zero crossings (sign changes between neighbouring pixels). Use a strength threshold to ignore weak ones.
A commonly used 5x5 LoG approximation:
| 0 | 0 | -1 | 0 | 0 |
| 0 | -1 | -2 | -1 | 0 |
| -1 | -2 | 16 | -2 | -1 |
| 0 | -1 | -2 | -1 | 0 |
| 0 | 0 | -1 | 0 | 0 |
Zero-crossing example. Second-derivative values along a line: $0,0,5,-5,0,0$. The sign changes from $+5$ to $-5$ between positions 3 and 4, so the edge is there. This is the step edge from the scan-line example. LoG gives closed, thin contours. It responds to noise less than the raw Laplacian.
Gradient-based vs. LoG
| Gradient (Sobel/Prewitt/Roberts) | LoG | |
|---|---|---|
| Derivative used | First | Second (after Gaussian) |
| Edge located at | Peak of gradient magnitude | Zero crossing |
| Gives direction? | Yes ($\theta$) | No (isotropic) |
| Edge thickness | Thick, needs thresholding | Thin, closed contours |
| Noise handling | Optional smoothing | Built-in Gaussian smoothing |
| Cost | Two small convolutions | Larger kernel |
Quick self-test
1. Why is sharpening analogous to differentiation and smoothing to integration?
Smoothing sums or averages the neighbours, which is like integration. Sharpening measures local intensity change (differences), which is like a derivative. Derivatives are large at discontinuities and zero in flat areas. So differentiation emphasises edges and noise.
2. Write the discrete first and second derivatives. Compute both for the line 3 3 3 9 9 at the pixel with value 3 next to the jump.
$f'=f(x+1)-f(x)$, $f''=f(x+1)+f(x-1)-2f(x)$. At the third pixel (value 3, next is 9, previous 3): $f'=9-3=6$ and $f''=9+3-6=6$. At the next pixel (value 9): $f'=9-9=0$ and $f''=9+3-18=-6$. The second derivative shows $+6$ then $-6$, with a zero crossing.
3. Compare first and second derivative responses to a ramp, a step and an isolated point.
Ramp: the 1st derivative is constant and non-zero along it. The 2nd is non-zero only at the onset and end. Step: the 1st is a single spike. The 2nd is a +/- pair with a zero crossing. Isolated point: the 2nd gives a much stronger response than the 1st. The 1st derivative produces thick edges. The 2nd produces thin edges.
4. Apply the 4-neighbour Laplacian ($-4$ centre) and the sharpening rule to centre 20 with neighbours 10, 10, 10, 10.
$\nabla^2f=40-80=-40$. The centre coefficient is negative, so $g=f-\nabla^2f=20+40=60$.
5. Explain unsharp masking and high-boost filtering. What changes with $k$?
Mask $=f-\bar f$ (original minus blurred). Unsharp masking: $g=f+\text{Mask}$. High-boost: $g=f+k\,\text{Mask}$ with $k>1$, which gives stronger edge enhancement. A larger $k$ gives more overshoot and more noise amplification.
6. The row is $f=20\ 20\ 80\ 80$. Its 3-point blur at the two middle pixels is $40,\ 60$. Find the unsharp result at those pixels.
Mask $=20-40=-20$ and $80-60=20$. $g=20-20=0$ and $80+20=100$.
7. Compute the Sobel $G_x$, $G_y$, magnitude for the patch rows (1 1 1), (1 1 1), (9 9 9).
$G_x=0$ (all columns equal). $G_y=(9+18+9)-(1+2+1)=36-4=32$. $M=32$, direction $90^\circ$: a horizontal edge.
8. Differences between Sobel, Prewitt and Roberts.
Roberts: 2x2, diagonal differences, fast, noisy, thin edges. Prewitt: 3x3, equal weights. Sobel: 3x3, centre weight 2, extra smoothing, so it is less noise-sensitive. All three are first-derivative (gradient-based) and their kernels sum to zero.
9. Why does LoG smooth first, and how are edges located?
Second derivatives amplify noise, so a Gaussian smooths first (smoothing plus Laplacian equals convolution with $\nabla^2G$). Edges are at the zero crossings of the result, where it changes sign.
10. Define step, ramp and roof edges, and give the formula for gradient magnitude and its cheap approximation.
Step: an abrupt change in a single step. Ramp: a gradual change over a distance. Roof: a peak or ridge. $M=\sqrt{g_x^2+g_y^2}\approx|g_x|+|g_y|$.
- Sharpening highlights intensity transitions with spatial differentiation. The derivative response is proportional to the size of the discontinuity.
- $f'=f(x+1)-f(x)$, $f''=f(x+1)+f(x-1)-2f(x)$. The 1st derivative is non-zero along ramps and gives thick edges. The 2nd is non-zero only at ramp ends. It gives a double response at a step with a zero crossing. It is stronger on fine detail and noise.
- Laplacian: $\nabla^2f=f(x{\pm}1,y)+f(x,y{\pm}1)-4f$. The mask centre is $-4$/$-8$ or $+4$/$+8$. Sharpen with $g=f\mp\nabla^2 f$, and choose the sign from the centre coefficient.
- Unsharp masking: $g=f+(f-\bar f)$. High-boost: $g=f+k(f-\bar f)$, $k>1$.
- Edges are sudden intensity changes. The edge models are step, ramp and roof. Gradient methods find the peak of the first derivative. Gaussian methods smooth first, then find the zero crossings of the second derivative.
- $\nabla f=[g_x,g_y]^T$, $M=\sqrt{g_x^2+g_y^2}\approx|g_x|+|g_y|$, $\theta=\tan^{-1}(g_y/g_x)$.
- Sobel has (1,2,1) weights and is the least noise-sensitive. Prewitt has equal weights. Roberts is 2x2 and diagonal. The pipeline is grayscale, optional Gaussian smoothing, kernels, magnitude, threshold, normalise.
- LoG = Gaussian smoothing plus Laplacian. Edges are at zero crossings. The contours are thin and closed. LoG is robust to noise but gives no direction.