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.

$$ \frac{\partial f}{\partial x}=f(x+1)-f(x) $$ $$ \frac{\partial^2 f}{\partial x^2}=f(x+1)+f(x-1)-2f(x) $$

$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

FeatureFirst derivativeSecond derivative
Constant intensityZeroZero
Onset of a step or rampNon-zeroNon-zero
End of a step or ramp-Non-zero (opposite sign)
Along a rampNon-zero (constant)Zero
Edges producedThickThin, double response with zero crossing
Fine detail, isolated points, noiseWeaker responseStronger 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.

x01234567891011121314151617
f666654321111116666
1st000-1-1-1-1-10000050000
2nd000-10000100005-5000

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.

Intensity f(x) 1st derivative 2nd derivative zero crossing at the step
The data is the same as in the table. The ramp gives a constant first derivative and only two small second-derivative blips. The step gives a first-derivative spike and a +/- second-derivative pair that crosses zero.

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 f=\begin{bmatrix}\partial f/\partial x\\[2pt]\partial f/\partial y\end{bmatrix}\ \ \text{(gradient, a vector)},\qquad \nabla^2 f=\frac{\partial^2 f}{\partial x^2}+\frac{\partial^2 f}{\partial y^2}\ \ \text{(Laplacian, a scalar)} $$

$\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

$$ \frac{\partial^2 f}{\partial x^2}=f(x+1,y)+f(x-1,y)-2f(x,y),\qquad \frac{\partial^2 f}{\partial y^2}=f(x,y+1)+f(x,y-1)-2f(x,y) $$ $$ \nabla^2 f=f(x+1,y)+f(x-1,y)+f(x,y+1)+f(x,y-1)-4f(x,y) $$

Add the two 1D masks $[1,-2,1]$ (horizontal and vertical). The sum is the 4-neighbour Laplacian kernel.

010
1-41
010

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

111
1-81
111
0-10
-14-1
0-10
-1-1-1
-18-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:

$$ g(x,y)=\begin{cases} f(x,y)-\nabla^2 f(x,y) & \text{centre coefficient of the mask is negative}\\ f(x,y)+\nabla^2 f(x,y) & \text{centre coefficient is positive}\end{cases} $$

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:

10105050
10105050
10105050

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.

$$ \text{Mask}(x,y)=f(x,y)-\bar f(x,y),\qquad g(x,y)=f(x,y)+\text{Mask}(x,y) $$

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

$$ g(x,y)=f(x,y)+k\cdot\text{Mask}(x,y),\qquad k\ge 1 $$

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

x1234
$f$10105050
blur $\bar f$1023.336.750
Mask $=f-\bar f$0-13.313.30
Unsharp ($k=1$)10-3.3 $\to$ 063.350
High-boost ($k=2$)10-16.7 $\to$ 076.750

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.

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

Edge models

ModelProfileDerivative behaviour
Step edgeAbrupt change from one value to another in a single step1st: single spike. 2nd: +/- pair with a zero crossing
Ramp edgeGradual transition over some distance1st: constant plateau along the ramp. 2nd: only at start and end
Roof edgeA peak or ridge: intensity rises to a maximum then falls (a thin line)Needs a derivative of both signs at the peak
step ramp roof
Intensity profiles of the three ideal edge models. Real edges are usually ramps because of blur.

Gradient-based edge detection

$$ \nabla f=\begin{bmatrix}g_x\\g_y\end{bmatrix}=\begin{bmatrix}\partial f/\partial x\\\partial f/\partial y\end{bmatrix},\qquad M(x,y)=\|\nabla f\|=\sqrt{g_x^2+g_y^2}\ \approx\ |g_x|+|g_y| $$ $$ \theta(x,y)=\tan^{-1}\!\left(\frac{g_y}{g_x}\right) $$

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

-101
-202
-101
-1-2-1
000
121

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:

  1. Load the image.
  2. Convert it to grayscale.
  3. Smooth it with a Gaussian (optional).
  4. Apply the Sobel operator.
  5. Compute the gradient magnitude.
  6. Threshold: pixels above the threshold are edges.
  7. Normalise.

Worked example 1 (vertical edge). 3x3 neighbourhood:

101050
101050
101050

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

123
456
789

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

-101
-101
-101
-1-1-1
000
111

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.

10
0-1
01
-10

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

RobertsPrewittSobel
Kernel size2x23x33x3
Weights+1/-1 on diagonalsEqual (1,1,1)Centre weighted (1,2,1)
Noise sensitivityHighestModerateLowest (built-in smoothing)
Edge thickness / localisationThin, sharp, half-pixel shiftThickerThicker
MeasuresDiagonal differencesHorizontal / verticalHorizontal / vertical
CostCheapestLowLow

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.

$$ \text{LoG}(x,y)=\nabla^2\big(G(x,y)*f\big)=\big(\nabla^2G\big)*f,\qquad \nabla^2G(x,y)=\frac{x^2+y^2-2\sigma^2}{2\pi\sigma^6}\,e^{-\frac{x^2+y^2}{2\sigma^2}} $$

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

  1. Smooth the image with a Gaussian.
  2. Apply a Laplacian mask.
  3. Find the zero crossings (sign changes between neighbouring pixels). Use a strength threshold to ignore weak ones.

A commonly used 5x5 LoG approximation:

00-100
0-1-2-10
-1-216-2-1
0-1-2-10
00-100

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 usedFirstSecond (after Gaussian)
Edge located atPeak of gradient magnitudeZero crossing
Gives direction?Yes ($\theta$)No (isotropic)
Edge thicknessThick, needs thresholdingThin, closed contours
Noise handlingOptional smoothingBuilt-in Gaussian smoothing
CostTwo small convolutionsLarger 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|$.