NivaarExam PrepOfficial exam papers ↗

20-Bio-B4 Robotics · December 2014

Question 2 of 6: Image Filtering

Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)

Notes on this paper

Paper format: National Exams, December 2014 — 04-Bio-B4 Digital Image Processing. Three hours, open book (any paper notes or textbooks permitted, but no calculator or computer). Six questions of equal value (20 marks each); five constitute a complete paper and only the first five appearing in the answer book are marked. All six are solved here, because this set is a study resource rather than an examination script. Every question is essay/descriptive (definitions, algorithm design, system design) except the operation-count comparison in Question 2(d), which is a short analytical calculation.

Reference texts (the books a candidate should have reviewed for this subject):

Question 2: Image Filtering (20 marks)

Question text not reproduced: the examination questions are © Engineers and Geoscientists BC. Open the official past paper (linked at the top of this page) to read the question, then follow the worked solution below.

Given. An image $f$ of size $M_1 \times N_1$ convolved with a kernel $h$ of size $M_2 \times N_2$.

Find. The three standard output sizes, a direct-convolution algorithm, an FFT-based algorithm, their computational complexities, and the invertibility condition on $h$.

(a) The Three Convolution Output Sizes

Two-dimensional discrete convolution slides the (flipped) kernel over the image; how far the kernel is allowed to overhang the image border fixes the output size:

"full": (M1+M2-1) x (N1+N2-1) "same": M1 x N1 "valid": (M1-M2+1) x (N1-N2+1) f (M1 x N1) kernel h M2 x N2 (shown centred over f for "same")
The three standard 2-D convolution output sizes for an $M_1 \times N_1$ image and an $M_2 \times N_2$ kernel.

(b) Direct (Spatial-Domain) Pseudo-Code

Direct convolution flips the kernel and, at every output position, sums the elementwise products of the kernel with the underlying image patch (here shown producing the "same"-size output, with zero-padding at the border):

function g = convolve2D(f, h):
    // f is M1 x N1, h is M2 x N2 (M2, N2 assumed odd)
    a = floor(M2 / 2); b = floor(N2 / 2)
    g = zeros(M1, N1)
    for m = 0 .. M1-1:
        for n = 0 .. N1-1:
            acc = 0
            for i = -a .. a:
                for j = -b .. b:
                    if (m-i, n-j) is inside f:
                        // h is flipped: index (a-i, b-j) is h(-i,-j)
                        acc += f(m-i, n-j) * h(a-i, b-j)
                    // else: f is treated as 0 (zero-padding)
            g(m, n) = acc
    return g

(c) Fast Convolution via the Fourier Transform

The convolution theorem states that convolution in the spatial domain is elementwise multiplication in the frequency domain. To avoid the wraparound error that a naive equal-size DFT would introduce (circular convolution ≠ linear convolution), both $f$ and $h$ are first zero-padded to at least $(M_1+M_2-1) \times (N_1+N_2-1)$:

  1. Zero-pad $f$ and $h$ to a common size $P \times Q \ge (M_1+M_2-1) \times (N_1+N_2-1)$ (rounded up to a size efficient for the FFT, e.g. a power of two).
    $F = \text{FFT}(f_{\text{padded}}), \quad H = \text{FFT}(h_{\text{padded}})$
  2. Multiply elementwise in the frequency domain: $G(u,v) = F(u,v)\,H(u,v)$.
  3. Inverse transform and crop: $g = \text{IFFT}(G)$, then crop to the desired "full"/"same"/"valid" size from (a).

This, the same sequence convolved via zero-padded forward DFT → pointwise multiply → inverse DFT — confirming the convolution theorem invoked above.

(d) Computational Complexity and the Crossover

Approach. Count multiplications for each method as a function of the image and kernel sizes, then compare on a concrete example.

  1. Direct method. Each of the $M_1 N_1$ output pixels performs $M_2 N_2$ multiplications: $$C_{\text{direct}} = M_1 N_1 M_2 N_2$$
  2. FFT-based method. Padding to $P \times Q$ (with $P,Q = O(M_1+M_2)$), each 2-D FFT/IFFT costs $O(PQ\log_2(PQ))$; there are three transforms (forward $f$, forward $h$, one inverse) plus $PQ$ pointwise multiplications: $$C_{\text{FFT}} \approx 3\,PQ\log_2(PQ) + PQ$$
  3. Crossover. For a $256\times256$ image with a small $5\times5$ kernel, $C_{\text{direct}} = 256^2\cdot25 = 1{,}638{,}400$, against $C_{\text{FFT}} \approx 1.44\times10^7$ (both $f$ and $h$ must be padded to at least $260\times260$ to avoid wraparound, which rounds up to a $512\times512$ FFT) — the direct method wins by roughly a factor of 9. For a large kernel approaching the image size (e.g. $129\times129$ on the same image), $C_{\text{direct}}$ grows to $\approx1.09\times10^9$, while the required padding is still $256+129-1=384$, rounding up to the same $512\times512$ transform, so $C_{\text{FFT}}$ is unchanged at $\approx1.44\times10^7$ — now the FFT method wins by roughly a factor of 75. The comparison shows the FFT-based cost is set almost entirely by which power-of-two padded transform size the kernel forces, not by the kernel area directly, whereas the direct cost scales linearly with $M_2N_2$ regardless of padding.
CaseDirect multiplicationsFFT-based multiplications (approx.)Winner
$256\times256$ image, $5\times5$ kernel1,638,400≈1.44×107Direct
$256\times256$ image, $129\times129$ kernel≈1.09×109≈1.44×107FFT

Answer to (d): when $M_2, N_2 \ll M_1, N_1$, the direct method is more efficient, because its cost scales with the small kernel area $M_2N_2$, whereas the FFT approach pays a near-fixed overhead set by the (much larger) padded image size regardless of how small the kernel is.

(e) Conditions on $h$ for Deconvolution

Recovering $f$ from $g = f * h$ means, in the frequency domain, dividing $G(u,v)$ by $H(u,v) = \text{FFT}(h)$. This is only well-posed if: