Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Spatial filtering: smoothing and sharpening

Spatial filtering modifies an image by local weighted sums (convolution) to smooth (low-pass) or sharpen (high-pass) features; engineers use these for denoising, pre-processing for segmentation/recognition, and improving visual detail for inspection systems.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Smoothing and sharpening are basic spatial filters used in many engineering tasks: smoothing (low-pass) removes noise before segmentation or compression, and sharpening (high-pass) enhances edges and fine details used by feature detectors and machine-vision inspectors. Both are applied directly to pixel neighborhoods and are computationally cheap, so they are used in real-time image pipelines and pre-processing before more advanced methods (for example, edge detection, segmentation, or frequency-domain filtering).

Key ideas

Start with definitions and assumptions.

  • Image: a 2D function f(x,y) giving intensity (gray level) at integer pixel coordinates x,y. Intensity is unitless or in arbitrary gray-level units.
  • Neighborhood / support: the set of pixels around a central pixel used by a filter (for example, a 3×3 neighborhood).
  • Kernel (mask, filter): a small matrix h(s,t) of weights applied to the neighborhood. The kernel size is (2a+1)×(2b+1) with integer a,b ≥ 0.
  • Linear shift-invariant (LSI) filter: a filter for which output depends linearly on inputs and is invariant under translation of the image. For LSI filters the operation is convolution.
  • Convolution (discrete 2D): the output image g(x,y) is the weighted sum of the input intensities with the kernel reversed and shifted. Convolution assumes LSI.

Convolution formula (derivation universities expect): Start from the discrete LSI property. For an impulse δ(x,y) (1 at (0,0), 0 elsewhere) the filter response is h(x,y). For a general image f(x,y) = Σ_x' Σ_y' f(x',y') δ(x-x', y-y'). By linearity and shift-invariance,

g(x,y) = Σ_x' Σ_y' f(x',y') h(x-x', y-y').

Replace indices s = x - x', t = y - y' to obtain the usual convolution form:

g(x,y) = Σ_s Σ_t f(x-s, y-t) h(s,t).

  • Correlation vs convolution: correlation does not flip the kernel; many implementations use correlation notation but call it convolution. In exams, state whether kernel is flipped.

Smoothing (low-pass) filters

  • Smoothing (low-pass): reduces high-frequency components (rapid intensity changes), thus reduces noise but blurs edges. Examples: uniform (box) filter, Gaussian filter, separable filters. Also common in exams: the nonlinear median filter (replace each pixel by the median of its neighborhood); it preserves edges better than mean/Gaussian but is not LSI and cannot be expressed as a convolution.
  • Box (mean) filter: kernel h(s,t) = 1/N over N elements (for a k×k kernel N = k^2). It is simple but can create blocky/aliasing artifacts and its frequency response is anisotropic with strong sidelobes (unlike the isotropic Gaussian).
  • Gaussian filter: continuous 2D Gaussian G(x,y) = (1/(2πσ^2)) exp(-(x^2+y^2)/(2σ^2)). It is isotropic, smooth, and separable into 1D Gaussians: G(x,y)=g(x)g(y) where g(x)= (1/(√(2π)σ)) exp(-x^2/(2σ^2)). Discrete Gaussian is sampled and renormalized so its entries sum to 1.
  • Separable filter: if h(x,y) = h_x(x)·h_y(y) then convolution with the 2D kernel can be done by first convolving rows with h_x then columns with h_y – reducing operations from O(k^2) to O(2k).

Sharpening (high-pass) filters

  • Sharpening (high-pass): emphasizes high-frequency components (edges, small details). It enhances edges but also amplifies high-frequency noise.
  • Gradient and Laplacian: derivatives respond to intensity changes. The Laplacian is the sum of second derivatives: ∇²f = ∂²f/∂x² + ∂²f/∂y². Discrete approximations: 4-neighbour Laplacian or 8-neighbour versions.
  • Discrete Laplacian (4-neighbour) commonly used (definition choice matters):
    • Version A (∇² ≈ north+south+east+west − 4·center): kernel K1 = [[0,1,0],[1,-4,1],[0,1,0]]. This yields positive response at dark-to-bright transitions depending on sign.
    • Version B (negative of above): K2 = [[0,-1,0],[-1,4,-1],[0,-1,0]]; both are used—state which in exam answers. An 8-neighbour Laplacian example is K3 = [[1,1,1],[1,-8,1],[1,1,1]] (or its negative); all Laplacians sum to 0 and are often scaled by a factor before adding back to f.
  • Laplacian sharpening: combine original image and Laplacian to get enhanced image. Common forms:
    • g = f − ∇²f (if ∇² defined as neighbors − 4·center), or
    • g = f + (−∇²f) (equivalent when using the opposite sign kernel). Always state the kernel and sign in an answer.
  • Unsharp masking (USM): compute a smoothed version L = lowpass(f). The mask m = f − L. Sharpened image g = f + k·m, where k ≥ 0 controls enhancement. k = 1 is classic unsharp; k > 0 with larger k gives stronger sharpening.
  • High-boost filtering: defined as g = A·f − L with A ≥ 1. Special cases: A = 1 yields the high-pass component f − L; A = 2 equals unsharp masking with k = 1. Do not conflate A with k except in this special case.

Diagrams to draw (describe exactly):

  • To show convolution: draw a 3×3 grid of image pixel values and a 3×3 kernel matrix beside it. Then draw the kernel centered on the image center pixel and label each product and the final sum arrow to a single output pixel value.
  • To show separability: draw a 3×3 Gaussian kernel and show decomposition into a 3×1 column and 1×3 row; indicate first convolve rows then columns.

Assumptions behind derivations: pixels are sampled on integer grid, filters are small relative to image, boundary handling (zero-padding, replicate, or crop) must be chosen; results depend on that choice.

Formulas

g(x,y) = Σ_s Σ_t f(x-s, y-t) · h(s,t) — g is filtered output at pixel (x,y), f is input intensity (unit: gray level), h is kernel (unitless weights); applies to discrete LSI filters (convolution).

Box kernel (k×k): h(s,t) = 1/k^2 — h unitless; applies when smoothing by uniform averaging over k×k neighborhood.

G(x,y) = 1/(2πσ^2) · exp(-(x^2 + y^2)/(2σ^2)) — continuous Gaussian; x,y in pixels, σ in pixels; G has units 1/pixel^2 before discretization; when sampled for discrete filter its entries are renormalized to sum 1 (unitless weights).

Discrete separable convolution: g = (f * h_x) * h_y — h_x length m, h_y length n; reduces computation cost.

Discrete Laplacian (4-neigh): ∇²f(x,y) ≈ f(x+1,y)+f(x-1,y)+f(x,y+1)+f(x,y-1) − 4·f(x,y) — output unit = gray level; kernel K1 = [[0,1,0],[1,-4,1],[0,1,0]].

Unsharp mask: g = f + k·(f − L) where L = lowpass(f), k ≥ 0 (unitless).`

High-boost filter: g = A·f − L (A ≥ 1). Note: A = 2 corresponds to unsharp masking with k = 1; in general g = A·f − L is not identical to g = f + k·(f − L) unless A = 2 and k = 1.`

Normalization rule: Σ_s Σ_t h(s,t) = 1 for low-pass (preserve DC / average intensity). For high-pass filters this sum is 0 (no DC), so they emphasize changes.

Worked examples

Example 1 — 3×3 mean (box) smoothing of a 5×5 image (numerical, common exam task): Given image f (gray levels, unitless): Row1: 52 55 61 59 79 Row2: 62 59 55 104 94 Row3: 63 65 66 113 144 Row4: 64 70 70 126 154 Row5: 69 73 75 136 180

Apply a 3×3 mean filter (h = all 1/9). Compute output at center pixel (x=3,y=3) (third row, third column). Formula: g(3,3) = (1/9) · Σ_{i=2..4} Σ_{j=2..4} f(i,j) Sum the 3×3 neighborhood: 59 + 55 + 104 + 65 + 66 + 113 + 70 + 70 + 126 = 728 Divide by 9: 728 / 9 = 80.888... ≈ 80.89 Final answer: g(3,3) = 80.89 (gray-level units) (rounded to 2 decimals). Edge handling: we computed only for interior pixel; for edges use chosen padding.

Example 2 — unsharp masking and high-boost on a simple 3×3 test pattern (shows sharpening effect): Original 3×3 image (gray level units): 10 10 10 10 80 10 10 10 10

Compute 3×3 mean lowpass L at center: L = (8·10 + 80)/9 = (80 + 80)/9 = 160/9 = 17.777... Wait, recompute carefully: 8*10 = 80; +80 = 160; 160/9 = 17.777... (I previously used a different number; corrected now.) Mask m = f − L at center = 80 − 17.777... = 62.222... Unsharp mask (k = 1): g = f + k·m = 80 + 62.222... = 142.222... => 142.22 (gray-level units). High-boost with A = 1.5: g = A·f − L = 1.5·80 − 17.777... = 120 − 17.777... = 102.222... => 102.22 (gray-level units). (These show how unsharp masking with higher k produces stronger enhancement; values may be clipped to image range [0,255] in practical implementations.)

Note: I corrected the mean value carefully — verify arithmetic if you reproduce variants.

Common mistakes

  • Forgetting to renormalize discrete Gaussian samples so they sum to 1 for smoothing.
  • Getting the Laplacian sign wrong; different textbooks choose kernels with opposite signs—always show which kernel you use and the sign convention for sharpening.
  • Using an integer-only intermediate without clipping or range checking and getting wrap-around errors; in exams state clipping to valid intensity range if required.
  • For separability, trying to separate a non-separable kernel (you must prove h(x,y) = h_x(x)·h_y(y)).

For your exam

Types of questions you will see:

  • Definitions and short notes: define convolution, kernel, separability, Gaussian smoothing, Laplacian, unsharp masking.
  • Derivations: derive discrete convolution formula starting from LSI property; show separability and reduced computational cost; derive discrete Laplacian from second-derivative finite differences.
  • Numericals: apply a given kernel to a small image and compute a pixel value (always show sums and normalization). State padding rule.
  • Programs: short pseudo-code or Python to implement 2D convolution (show double loops or use separable form). Mention complexity.
  • Diagrams: draw kernel overlay on the image grid and label multiplications and sums.

What a full-marks answer includes:

  • Clear definitions of terms used and the sign conventions for derivative operators.
  • Step-by-step derivation with index changes clearly shown for convolution.
  • For numericals: show the neighborhood values, the kernel values, the pointwise products, the sum, and final normalization and rounding/clipping.
  • For separability: show explicit factorization and complexity comparison O(k^2) vs O(2k).

Quick check

  1. Write the discrete 2D convolution formula for an LSI filter.
  2. What is the continuous 2D Gaussian formula and what does σ represent?
  3. Give the unsharp masking formula in terms of f, L (lowpass of f) and k.

Answers: 1) g(x,y) = Σ_s Σ_t f(x−s,y−t)·h(s,t). 2) G(x,y) = 1/(2πσ^2)·exp(−(x^2+y^2)/(2σ^2)), σ is the standard deviation (in pixels) controlling the blur width. 3) g = f + k·(f − L).

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?