Convolution and Correlation - iffatAGheyas/computer-vision-handbook GitHub Wiki
What Are Convolution and Correlation?
Convolution and correlation are core image-processing operations. Both involve sliding a small matrix—called a kernel or filter—over an image and computing a weighted sum at each position. These techniques power blurring, edge detection, sharpening and more.
Correlation vs Convolution
Operation | Description |
---|---|
Correlation | Place the kernel as-is and compute the dot product with the image patch |
Convolution | Flip the kernel horizontally and vertically, then compute the dot product |
Note: OpenCV’s
filter2D
implements correlation, even when it’s referred to as convolution.
Step-by-Step Numerical Example
Given a 3×3 image I and a simple averaging kernel K:
I = [ [10, 20, 30],
[40, 50, 60],
[70, 80, 90] ]
K = (1/9) × [ [1, 1, 1],
[1, 1, 1],
[1, 1, 1] ]
At the centre pixel (position (1,1)), correlation yields:
Output = (1/9) × (10 + 20 + 30 + 40 + 50 + 60 + 70 + 80 + 90)
= 450 / 9
= 50
So the central pixel becomes 50, the average of its 3×3 neighbourhood.
🐍 Python Example (OpenCV)
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Load grayscale image
img_gray = cv2.imread("bird.jpg", cv2.IMREAD_GRAYSCALE)
# Define a 3×3 averaging kernel
kernel = np.ones((3, 3), np.float32) / 9
# Apply filtering (correlation)
filtered = cv2.filter2D(img_gray, -1, kernel)
# Display results
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.imshow(img_gray, cmap="gray")
plt.title("Original")
plt.axis("off")
plt.subplot(1, 2, 2)
plt.imshow(filtered, cmap="gray")
plt.title("After Convolution (Averaging)")
plt.axis("off")
plt.tight_layout()
plt.show()
🔄 Summary
Term | Description |
---|---|
Kernel | Small matrix used for filtering |
Correlation | Slide kernel as-is over the image |
Convolution | Slide a flipped kernel over the image |
filter2D | OpenCV function that performs correlation |
🔄 Correlation in Image Processing
🧠 What is Correlation?
In image processing, correlation measures similarity between a small filter (kernel) and a region of the image.
Imagine sliding a stencil over an image and checking how well it “matches” each patch.
🔍 Correlation vs. Convolution — What’s the Difference?
Operation | Flip the Kernel? | Used in Deep Learning? | Intuition |
---|---|---|---|
Convolution | ✅ Yes (both axes) | ✅ Yes | Emphasises pattern matching |
Correlation | ❌ No | ✅ Used in OpenCV & NumPy | Measures similarity directly |
📌 In OpenCV,
filter2D()
actually performs correlation, even though it’s named like convolution.
🧮 How Correlation Works
Given an image I and a kernel K, for each pixel (x, y) we compute:
Steps to Perform Correlation
- Slide the kernel over the image
- Multiply corresponding elements
- Sum the result → place in the output image
Note: No flipping of the kernel is done here.
🔢 Example: Correlation with a 3×3 Filter
Imagine this 3×3 grayscale image:
I = [ [10, 20, 30],
[40, 50, 60],
[70, 80, 90] ]
And a 3×3 kernel (similar to a Sobel X filter):
K = [ [ 1, 0, -1],
[ 1, 0, -1],
[ 1, 0, -1] ]
At the centre pixel (1,1), whose original value is 50, the correlation is:
G(1,1) = 10⋅1 + 20⋅0 + 30⋅(-1)
+ 40⋅1 + 50⋅0 + 60⋅(-1)
+ 70⋅1 + 80⋅0 + 90⋅(-1)
= (10 - 30) + (40 - 60) + (70 - 90)
= -60
🧪 Python Code: Correlation with OpenCV
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Load image in grayscale
img = cv2.imread('bird.jpg', cv2.IMREAD_GRAYSCALE)
# Define a kernel (like a Sobel X)
kernel = np.array([[1, 0, -1],
[1, 0, -1],
[1, 0, -1]], dtype=np.float32)
# Apply correlation using filter2D (no kernel flip)
correlated = cv2.filter2D(img, -1, kernel)
# Show result
plt.figure(figsize=(10, 4))
plt.subplot(1, 2, 1)
plt.imshow(img, cmap='gray')
plt.title("Original")
plt.axis("off")
plt.subplot(1, 2, 2)
plt.imshow(correlated, cmap='gray')
plt.title("Correlation Output")
plt.axis("off")
plt.tight_layout()
plt.show()