Friday, November 20, 2026

How CNN Filters Find Edges: A 3×3 Convolution Example in Python

How can a convolutional neural network (CNN) begin to find useful patterns in an image? One building block is a small filter that moves across pixel values. At each position, it combines a local patch of pixels with the filter's weights to produce one number.

In this tutorial, we follow a 3×3 edge filter across a tiny image, calculate a value by hand, and reproduce the complete result in Python. The example uses a hand-picked filter to explain an operation used in CNNs; it is not an activation taken from a trained network.

Watch the moving filter and feature map in the YouTube Short.

1. The image and the filter

Our grayscale image is a 5×5 grid. Each row is identical. Here, 0 represents black and 255 represents white:

  0  255  255  255    0
  0  255  255  255    0
  0  255  255  255    0
  0  255  255  255    0
  0  255  255  255    0

The example filter has three identical rows. It subtracts the left side of each patch from the right side:

-1   0   1
-1   0   1
-1   0   1

This filter is sensitive to changes from left to right, so it responds to vertical boundaries. Different filters respond to different patterns.

2. Calculate the first output value

Place the filter over the upper-left 3×3 patch. Multiply each pixel by the weight at the same position and sum all nine products. Because the three rows in this particular patch are identical, we can show the calculation for one row and multiply its result by three:

One row: 0 × -1 + 255 × 0 + 255 × 1 = 255
Three rows: 255 + 255 + 255 = 765

The first output cell is therefore +765. Move the filter one pixel to the right and repeat. In the middle patch, all pixel values are 255, so the positive and negative contributions cancel and the output is 0. At the right boundary the response is -765.

3. From a moving filter to a feature map

We move one pixel at a time (stride 1) and do not add a border (no padding). The filter fits in three positions across and three down, so the 5×5 input produces a 3×3 output: (5 − 3 + 1) × (5 − 3 + 1).

 765   0  -765
 765   0  -765
 765   0  -765

These are signed raw responses. In this example, +765 and -765 indicate opposite directions of the same kind of brightness change. Zero appears for the uniform middle patch; it does not prove that every possible image patch with a zero response is uniform. Taking an absolute value, clipping values, or applying an activation changes how the output is interpreted.

4. Run the calculation in Python

You need Python and NumPy. If NumPy is not installed in your chosen environment, run python -m pip install numpy. Save the code below as cnn_filter_example.py and run python cnn_filter_example.py. No image file, model weights, or GPU is required.

import numpy as np

# Five identical rows make a simple grayscale image.
# Signed integers preserve negative results.
image = np.array([[0, 255, 255, 255, 0]] * 5, dtype=np.int32)
kernel = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]], dtype=np.int32)

# Valid cross-correlation: stride 1, no padding, no flipped kernel.
feature_map = np.zeros((3, 3), dtype=np.int32)
for row in range(feature_map.shape[0]):
    for col in range(feature_map.shape[1]):
        patch = image[row:row + 3, col:col + 3]
        feature_map[row, col] = np.sum(patch * kernel)

print('First patch:')
print(image[:3, :3])
print('First output value:', feature_map[0, 0])
print('Signed feature map:')
print(feature_map)
np.testing.assert_array_equal(feature_map, [[765, 0, -765]] * 3)

The assertion checks this particular fixture; it is not a test of a trained model's accuracy. Using a signed integer type matters: unsigned pixel types cannot represent -765 directly.

How does this relate to a CNN?

A convolutional layer applies filters to local patches to create feature maps. A real CNN can use multiple filters, input channels, biases, nonlinear activations and other layers. Its filter weights are usually learned during training. Here we chose the numbers ourselves to make the calculation easy to inspect; we did not train or run a CNN.

Strictly speaking, this example does cross-correlation: it does not flip the kernel before applying it. CNN libraries commonly call this operation convolution. The PyTorch Conv2d documentation describes the cross-correlation operation used by that layer.

Try it yourself

Reverse each filter row to [1, 0, -1] and run the script again after removing or updating the assertion. Can you predict the new signs? Keep the image and all other code unchanged. The positive and negative outputs should exchange signs, while the middle column remains zero.

Frequently asked questions

Why is the output smaller than the input?
With a 3×3 filter, stride 1 and no padding, the filter can start at only three rows and three columns of a 5×5 input. Padding and stride change the output dimensions.

Are CNN filters always edge detectors?
No. Filters are learned for the task; the patterns and their interpretation depend on the trained network, layer and input.

Does a negative value mean an error?
No. A negative value is a valid signed response. Here its sign reflects the direction of the brightness change relative to our chosen filter.

Continue learning

Watch the short animation to follow the moving window. Read the course overview on this blog. My English-language course Computer Vision: OpenCV, Machine Learning and YOLO includes a hands-on lesson on convolution. This is my instructor referral link.

No comments:

Post a Comment