In the previous article, we saw why CNNs are inspired by the human visual cortex. Now let's get into the actual mechanics: what does "convolution" mean, and how does a CNN process an image pixel by pixel?
Images Are Just Numbers
Grayscale images
A grayscale (black & white) image is really just a grid of numbers. Each cell in the grid is a pixel, and its value ranges from 0 to 255:
-
0→ completely black -
255→ completely white - Anything in between → a shade of gray
For example, a small 4×4 grayscale image might look like a matrix of pixel intensities. You can resize this grid — reduce or increase the number of pixels — depending on how much detail you want to keep.
Color images
A color image adds two more layers on top: Red, Green, and Blue (RGB). So instead of a single 2D grid, a color image is a 3D volume — for example, a 6×6×3 image means 6×6 pixels across 3 color channels.
The Convolution Operation
Convolution is the process of sliding a small matrix — called a filter or kernel (commonly 3×3) — across the image, and at each position, performing an element-wise multiplication followed by a sum. The result becomes one pixel in the output feature map.
This is how a CNN detects patterns: different filters are designed (or learned) to highlight specific features like edges, corners, or textures. When you apply a filter across a grayscale image, you get a new, smaller grid where certain regions light up — these are the features the filter was designed to catch.
The Problem: Shrinking Output
Here's something important to notice: after applying a filter, the output is smaller than the input. There's a formula for this:
output size = n − f + 1
Where:
-
n= input size (e.g., 6 for a 6×6 image) -
f= filter size (e.g., 3 for a 3×3 filter)
So a 6×6 image with a 3×3 filter gives a 4×4 output — we've lost information at the borders! Pixels at the edges of the image get "seen" by the filter far fewer times than pixels in the center.
The Fix: Padding
To prevent this loss of information, we use padding — adding extra rows and columns (usually filled with zeros) around the border of the image before applying the filter.
There are two common ways to fill padding:
- Fill with the value of the neighboring pixel.
- Fill with zero — this is the standard, best-practice approach.
With padding added, the formula becomes:
output size = n + 2p − f + 1
Where p is the padding amount. Let's verify with an example: n = 6, f = 3, p = 1:
6 + 2(1) − 3 + 1 = 6
The output stays 6×6 — exactly the same as the input! No information is lost, because padding adds new rows and columns on the top, bottom, left, and right sides of the original image before the filter slides over it.
Coming Up Next
In the next (tutorial) article, we'll actually build edge-detection filters — horizontal and vertical — and see step by step how padding is applied in practice, along with how output pixel values are normalized back into a valid 0–255 range.
This is Part 2 of a CNN fundamentals series, based on lecture notes from a Deep Learning course.

Top comments (0)