Subroutine Logo
Subroutine
← Back to Playground AI & ML Intermediate

CNN 2D Image Filters & Convolution Feature Maps

An interactive computer vision exploration of sliding 2D kernel matrices, Sobel edge detectors, and convolutional feature map generation.

Published: 2026-07-26
#AI & ML#Deep Learning#CNN#Computer Vision#Convolution

Convolutional Neural Networks (CNNs) form the foundation of modern computer vision. Unlike fully connected neural networks that evaluate flat 1D vectors, CNNs preserve spatial 2D relationships in images by sliding small weight matrices (kernels) across raw pixels to extract abstract visual features like edges, textures, and shapes.


1. Summary & Key Takeaways

  • 2D Discrete Convolution: Computes the dot product between a K×KK \times K kernel and localized pixel regions: 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) \cdot K(m, n)
  • Sobel Edge Detection Filters: Highlights high-frequency spatial intensity gradients corresponding to object borders.
  • Feature Map Dimensions: For an input of size N×NN \times N, kernel size KK, stride SS, and padding PP: Output Size=NK+2PS+1\text{Output Size} = \left\lfloor \frac{N - K + 2P}{S} \right\rfloor + 1
  • Translation Invariance: Convolution filters recognize visual patterns regardless of where they appear in the frame.

2. Interactive 2D Convolution Playground

Use the interactive visualizer below to slide a 3×33 \times 3 Sobel Edge or Sharpen filter kernel across a 6×66 \times 6 pixel grid and build the resulting 4×44 \times 4 feature map!

Sliding Window Filter

Click Slide 3x3 Kernel 1 Step to watch elementwise multiplication sum values get written into the destination output feature map.

CNN 2D Convolution & Feature MapsComputer Vision Filter

Slide a 3x3 Filter Kernel across the input image pixel grid to build the output feature map.

Input Image (6x6 Pixels)
10
10
10
200
200
200
10
10
10
200
200
200
10
10
10
200
200
200
10
10
10
200
200
200
10
10
10
200
200
200
10
10
10
200
200
200
3x3 Kernel Matrix
-1
-2
-1
0
0
0
1
2
1
Feature Map (4x4 Output)
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Convolution Feature Map Code
convolution2d.py
Python (PyTorch)
# 2D Convolution in Python (PyTorch / NumPy)
import torch
import torch.nn as nn

# Define 2D Convolutional Layer
conv = nn.Conv2d(in_channels=1, out_channels=1, kernel_size=3, padding=0)

# Sobel Edge Filter Weights
sobel_kernel = torch.tensor([
    [[-1., -2., -1.],
      [ 0.,  0.,  0.],
      [ 1.,  2.,  1.]]
]).unsqueeze(0)

conv.weight = nn.Parameter(sobel_kernel)
# Forward pass: Input Image -> Output Feature Map
# output = conv(input_tensor)

3. Mathematical Foundations & Sobel Edge Kernels

1. Sobel Horizontal Edge Filter (KxK_x)

Detects horizontal pixel intensity transitions: Kx=(121000121)K_x = \begin{pmatrix} -1 & -2 & -1 \\ 0 & 0 & 0 \\ 1 & 2 & 1 \end{pmatrix}

2. Sobel Vertical Edge Filter (KyK_y)

Detects vertical pixel intensity transitions: Ky=(101202101)K_y = \begin{pmatrix} -1 & 0 & 1 \\ -2 & 0 & 2 \\ -1 & 0 & 1 \end{pmatrix}

Combined Edge Magnitude (GG)

G=Gx2+Gy2G = \sqrt{G_x^2 + G_y^2}


4. CNN Architecture Layer Comparison

Layer TypePurposePrimary OperationOutput Dimension Change
Convolutional LayerFeature ExtractionSliding Kernel Dot ProductReduces by K1K - 1 (if P=0P=0)
Pooling Layer (MaxPool)Spatial DownsamplingMax/Average Window PoolingCuts dimensions in half (N/2N/2)
Fully Connected (Dense)Final ClassificationMatrix Multiplication + SoftmaxFlattens into class logits