Convolution Operation, Kernels, Stride & Padding Explained

⚡ Direct Answer & Key Takeaway

Convolution slides a small weight matrix called a kernel across an input image, multiplying overlapping pixels element-wise and summing them to build a feature map. Stride `S` sets how many pixels the kernel jumps per step, shrinking spatial dimensions as `S` grows. Padding `P` adds border zeros to preserve edge pixels and output size. For an `N × N` input and `K × K` kernel, output width is `⌊(N + 2P - K) / S⌋ + 1`.

1. How Kernels Extract Edges, Blurs & Sharpening

In CBSE Class 10 AI (Code 417) Part B, convolution is taught as the mathematical engine behind image filtering and Convolutional Neural Networks. Rather than inspecting one isolated pixel at a time, a kernel (also called a filter)—often sized `3×3` or `5×5`—captures local neighborhood patterns. Each position of the kernel multiplies its weights against the underlying pixel intensities and adds the products into one cell of the output feature map. Handcrafted kernels perform distinct classical operations: an averaging kernel filled with `1/9` blurs noise, a Sobel vertical kernel with negative weights on the left and positive weights on the right highlights vertical edges, and a high-center negative-surround kernel sharpens details. In deep learning, the network learns these kernel weights automatically through backpropagation instead of relying on fixed human-designed numbers.

  • Box blur kernel (3x3): all 9 entries equal 1/9, smoothing rapid pixel noise.
  • Vertical Sobel kernel: [[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]] responds to left-to-right brightness jumps.
  • Sharpen kernel: [[0, -1, 0], [-1, 5, -1], [0, -1, 0]] boosts center contrast.

2. Stride & Padding Output Size Formula

Without padding, a `3×3` kernel placed over a `6×6` image can only center itself on the inner `4×4` pixels, causing two issues: the feature map shrinks after every layer, and corner pixels are sampled only once while interior pixels are sampled up to nine times. Zero-padding (`P`) resolves both drawbacks by wrapping a border of zeros around the input. Under 'valid' padding (`P = 0`), spatial size drops; under 'same' padding with `S = 1`, setting `P = (K - 1) / 2` keeps output dimensions equal to input dimensions. Stride (`S`) controls step size: moving by `S = 2` halves width and height roughly in one step. In Class 12 AI (Code 843) numericals, always evaluate `Output = floor((N + 2P - K) / S) + 1` before multiplying by the number of filters.

  • Valid padding (P = 0) shrinks an N x N image to (N - K + 1) when stride S = 1.
  • Same padding for K = 3, S = 1 uses P = 1 so output size remains N x N.
  • Multi-channel RGB images use a K x K x 3 kernel that sums across all 3 channels into 1 output slice.

3. Worked Convolution Numerical & Feature Map Depth

Suppose an input grayscale matrix of size `5×5` passes through a `3×3` vertical edge kernel with stride `S = 1` and padding `P = 0`. Using the formula `(5 + 2(0) - 3) / 1 + 1`, the resulting feature map is `3×3`. For the top-left `3×3` window containing values `[[10, 10, 50], [10, 10, 50], [10, 10, 50]]` and kernel `[[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]`, the dot product equals `(-10 + 0 + 50) + (-20 + 0 + 100) + (-10 + 0 + 50) = 160`. A high positive activation flags a sharp vertical boundary from dark left (`10`) to bright right (`50`). If a convolutional layer applies `32` distinct `3×3` kernels simultaneously, it stacks `32` feature maps into a `3×3×32` output volume—each channel detecting a different edge orientation or texture.

  • Each filter in a Conv layer produces exactly one 2D channel in the output feature map.
  • Negative convolution outputs are typically clamped to 0 by the following ReLU layer.
  • Always count 2P (both sides: left + right, or top + bottom) in dimension calculations.

4. 2D Convolution with Configurable Stride & Zero Padding

def conv2d_single(image, kernel, stride=1, padding=0):
    n = len(image)
    k = len(kernel)
    padded = [[0] * (n + 2 * padding) for _ in range(n + 2 * padding)]
    for r in range(n):
        for c in range(n):
            padded[r + padding][c + padding] = image[r][c]

    out_size = (n + 2 * padding - k) // stride + 1
    output = []
    for r in range(0, out_size * stride, stride):
        row = []
        for c in range(0, out_size * stride, stride):
            val = sum(
                padded[r + kr][c + kc] * kernel[kr][kc]
                for kr in range(k)
                for kc in range(k)
            )
            row.append(val)
        output.append(row)
    return output

img = [
    [10, 10, 50, 50],
    [10, 10, 50, 50],
    [10, 10, 50, 50],
    [10, 10, 50, 50],
]
sobel_x = [[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]
feat_map = conv2d_single(img, sobel_x, stride=1, padding=0)
print("Output shape:", len(feat_map), "x", len(feat_map[0]))
for row in feat_map:
    print(row)

Verified Console / Dry-Run Output:

Output shape: 2 x 2
[160, 160]
[160, 160]

5. Worked Convolution Output Dimension Numericals (Code 417 / 843)

Input (N×N)Kernel (K×K)Padding (P) & Stride (S)Output Size ((N+2P-K)/S + 1)
32 × 323 × 3P = 0, S = 130 × 30
32 × 323 × 3P = 1, S = 132 × 32 (Same)
28 × 285 × 5P = 0, S = 124 × 24
64 × 644 × 4P = 1, S = 232 × 32
🎯 1-on-1 Live Mentorship & Free Study Kit

Ace CNN & Convolution Numericals in 1-on-1 Micro-Pods

Work through CBSE 417 & 843 feature-map problems and live Python filters with 4Achievers Junior tutors for Grades 3-12.

💬 WhatsApp Now
🔒 Instant WhatsApp notes + free 60-min 1-on-1 trial session
Confirmed! Our Senior Mentor is sending your study kit & free 1-on-1 demo link on WhatsApp now.

6. Common Mistakes & Top Scorer Tips

  • Avoid Mistake: Writing (N + P - K)/S + 1 instead of (N + 2P - K)/S + 1 and losing half the border padding.
  • Avoid Mistake: Assuming the number of channels in the output feature map depends on input size rather than filter count.
  • Avoid Mistake: Confusing convolution kernel weights with RGB pixel values when tracing manual 3x3 calculations.
  • Examiner / Mentor Tip: Always write the formula O = floor((N + 2P - K) / S) + 1 first in CBSE Part B numericals for guaranteed step marks.
  • Examiner / Mentor Tip: Remember that odd kernel sizes (3x3, 5x5) allow symmetric padding P = (K - 1) / 2 around a single center pixel.
  • Examiner / Mentor Tip: Explain clearly that a 3x3 filter on a 3-channel RGB image actually has 3×3×3 = 27 weights plus 1 bias.

7. Interactive Concept Check Quiz

Question: An input feature map of size 14 × 14 is convolved with sixteen 5 × 5 kernels using padding P = 2 and stride S = 2. What is the output volume shape?

8. Frequently Asked Questions

Why do modern CNNs prefer stacking two 3×3 kernels over one 5×5 kernel?
Two 3×3 layers cover the same 5×5 receptive field while using fewer parameters (18 vs 25 per channel) and adding an extra ReLU non-linearity.
Does zero-padding distort the image content?
No. Border zeros minimally affect edge responses while preventing rapid spatial shrinking and giving corner pixels fair coverage.
What happens if (N + 2P - K) is not evenly divisible by stride S?
Standard frameworks take the floor (integer division), discarding incomplete trailing columns or rows on the right and bottom edges.

Explore Related Lessons & Free Interactive Tools

Free 1-on-1 Session

Book a Free Demo Class