Practical 7: 2D Correlation & Convolution

Open In Colab Run this practical in Google Colab

1. Aim

Aim

To implement and analyze the two fundamental linear spatial operations of digital image processing — correlation and convolution — from first principles, demonstrate the impulse-response interpretation that distinguishes them, and apply standard kernels to a real image.

2. Description / Theory
Theory: For an image \(f\) and kernel \(w\) of size \(m \times n\) (with \(m=2a+1, n=2b+1\)):
  • Correlation: \( (w \star f)(x,y) = \sum_{s=-a}^{a}\sum_{t=-b}^{b} w(s,t)\,f(x+s,y+t) \)
  • Convolution: \( (w * f)(x,y) = \sum_{s=-a}^{a}\sum_{t=-b}^{b} w(s,t)\,f(x-s,y-t) \)
  • Identity: \( w * f \;=\; \text{rot}_{180}(w) \star f \)
At a unit impulse the correlation places \(\text{rot}_{180}(w)\) and the convolution places \(w\). The two operations coincide only when the kernel is symmetric under 180° rotation.
$$ (w * f)(x,y) \;=\; \text{rot}_{180}(w) \;\star\; f $$
3. Code

Part 1: Impulse Response Demonstration

Place a unit impulse at the centre of a \(3\times3\) image and operate with a \(3\times3\) ramp kernel. The two outputs differ by a 180° rotation of \(w\) at the impulse position.

Part 2: Custom Matrices

Provide your own \(f\) and \(w\) matrices (rows of space-separated numbers, one row per line). The kernel must have odd dimensions so it has a unique centre.

Part 3: Standard Kernels on a Real Image

Apply a \(3\times3\) box (averaging), a Laplacian (sharpening edge detector), and a Sobel-X (vertical edge detector) to a real grayscale image via convolution.

Part 4: Verification — corr ≠ conv for an Asymmetric Kernel

OpenCV's filter2D performs correlation. With the asymmetric Sobel-X kernel the correlation and convolution outputs differ; the difference image confirms the kernel is not symmetric under 180° rotation.

4. Output 5. Analysis / Conclusion

Analysis Questions

  1. Why does correlation produce a flipped copy of \(w\) when \(f\) is a unit impulse, while convolution produces \(w\) itself?
  2. For which kernels are correlation and convolution numerically identical? Give two examples and one counter-example.
  3. What is the role of zero padding, and what alternatives (replicate, reflect, wrap) might you prefer in different contexts?
  4. What is the computational cost of a direct \(m\times n\) convolution on an \(M\times N\) image, and how is it reduced via separability and FFT?