Convolution
Convolution is the main operation used by a CNN to detect useful patterns such as edges, lines, curves, textures, and other visual features in an image.
What Is Convolution?
Convolution is a mathematical operation where a small matrix called a filter or kernel moves across an image and performs calculations with the pixels.
The goal is to detect a particular pattern.
Image
↓
Small Filter
↓
Move across image
↓
Multiply + Add
↓
Feature Map
In simple words:
Convolution = Look at a small part of an image
+ calculate a value
+ move to the next part
+ repeat
Image and Filter
Let's use a very small example so the calculation is easy to understand.
Suppose our image is:
Image
1 2 3
4 5 6
7 8 9
And our filter is:
Filter
1 0
0 1
The filter is smaller than the image.
Image: Filter:
1 2 3 1 0
4 5 6 0 1
7 8 9
The filter will look at small regions of the image.
Step 1 — Place the Filter
Initially, place the filter over the top-left part of the image.
Image
[1 2] 3
[4 5] 6
7 8 9
Filter
1 0
0 1
The selected image region is:
1 2
4 5
Step 2 — Multiply Corresponding Values
Multiply each image value by the corresponding filter value.
Image region:
1 2
4 5
Filter:
1 0
0 1
Multiply them:
(1 × 1) + (2 × 0)
+ (4 × 0) + (5 × 1)
Now calculate:
1 + 0 + 0 + 5
= 6
So the first convolution result is:
6
Step 3 — Move the Filter
The filter now moves one position to the right.
Image
1 [2 3]
4 [5 6]
7 8 9
The new image region is:
2 3
5 6
Apply the same filter:
(2 × 1) + (3 × 0)
+ (5 × 0) + (6 × 1)
= 2 + 0 + 0 + 6
= 8
The second result is:
8
Complete Convolution Example
Our 2 × 2 filter can move across the 3 × 3 image in four positions.
Image:
1 2 3
4 5 6
7 8 9
Filter:
1 0
0 1
The four calculations produce:
Position 1 → 6
Position 2 → 8
Position 3 → 10
Position 4 → 12
Therefore, the output is:
Feature Map:
6 8
10 12
This output is called a feature map.
What Does the Filter Actually Detect?
The important point is that different filters can detect different patterns.
Filter A
↓
Vertical edges
Filter B
↓
Horizontal edges
Filter C
↓
Corners
Filter D
↓
Textures
Filter E
↓
Other visual patterns
In a real CNN, the filters are usually not manually created like this. The network learns useful filter values during training.
Example — Detecting an Edge
Consider this simple vertical-edge filter:
-1 0 1
-1 0 1
-1 0 1
This filter can respond strongly to certain vertical changes in pixel intensity.
The filter moves across the image and calculates a value at every location.
Image
↓
Apply edge filter
↓
Strong response
↓
Possible edge detected
So convolution allows a CNN to turn raw pixel information into useful visual information.
Strong vs Weak Response
A filter produces different values depending on how well the image region matches the pattern represented by the filter.
Image region matches filter well
↓
Large response
Image region does not match
↓
Small response
This is how convolution helps the network locate useful features in an image.
What Is the Sliding Window?
The movement of the filter across the image is often described as a sliding window.
Image
[■■]■■
[■■]■■
■ ■
↓ move right
Image
■[■■]■
■[■■]■
■ ■
At every position, the filter performs the same multiply-and-add calculation.
Move
↓
Multiply
↓
Add
↓
Store result
↓
Move again
↓
Repeat
What Is Stride?
Stride tells us how many pixels the filter moves each time.
A stride of 1 means the filter moves one position at a time.
Stride = 1
Position 1
↓
Position 2
↓
Position 3
↓
Position 4
A stride of 2 means it jumps two positions.
Stride = 2
Position 1
↓
Position 3
↓
Position 5
Larger strides generally produce smaller output feature maps.
What Is Padding?
When a filter moves across an image, the output can become smaller than the original image.
Padding adds extra values, commonly zeros, around the border of the image.
Original:
1 2 3
4 5 6
7 8 9
With zero padding:
0 0 0 0 0
0 1 2 3 0
0 4 5 6 0
0 7 8 9 0
0 0 0 0 0
Padding allows the filter to process areas near the edges of the original image and can help preserve spatial dimensions.
Convolution Inside a CNN
In a CNN, convolution is normally performed using a
convolution layer such as Conv2D.
Input Image
↓
Conv2D
↓
Feature Map
↓
Activation
↓
Next Layer
A layer can contain many filters.
Input Image
↓
┌───────────────┐
│ Filter 1 │ → Feature Map 1
│ Filter 2 │ → Feature Map 2
│ Filter 3 │ → Feature Map 3
│ Filter 4 │ → Feature Map 4
└───────────────┘
↓
Multiple Feature Maps
Each filter can learn to respond to different patterns.
Convolution With Python
We can implement a simple convolution ourselves to understand what the CNN is doing internally.
import numpy as np
image = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])
kernel = np.array([
[1, 0],
[0, 1]
])
output = np.zeros((2, 2))
for i in range(2):
for j in range(2):
region = image[i:i + 2, j:j + 2]
output[i, j] = np.sum(region * kernel)
print(output)
Output:
[[ 6. 8.]
[10. 12.]]
Understand the Python Code
First, we create the image.
image = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])
Then we create the kernel.
kernel = np.array([
[1, 0],
[0, 1]
])
The output starts as a 2 × 2 matrix filled with zeros.
output = np.zeros((2, 2))
The two loops move the kernel across the image.
for i in range(2):
for j in range(2):
This line extracts the current 2 × 2 image region.
region = image[i:i + 2, j:j + 2]
Then the region and kernel are multiplied element by element and all values are added together.
output[i, j] = np.sum(region * kernel)
That one line represents the core mathematical operation of convolution.
Multiply corresponding values
↓
Add the results
↓
Store one output value
Easy Way to Remember Convolution
Image
↓
Take a small region
↓
Apply filter
↓
Multiply
↓
Add
↓
Get one number
↓
Move filter
↓
Repeat
↓
Feature Map
If you understand this process, you understand the basic idea of convolution.
Check Your Understanding
What is convolution?
It is an operation where a small filter moves across
an image and calculates values from local regions.
What is a filter or kernel?
It is a small matrix of numbers used to detect
patterns in an image.
What happens at each position?
The image region and filter are multiplied element by
element, and the results are added together.
What is the output called?
The collection of convolution results forms a feature
map.
What does stride control?
It controls how far the filter moves at each step.
Why is convolution useful?
It allows a CNN to detect useful local patterns such
as edges, shapes, and textures.