Loading
0x40Lesson 5 of 16

Detect patterns with image filters

Slide a small grid of weights over an image to blur, sharpen and find edges.

22 min 6-question quiz 2 code exercises
By the end of this lesson you can
  • Compute one convolution output by hand
  • Predict what blur, sharpen and edge kernels do
  • Connect convolution to the filters a neural network learns

Blur, sharpen, “find edges”: these photo-editor effects are all one operation, convolution. A kernel - a small grid of weights, usually 3×3 - slides over the image. At each position, every pixel under the kernel is multiplied by its weight and the products are added up. That sum is the output pixel.

one_position.py
1patch = [
2    [10, 10, 200],
3    [10, 10, 200],
4    [10, 10, 200],
5]
6kernel = [
7    [-1, 0, 1],
8    [-2, 0, 2],
9    [-1, 0, 1],
10]
11total = sum(patch[row][column] * kernel[row][column] for row in range(3) for column in range(3))
12print(total)
Output
760

That kernel subtracts the left column from the right column: across a sharp dark-to-bright change it gives a big number (760), while in a flat region the products cancel out to 0. That is an edge detector.

Try it

Up to 15 XP

Kernel playground

Pick a kernel, then click any pixel of the output to see exactly how it was computed.

  • Blur averages each 3×3 neighborhood. What happens to the square’s sharp corners?
  • Vertical edges and Horizontal edges each find only some of the square’s sides. Which ones? (Orange means negative: a bright-to-dark change.)
  • Edit the kernel weights yourself. Can you make a filter that only finds the diagonal line?
Input (click a pixel in the output)
Kernel
Edit any weight to make your own filter.
Output (orange = negative)

Output at row 6, column 6 = sum of (pixel × weight) over the highlighted 3×3 window (pixels outside the image count as 0):

220×0 + 220×0 + 220×0 + 20×0 + 20×1 + 20×0 + 20×0 + 20×0 + 220×0 = 20

Key takeaways

  • Convolution slides a kernel over the image; each output is a weighted sum of a neighborhood.

  • Weights that average → blur; weights that subtract neighbors → edges; a boosted center → sharpen.

  • Edges at the image border need padding (here, pixels outside count as 0).

Lesson quiz

6 questions · pass with 5 correct · up to 50 XP

Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.

Practice: apply computer vision with Python

Use small pixel arrays to explore vision concepts, run your code against sample images, and connect each result to the larger computer vision idea.

Exercise 1

Find the strongest neighboring change

+25 XP

Read a JSON list of at least two grayscale values. Find the largest absolute difference between adjacent values and print it.

  • One strong edge
  • Small variations
main.py
Loading editor…

Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.

Exercise 2

Convolve an image

+25 XP

Read a JSON grayscale image and, on the next line, a JSON 3×3 kernel. Apply the kernel at every position where it fits entirely inside the image (no padding), and print the output image as a list of rows. The output is 2 smaller in each direction.

  • Vertical edge
  • Sum a 3x3 block
main.py
Loading editor…

Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.

Questions about this lesson

Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.

Loading posts…