CoursesAIMathCodePhysicsAbout and FAQ

TwiddleLab / Artificial Intelligence / How Machines See and Hear

Filters Find Edges

Lesson 2 of 10 · 7 min · Free, no account needed

What you will learn

  • A filter (or kernel) is a tiny grid of numbers, like 3 × 3.
  • A convolution slides the filter over the picture: multiply, add, write the sum, move one step.
  • The result is a feature map: bright where the filter's pattern is. Change the numbers, change what it finds.
  • In code, a convolution is two loops, one inside the other.

Start the lesson

How the lesson goes

  1. Your eyes find a critter's outline first. Can plain numbers find an edge?

    Take each pixel minus the one to its left. Where do you get the biggest number? Tap that gap, then Check.

  2. Here's the rule as a tiny filter: -1 and +1. Slide it along the row.

  3. Now in two dimensions. Drag the 3 × 3 window over the critter.

    -1 on the left column, +1 on the right. Watch the output map fill in.

  4. This trick runs inside every vision model.

    Tap to reveal its parts.

    The tiny grid of numbers is a filter, also called a kernel.

    Multiply, add, slide one step, repeat. That's a convolution.

    The result is a new grid called a feature map. Bright means: my pattern is here.

    The same 9 numbers are used at every spot. That keeps it small and fast.

  5. Build your own filter: make it find the top and bottom of the critter.

    Tap cells to set them to +1, -1 or 0. Your map updates as you go. Check at 80% match.

    A filter fires where the picture matches its pattern of minus and plus. -1 above and +1 below gives a big sum only where the pixels above are darker or lighter than the ones below: a horizontal edge.

  6. Game: ear lines. Make your map match the target in as few taps as you can.

    You start from your filter. The target lights up slanted edges.

  7. Four filters made these four maps. Put each filter on its map.

    Drag a filter onto a map, or tap a filter and then a map.

    Edge filters have -1s and +1s that cancel on flat areas, so their maps are dark except at edges. Blur (all 1s) averages neighbors. Sharpen (5 in the middle, -1 around) boosts the difference from neighbors.

  8. Code: a convolution is two loops, one inside the other.

    Before each Step, tap the cell where the window's center goes next.

  9. Which filter finds the top and bottom edges?

    Read the numbers, pick a filter, then Check.

    Filter A compares left with right (vertical edges). Filter B adds everything equally (blur). Filter C puts -1 above and +1 below, so it fires on horizontal edges.

  10. Before and after: run your filter on a bigger photo.

    Switch filters and flip between Before and After.

Previous lesson: A Picture Is a Grid of Numbers · Next lesson: Stacking Filters: Convolution Layers · Part of the course: How Machines See and Hear

Lessons are interactive and need JavaScript. Turn it on to play them.

Loading TwiddleLab