How a camera sees an edge

One pixel is a number. Nine numbers and nine weights make 643, an edge. You play first. The maths shows up last.

the picture: 256 numbers
after the recipe, 14 × 14

Switch between them. Both come from the same trick, nine weights slid over the picture. Only the weights differ. By lesson 3 you'll work out one of the edge numbers by hand: 643.

1What is one pixel, to a computer?

The reel started on one grey square with a number in it: 194. This lesson starts there too.

Still from the reel: nine pixel values times nine weights, written out as a sum that comes to 643

The reel this lesson picks up from: one pixel, its eight neighbours, nine weights, and 643.

Play

tap any pixel
it and its 8 neighbours

See it

A pixel is one square of a picture. To a computer, each pixel is just one number: how bright it is.

0 means black and 255 means white. Everything in between is a grey. The picture holds nothing else, so changing the numbers changes the picture.

Why stop at 255? Each number is stored in one byte. A byte has 8 on-off switches, so it can hold 28 = 256 different values: 0 to 255.

Think of a mosaic made of tiles. The comparison stops working in one place: a tile has a colour, but a pixel only has a number. The screen decides how to show it.

This picture is 16 × 16 = 256 pixels. It has a bright disc (about 200), a grey square (about 140) and a dark background (about 40). A little random noise is added, like a real camera.

The reel's pixel is at column 1, row 6. (Counting starts from 0 at the top left, as code does.) It reads 194. It sits on the left edge of the disc: dark on its left, bright on its right.

Surprising number: a 12-megapixel phone photo is 4000 × 3000 = 12,000,000 pixels. With red, green and blue (one number each), that's 36,000,000 numbers in one photo.

So: a picture is a big table of brightness numbers, one per pixel.

2What can nine weights do?

One number can't tell you if you're on an edge. You need its neighbours too.

Play

drag the orange window
the new picture, 14 × 14

Each box shows a pixel, its weight, and the two multiplied. The nine answers add up to one number in the new picture. For the edge weights, mid-grey means 0, lighter means positive and darker means negative. Blur and sharpen answers past 0 or 255 are cut off when drawn.

See it: 194 becomes 643

Here is the reel's pixel, 194, with its eight neighbours. Under each one sits a weight. A weight is just a number you multiply by.

Top: the pixel. Middle: its weight. Bottom: the two multiplied.

Multiply each pixel by its weight, one at a time:

  1. Top row: 35 × −1 = −35,   50 × 0 = 0,   203 × 1 = 203
  2. Middle row: 33 × −2 = −66,   194 × 0 = 0,   192 × 2 = 384
  3. Bottom row: 32 × −1 = −32,   37 × 0 = 0,   189 × 1 = 189
  4. Add all nine: −35 + 0 + 203 − 66 + 0 + 384 − 32 + 0 + 189 = 643

That's the reel's number. The nine weights together are called a kernel: a small grid of numbers you slide over the picture.

Now slide it. Move the window one pixel along and do the same nine multiplies again. The window needs a full ring of neighbours, so it fits 14 × 14 = 196 places. That's why the new picture is 14 × 14. Sliding a kernel over every place is called a convolution.

A kernel is like a recipe card: how much of each ingredient to use. The comparison breaks in one way: a recipe never uses −1 of an egg, but a kernel can.

Different weights, different effects

Blur. Give all nine pixels the weight 1/9. Multiplying by 1/9 and adding is the same as taking the average. Around 194 the nine pixels add up to 965, and 965 ÷ 9 = 107.2. The bright pixel got pulled down by its dark neighbours. Done everywhere, bright and dark get mixed: a blur.

Sharpen. Put 5 in the middle and −1 on the four sides. Around 194 that gives 658. That's past white, so it's drawn as white.

Why some kernels leave flat areas alone. Say all nine pixels are 100. Then the answer is 100 times the sum of the weights. Blur and sharpen weights add up to 1, so you get 100 back: a flat area stays the same. The edge weights add up to 0, so a flat area becomes 0.

The formula

The reel wrote all of that in its shortest form. Σ ("sigma") means "add them all up".

out = Σ wi pi = w1p1 + w2p2 + … + w9p9

Some textbooks flip the kernel upside down before sliding it. Code libraries don't, and nor does this page. For these kernels the flip changes nothing except the sign of the edge answers.

So: a kernel is nine weights; multiply, add, slide, and you get a new picture.

3Why is 643 an edge?

Put the window on 194 with "Edge ←→" and you get 643. Move it into the dark background and you get almost nothing. Why?

See it: right minus left

Look at the weights again: −1, 0, 1 on the top row, −2, 0, 2 in the middle, −1, 0, 1 at the bottom.

The middle column is all 0, so the middle pixels don't count. Each row takes its right pixel and subtracts its left one. So the kernel asks one question: how much brighter is the right side than the left?

  1. Top row: 203 − 35 = 168
  2. Middle row: 192 − 33 = 159
  3. Bottom row: 189 − 32 = 157
  4. Add them. The middle row counts twice, because it's closest to our pixel: 168 + 2 × 159 + 157 = 643

It's the same 643 as before, just added up row by row. A big positive number means "dark on the left, bright on the right". That's an edge.

It's like feeling for a step in the dark with your foot. You compare the floor on the left with the floor on the right. Where this comparison stops working: the kernel only feels in one direction at a time.

Why it works

On flat ground, right minus left is 0 in every row, so the answer is 0. The background's noise only makes small answers. A clean edge with three rows of 40, 40, 200 gives (1 + 2 + 1) × (200 − 40) = 4 × 160 = 640. Turn it round to 200, 40, 40 and you get −640: same edge, facing the other way.

A common mistake: "edge detection finds the bright parts"

It doesn't. It finds where brightness changes. The brightest pixel in the disc's middle is 209. The kernel there gives only −42: its neighbours are just as bright, so nothing changes. The dimmer 194 gives 643, because it sits where dark meets bright.

The other direction

Turn the kernel a quarter turn. Now it asks: how much brighter is the bottom than the top? That's "Edge ↕" in the Play box. Around 194 it gives only −43. Going down that part of the disc's edge, the brightness hardly changes. Call the two answers gx (left to right) and gy (top to bottom). Books call this pair of kernels the Sobel operator.

An edge can lean any way, so you combine the two answers. Picture them as the two short sides of a right-angled triangle. The edge's strength is the long side, by Pythagoras: √(643² + (−43)²) = √415,298 = 644.4. Almost all of it is left-to-right.

The formula

strength = √( gx² + gy² )

Work out the strength at all 196 places and you get the edge picture at the top of this page. That's the reel's "slide it everywhere" moment.

So: an edge kernel subtracts one side from the other, so it only answers where the brightness changes.

4How do edges turn into shapes?

An edge map is still just numbers. How do you get from there to "that's a round thing"?

Play

the picture
layer 2: lit where the total beats the bias

The four small maps are layer 1. Layer 2 adds them up at each pixel, takes away the bias, and keeps only what's left above 0. The dotted ring marks the disc's outline.

See it

Layer 1: four edge maps. The reel used four kernels: "Edge ←→", its opposite (every weight times −1), "Edge ↕" and its opposite. After each one, any answer below 0 becomes 0. This step is called ReLU: keep the positive part, throw away the rest.

So each map keeps only one kind of edge. The first lights the disc's left side; its value at our pixel is the 643. The second lights the right side, and so on. The biggest value in any of the four maps is 698.

Layer 2: two directions at once. At each pixel, add the four maps and take away 800. The 800 is the bias: the bar the total has to beat before anything comes out. Then apply ReLU again.

17 pixels light up, and every one is on the disc's curved rim. At each of them, both gx and gy are big (more than 250 each). That's because a curve changes sideways and up-or-down at the same time.

Work one by hand. At column 8, row 4, on the disc's rim, the four maps add to 1028. Then 1028 − 800 = 228, so it lights. At our pixel the maps add to 643 + 43 = 686. Then max(0, 686 − 800) = 0, so it stays dark. It's on a straight bit of edge.

The grey square never lights up. Its step is smaller: about 140 − 40 = 100, against the disc's 200 − 40 = 160. So even at its corners the total only reaches 650. Any bias from 700 to 900 gives the same 17 pixels. At 600, 34 light up. At 1000, only 2 are left.

Then shapes, then objects. Layer 2 doesn't know what a curve is. It only fires where edges in two directions meet. Stack more layers and each one combines what the layer below found. In a real network each layer also slides its own 3 × 3 window. So two layers see 5 × 5 pixels, and three see 7 × 7.

It's like reading: letters make words, and words make sentences. The comparison breaks here: nobody tells a network what the "letters" are. It finds its own.

A network built from stacked convolutions is called a CNN (convolutional neural network). The reel's third layer, "objects", was drawn to show the idea. It wasn't computed, and this page doesn't compute it either.

What this demo is, and what it isn't

We chose these kernels and the bias by hand, to show the idea. In a real CNN the weights and biases start random. They are then learned from many example pictures, and they often look much messier than ours. Nothing on this page says how well a real network works.

Where it's used. Blur and sharpen in photo apps are kernels like these. CNNs are used to read handwriting, to look for problems in medical scans, and in the cameras on self-driving cars. Those networks are far bigger than ours, and their weights are learned.

The formula

Each layer is lesson 2's formula with the bias and ReLU added. The reel wrote ReLU as σ:

F = σ( Σ wi pi − b ),   σ(x) = max(0, x)
two layers: σ(W2 ∗ σ(W1 ∗ p))

Why ReLU matters. Without it, two kernels in a row work out the same as one bigger 5 × 5 kernel. Stacking would add nothing new. This page's check script tests that on the picture. ReLU is what lets layer 2 say "both at once".

So: stack kernels with ReLU in between, and later layers find shapes made of edges.

Try it: 643 by hand, in code

Multiply nine pixels by nine weights and add them up. Python, nothing to install.

window = [[35,  50, 203],
          [33, 194, 192],
          [32,  37, 189]]
gx = [[-1, 0, 1],
      [-2, 0, 2],
      [-1, 0, 1]]
total = 0
for j in range(3):
    for i in range(3):
        total = total + gx[j][i] * window[j][i]
print('edge answer:', total)

Expected output:

edge answer: 643
  • It prints 643, the reel's number.
  • Swap in the "Edge ↕" weights, [[-1, -2, -1], [0, 0, 0], [1, 2, 1]]. Predict the answer before you run it. (It should be −43.)
  • Type in a 6 × 6 picture of your own with a dark half and a bright half. Slide the kernel over every place, and check the biggest answers land on your edge.

Show it with: your code, the output and your prediction.

Go deeper (optional)
Recap

A picture is a table of numbers, one per pixel: 194 is a light grey.

A kernel is nine weights. Multiply, add, and 194 with its neighbours becomes 643.

Edge weights subtract one side from the other, so only changes give big answers.

Stack kernels with ReLU in between and later layers find shapes. A CNN learns its own weights.

Build your first robot

HalfSmart, a 3D-printed 5-axis robot arm, is coming to Jig Pro as a full step-by-step build. Join the waitlist and we'll tell you when it opens.

Join the waitlist