By the end of this lesson you will be able to
- explain how a picture is stored as a grid of numbers, one for each pixel
- find any pixel with
picture[y][x], counting rows and columns from 0 - say what the numbers from 0 to 255 mean in a grey picture, and what the three numbers of a colour pixel mean
- use the vision workbench to load a picture from Bramble's camera, show it, and print values with
console.log - change a pixel, and make a small picture of your own
Numbers, not pictures
Eleanor Price holds up a printed photograph of the workbench. “So this is what Bramble sees?”
Henry Wren shakes his head. “That's what you see. Bramble never sees a picture at all. Its camera measures how much light arrives at thousands of tiny points, and hands the computer a long list of numbers. Everything Bramble knows about the workbench, it has to work out from those numbers. Today you'll look at the numbers for yourself.”
Pixels, rows and columns
A camera picture is divided into a grid of tiny squares called pixels (short for “picture elements”). Each pixel holds one measurement of light. The pictures from Bramble's cameras in this course are 64 pixels wide and 48 pixels high: 64 × 48 = 3072 pixels in all. That is far fewer than a phone camera, which keeps the numbers small enough for you to study.
In JavaScript, a grey picture is stored as a list of rows. Each row is itself a list of numbers, one for each pixel from left to right. The first row is the top of the picture.
Here is a tiny picture of a cup, 12 pixels wide and 10 high. Run it to see the picture and the numbers that make it:
Here is what each line does. camera("tinycup") fetches a picture. const cup = ... gives that picture a name, cup, so that the later lines can use it; const is the JavaScript word for “make a new name”. show(cup, "A tiny cup") draws the picture with a title, and showNumbers(cup) prints the numbers inside it as a grid.
Point at the picture (or tap it on a phone) to see the value of each pixel. Compare the picture with the grid of numbers. The dark outline of the cup is made of 60s, the pale inside of the cup is 200, and the background is 230.
Brightness from 0 to 255
In a grey picture, each pixel is one number that says how bright that point is:
- 0 means black: no light at all.
- 255 means white: as much light as the camera can measure.
- Numbers in between are shades of grey. The bigger the number, the lighter the grey.
Why stop at 255? Each pixel is stored in one byte of computer memory, and a byte can hold 256 different values: 0 to 255. That is enough steps for the eye to see a smooth change from black to white.
Finding a pixel: picture[y][x]
Every pixel has an address made of two numbers:
- x is the column: how far across from the left edge.
- y is the row: how far down from the top edge.
Both are counted from 0, not from 1. So the top-left pixel is at x = 0, y = 0, and in a 64 × 48 picture the bottom-right pixel is at x = 63, y = 47. Notice that y grows downwards, the opposite of a graph in a mathematics book. Nearly every camera and computer screen counts this way.
To read a pixel, you first pick the row, then the column inside that row:
picture[y][x]
The row comes first because the picture is a list of rows: picture[y] is one whole row, and picture[y][x] is one pixel in it. It is easy to get the order the wrong way round, so say it to yourself as “row, then column”.
In the tiny cup, what is the value at x = 8, y = 3?
Row 3 is the fourth line of the grid (rows 0, 1, 2, 3): 230 230 60 200 200 200 200 60 230 60 230 230.
Column 8 is the ninth number along that row (columns 0 to 8): 230. That pixel is inside the handle's loop, so it shows the background.
In JavaScript: cup[3][8] gives 230.
Use console.log(...) to print a value. You can print several values at once by separating them with commas:
The last line shows how to find the size of a picture. cup.length is the number of rows, which is the height. cup[0].length is the number of pixels in the first row, which is the width. The workbench also has two ready-made helpers, width(picture) and height(picture), that give the same answers.
Anything after // on a line is a comment: a note for people reading the program. The computer ignores it. The workbenches in this course use comments to explain what each line does.
Colour pixels: three numbers
A colour camera measures three amounts of light at every pixel: how much red, how much green and how much blue. Each amount is again a number from 0 to 255. So in a colour picture, each pixel is a small list of three numbers, [red, green, blue].
[255, 0, 0]is bright red,[0, 255, 0]is bright green, and[0, 0, 255]is bright blue.[0, 0, 0]is black and[255, 255, 255]is white.- When the three numbers are equal, the colour is a grey:
[128, 128, 128]is middle grey.
This is Bramble's head camera looking down at the workbench, where Eleanor has left a red cup, a blue box, a green ball and a spanner:
The middle of the cup gives about [132, 25, 28]: plenty of red, very little green or blue. The box gives far more blue than anything else, and the wood has a lot of red, a fair amount of green and less blue, which is what brown is made of. You will use numbers like these to find objects in Module 2.
To get one of the three numbers on its own, add a third pair of square brackets: bench[15][15][0] is the red amount, [1] the green and [2] the blue.
Changing a pixel, and making a picture
A picture is an ordinary JavaScript list, so you can change a pixel by giving it a new value with =. Here the tiny cup gets a black pixel in its top-left corner and a white stripe across its base row:
You can also make a brand-new picture. makePicture(width, height, value) makes a picture of the size you ask for, with every pixel set to the value you give. For a colour picture, give [red, green, blue] as the value.
Small pictures are drawn with big square pixels so that you can see each one. Bramble's camera pictures are drawn a little smaller, but they are made the same way.
A name made with const always refers to the same picture, but the pixels inside that picture can still be changed, as above. You cannot make the name refer to a different picture later. If you need to do that, make the name with let instead.
Exercises
Exercise 1 · One pixel of the cup
Print the value of the tiny cup's pixel at x = 3, y = 2 (just the number). Remember the order: row first, then column.
Exercise 2 · How big is the workbench picture?
Load the "workbench" picture and print its width and then its height, in that order. You can use width(...) and height(...), or .length.
Exercise 3 · The colour of the ball
The green ball on the workbench has its centre at x = 52, y = 34. Print the green amount of that pixel on its own (just one number).
Exercise 4 · Mend the cup's handle
Oliver wants a solid handle on the tiny cup. Set the two pixels inside the handle's loop, at x = 8 in rows y = 3 and y = 4, to 60, the dark value of the outline. Then show the picture.
Exercise 5 · A picture of your own
Make a new grey picture, 5 pixels wide and 5 high, with every pixel set to 0 (black). Then set the five pixels of the diagonal from the top-left corner to the bottom-right corner (where x and y are equal) to 255, and show the picture.
Quick check
Choose an answer to see whether you are right and why.
A grey pixel has the value 0. What does it look like?
0 means no light was measured, so the pixel is black. 255 is white, and the values in between are greys.
In a picture 64 pixels wide and 48 high, what are x and y for the bottom-right pixel?
Counting starts at 0, so 64 columns are numbered 0 to 63 and 48 rows are numbered 0 to 47. (0, 0) is the top-left pixel.
Which line reads the pixel in column 10 of row 5?
A picture is a list of rows, so you pick the row first (y = 5), then the column in that row (x = 10).
A colour pixel is [30, 40, 210]. What colour is it, roughly?
The three numbers are red, green and blue, in that order. Here the blue amount (210) is far bigger than the other two, so the pixel is blue.
Summary
- A camera picture is a grid of pixels. Bramble's pictures in this course are 64 pixels wide and 48 high.
- A grey picture is a list of rows, and each row is a list of numbers from 0 (black) to 255 (white).
picture[y][x]reads the pixel in row y and column x. Both count from 0, and y grows downwards.- The height is
picture.lengthand the width ispicture[0].length, or useheight(picture)andwidth(picture). - A colour pixel is three numbers,
[red, green, blue], each from 0 to 255. camera(name)loads a picture,show(picture)draws it,showNumbers(picture)shows its numbers,console.log(...)prints values, andmakePicture(width, height, value)makes a new picture.
Found a mistake on this page, or something unclear? Report a problem and mention “Robot Vision Lesson 1.2”.
