DEV Community

Cover image for Tutorial: Padding & Edge Detection in CNNs (Hands-on)
Ali Raza
Ali Raza

Posted on

Tutorial: Padding & Edge Detection in CNNs (Hands-on)


This is a hands-on tutorial that builds directly on the convolution and padding concepts from the previous article. We'll walk through normalizing filter output values and applying edge-detection filters step by step.

Step 1: Normalizing Output Values (Min-Max)

When you apply a filter to an image, the resulting values aren't always valid pixel intensities. For instance, a convolution operation might produce values like -4 or numbers greater than 255 — but a pixel can only be between 0 and 255.

This is where a min-max function comes in:

if value < 0   → set to 0
if value > 255 → set to 255
Enter fullscreen mode Exit fullscreen mode

This clips the output back into a valid image range (this is conceptually very similar to what a ReLU activation does later in the network, followed by clipping at the maximum pixel value).

Step 2: Edge Detection Filters

Two of the most fundamental 3×3 filters in image processing are used to detect horizontal and vertical edges — this is essentially a simplified version of the classic Sobel operator.

Horizontal Edge Filter

 +1  +2  +1
  0   0   0
 -1  -2  -1
Enter fullscreen mode Exit fullscreen mode

Vertical Edge Filter

 +1   0  -1
 +2   0  -2
 +1   0  -1
Enter fullscreen mode Exit fullscreen mode

When either of these filters slides across an image, it produces a strong response wherever there's a sharp change in pixel intensity in that direction — which is exactly what an "edge" is: a sudden jump in brightness.

Step 3: Applying Padding in Practice

Continuing from our padding discussion — say we take a 6×6 grayscale image and want to preserve its size after convolution with a 3×3 filter. We calculated earlier that we need p = 1 (one row/column of zero-padding on every side).

This transforms our 6×6 image into an 8×8 padded image before the filter is applied:

  • Zeros are added to the top and bottom rows.
  • Zeros are added to the left and right columns.
  • The original 6×6 image data sits untouched in the center.

When the 3×3 filter now slides across this 8×8 padded image, the output comes back out as 6×6 — matching the original size, with every pixel (including the edges) properly represented in the output.

Putting It All Together

  1. Take the raw image (grayscale or RGB).
  2. Apply zero-padding based on your desired output size.
  3. Slide the filter (e.g., horizontal or vertical edge detector) across the padded image, computing the convolution at each step.
  4. Apply min-max clipping to keep pixel values within the valid 0–255 range.
  5. The result is a feature map that highlights edges — the very first building block CNNs use to eventually recognize full objects.

This is the foundation every CNN architecture builds on — stacking many of these convolution + padding + activation steps to go from raw pixels to full image understanding.


This is Part 3 (Tutorial) of a CNN fundamentals series, based on lecture notes from a Deep Learning course.

Top comments (0)