A Gentle Introduction to Convolutions

Convolutions are the workhorse behind most modern image models. At their core, they are a simple idea: slide a small window over an image and, at each stop, combine the pixels underneath it into a single number.

The Core Idea

A convolution uses a small grid of numbers called a kernel (or filter). You place the kernel over a patch of the image, multiply each kernel value by the pixel beneath it, and add everything up. That sum becomes one pixel in the output.

Move the kernel one step to the right and repeat. Do this across the whole image and you get a new image called a feature map.

Formally, for an input image II and a kernel KK, the value at position (i,j)(i, j) of the output feature map SS is a sum of element-wise products:

S(i,j)=(IK)(i,j)=mnI(i+m,j+n)K(m,n)S(i, j) = (I * K)(i, j) = \sum_{m}\sum_{n} I(i + m,\, j + n)\,K(m, n)

Here's that exact operation written out as a tiny 2D convolution in NumPy:

import numpy as np

def convolve2d(image, kernel):
    kh, kw = kernel.shape
    h, w = image.shape[0] - kh + 1, image.shape[1] - kw + 1
    out = np.zeros((h, w))
    for i in range(h):
        for j in range(w):
            patch = image[i:i + kh, j:j + kw]
            out[i, j] = np.sum(patch * kernel)  # multiply, then add
    return out

# A simple vertical-edge detector (Sobel kernel)
edge = np.array([[-1, 0, 1],
                 [-2, 0, 2],
                 [-1, 0, 1]])

Why It Works

The magic is in the kernel's values. Different kernels detect different things:

In a neural network, we don't hand-pick these kernels. The model learns them from data, discovering whatever patterns help it solve the task.

A smoothing (blur) kernel follows a Gaussian profile, ex2e^{-x^2} - the weights peak at the center and taper off toward the edges:

Gaussian blur kernel profile
00.250.500.751-3-1.5001.503xweight

Stacking Layers

One convolution finds simple patterns like edges. Feed its output into another convolution and it combines those edges into corners and textures. Stack a few more and the network builds up to eyes, wheels, or whole objects.

This hierarchy - simple parts combining into complex ones - is why convolutional neural networks (CNNs) are so good at understanding images.

Two Handy Knobs

Takeaway

A convolution is just a sliding weighted sum. Repeat it, learn the weights, and stack the layers - that simple recipe is the foundation of modern computer vision.