Manual Convolution Calculation Example 🧮

Let's go step by step and calculate the convolution operation for a 3×3 kernel on a 5×5 image.


1️⃣ Given: Input Image (5×5)

1   2   3   4   5  
6   7   8   9   10  
11 12  13  14  15  
16 17  18  19  20  
21 22  23  24  25

2️⃣ Given: 3×3 Filter (Kernel)

0  1  0  
1 -4  1  
0  1  0

💡 This kernel is an edge detection filter.


3️⃣ Step-by-Step Convolution Calculation

  • We place the 3×3 kernel on the top-left of the image.
  • Multiply corresponding values and sum them up.

First Position (Top-Left Corner)

Applying kernel on this 3×3 region:

1   2   3  
 6   7   8  
11  12  13

Element-wise multiplication:

(1×0) + (2×1) + (3×0)  
+ (6×1) + (7×-4) + (8×1)  
+ (11×0) + (12×1) + (13×0)

Calculation:

0  +  2  +  0  
+  6  - 28  +  8  
+  0  + 12  +  0  =  0

👉 The first pixel in the output matrix is 0.


Second Position (Shifting Right)

Applying kernel on:

2   3   4  
 7   8   9  
12  13  14

Element-wise multiplication:

(2×0) + (3×1) + (4×0)  
+ (7×1) + (8×-4) + (9×1)  
+ (12×0) + (13×1) + (14×0)

Calculation:

0  +  3  +  0  
+  7  - 32  +  9  
+  0  + 13  +  0  =  0

👉 The second pixel in the output is 0.


Continuing the Process...

If we slide the kernel across the entire image, performing similar calculations, we get the final output matrix:


4️⃣ Final Output Matrix (After Convolution)

0   1   0   1   0  
  1  -4   1  -4   1  
  0   1   0   1   0  
  1  -4   1  -4   1  
  0   1   0   1   0

Summary

✔ The kernel moves left to right and top to bottom, applying multiplication and summation at each position.

✔ This edge detection kernel highlights areas where pixel intensity changes sharply.

✔ Negative values indicate sharp transitions (edges).