Skip to content

Image Filtering

Source: CS1674 Ch.2 Image Filtering. An image is a function; filtering forms a new image whose pixels are combinations of neighboring original pixels.

1. Images as Functions

A digital grayscale image is a discrete 2D function:

f(x,y):Z2[0,255]
  • A pixel value 0 = black, 255 = white (one byte per pixel for 8-bit).
  • Filtering = local operation producing output pixels as a function of a neighborhood of input pixels.

Filtering is the foundation of nearly all low-level vision: smoothing (denoise), sharpening (enhance), and derivative computation (edges).


2. Linear Filtering: Convolution vs. Correlation

Given an image f and a kernel (filter) h, we compute a new image g.

2.1 Cross-correlation (what "filtering" means here)

The kernel is not flipped:

g(i,j)=u,vf(i+u,j+v)h(u,v)

where (u,v) ranges over the kernel support centered at (0,0).

2.2 Convolution

The kernel is flipped horizontally and vertically:

g(i,j)=u,vf(iu,jv)h(u,v)

Convention in this course: unless explicitly stated as "convolution," filtering means correlation (no flip). The flip only matters when combining two filters or proving theory; for symmetric kernels (Gaussian, box) the two are identical.

2.3 Properties of convolution

PropertyFormula
Commutativefg=gf
Associative(fg)h=f(gh)
Distributes over additionf(g+h)=fg+fh
Scalars factor outkfg=fkg=k(fg)
Identityfe=f, where e=[,0,1,0,] (unit impulse)

Because of associativity, applying filter h1 then h2 equals applying h1h2 once — the basis for separable and cascaded filters.


3. Common Linear Filters

3.1 Box / mean filter

Uniform weights normalized to sum 1:

19[111111111],or 1D: 15[1,1,1,1,1]
  • Replaces each pixel with the average of its neighborhood.
  • Effect: smooths, blurs, removes high-frequency (noise) components → a low-pass filter.
  • Drawback: does not introduce new values; averages across edges → blurs edges; gives a "boxy" appearance due to a non-smooth frequency response.

3.2 Weighted (tent / triangular) filter

116[14641](1D),116[1464141624164624362464162416414641]
  • Approximates a Gaussian; nearer neighbors have more influence.

3.3 Median filter (non-linear)

  • Replaces each pixel with the median of values in its window.
  • Non-linear: more robust to outliers; preserves edges (does not blur across boundaries).
  • Best for salt-and-pepper noise (random black/white pixels).

Mean vs. Median: the mean is linear (new pixel = weighted sum) and blurs edges; the median is order-statistic based, keeps edge sharpness, and is robust to impulse noise.


4. Image Noise

Noise typeDescriptionBest filter
Impulse / salt-and-pepperRandom black/white pixelsMedian
GaussianIntensity perturbed by N(0,σ2)Mean / Gaussian (averaging many shots reduces it)
Speckle / uniformMultiplicative/uniform perturbationMean / adaptive

Averaging many shots reduces Gaussian noise because noise is zero-mean and independent across frames; the mean filter is the canonical low-pass averaging operation.


5. Gaussian Filter

The 2D isotropic Gaussian:

G(x,y)=12πσ2e(x2+y2)/2σ2

5.1 Parameters

  1. Variance σ — controls the amount of smoothing. Larger σ → more blur, removes lower frequencies.
  2. Kernel (mask) size — must be large enough to hold the Gaussian. A common rule: kernel size 3σ to 6σ (e.g., σ=5 with a 30×30 kernel vs. a 10×10 kernel gives different effective truncation; size=10px vs 30px matters).

5.2 Properties

  • Separable: G(x,y)=G(x)G(y). Apply 1D horizontal pass then 1D vertical pass (or vice-versa) — reduces cost from O(K2) to O(2K) per pixel.
  • Low-pass: removes high-frequency detail; used before downsampling (anti-aliasing) and before taking derivatives (to suppress noise).

5.3 Worked intuition

  • σ=2 vs σ=5 (same 30×30 kernel): larger σ → noticeably smoother.
  • kernel size 10px vs 30px (same σ=5): 10px truncates the Gaussian tail, slightly sharper than the 30px version.

6. Image Gradients (Derivative Filters)

Image gradients capture intensity change along an axis — the basis of edge detection.

6.1 Horizontal / vertical gradient kernels

Prewitt-style and Sobel-style (3×3) derivative operators:

Prewitt Gx=[101101101],Gy=[111000111]Sobel Gx=[101202101],Gy=[121000121]
  • Sobel weights the center row/column by 2 to be more noise-robust.
  • The gradient vector and magnitude: f=(fx,fy), |f|=fx2+fy2, direction θ=atan2(fy,fx).

6.2 Why gradients matter

Edges = regions of large gradient magnitude. Used in Canny, HOG, SIFT, and as features for recognition.


7. Image Sharpening (High-Pass)

Sharpening accentuates edges:

sharpened=original+αdetail,detail=originalblurred

Equivalently, a sharpen filter = identity (unit impulse) + (Gaussian − impulse) = a Laplacian-of-Gaussian (LoG) style kernel:

sharpen=[000010000]impulse+λ([000010000]impulseGblurGaussian)
  • The detail extraction is a high-pass filter (it keeps high frequencies).
  • A common sharpening kernel: [010151010].

8. Boundary (Edge) Handling

When the filter window falls off the image edge, choose an extrapolation:

MethodBehavior
Clip / zero-padAssume black (0) outside — can create dark borders.
Wrap aroundTreat image as periodic (toroidal).
Copy edgeRepeat the nearest border pixel.
ReflectMirror pixels across the boundary.

Output size

  • 'full': output larger than input (kernel can extend past edges on all sides).
  • 'same': output same size as input (kernel centered; needs boundary handling).

9. Filter Separability

A 2D filter h is separable if it factors into an outer product of two 1D filters:

h=hcolhrowfh=(fhrow)hcol

Example: the K×K box filter = 1D row mean followed by 1D column mean. Cost drops from O(K2) to O(2K) per pixel. The Gaussian is the canonical separable filter.


10. Non-linear Filters: Thresholding

Simple non-linear filtering such as thresholding (f^=255 if f>t else 0) produces binary masks; useful for segmentation and masking before later processing.


11. Application: Hybrid Images (Oliva, Torralba & Schyns, SIGGRAPH 2006)

A hybrid image combines:

  • a low-frequency version of image A (heavy Gaussian blur), and
  • a high-frequency version of image B (original − blurred, i.e., Laplacian/high-pass).

Because humans perceive coarse (low-frequency) structure at a distance and fine (high-frequency) detail up close, the same image reads as A far away and B up close. This demonstrates that frequency content drives perception — a direct application of filtering.


12. Summary Table

FilterLinear?EffectUse case
Box / meanYesBlurs, low-passFast smoothing
WeightedYesSmooth, low-passGentler blur
MedianNoEdge-preserving denoiseSalt-and-pepper noise
GaussianYesSmooth, separable low-passPrefiltering, derivatives
Sobel/PrewittYesGradient / edgeEdge detection
Sharpen (LoG)YesHigh-passEdge accentuation
ThresholdNoBinarizeSegmentation

Key idea: filtering is correlation with a kernel; choose kernel+size+boundary handling by the frequency content you want to keep or remove.