Introduction
Computer vision is the part of computer science that lets machines make sense of pictures. Your phone unlocks when it sees your face, a car keeps to its lane, a scanner reads a barcode, and a hospital program marks a shadow on an X-ray. Every one of these systems starts from the same simple fact: an image is a grid of numbers.
This course builds computer vision from that fact upwards. You will filter images, find edges, separate objects from the background, measure and match shapes, and finally see how classifiers and convolutional networks learn to recognise what is in a picture. Everything runs in plain Python in your browser, on images small enough to check by hand. Real libraries such as OpenCV, Pillow and PyTorch cannot run on this site; when they appear, they are shown as text so you know what the real tools look like.
How it works
A greyscale image is a rectangle of pixels. Each pixel holds one intensity: 0 is black, 255 is white, and the numbers in between are greys. The range 0 to 255 comes from storing each pixel in one byte (uint8), which is how almost every camera and file format stores images.
In Python, the simplest image is a list of rows, where each row is a list of numbers:
img = [
[0, 128, 255],
[64, 64, 64],
]
This image has height 2 (two rows) and width 3 (three columns). Its resolution is 3 × 2 (width first, as cameras write it), but in code you meet the height first, because the outer list holds the rows.
Coordinates. Image coordinates start at the top-left corner. The x coordinate grows to the right and picks a column; the y coordinate grows downwards and picks a row. So the pixel at x = 2, y = 0 is img[0][2], which is 255 here. Remember the pattern img[y][x]: row first, then column. This is the opposite order to how x and y are usually written, and it is the most common source of bugs in image code.
Simple operations.
- Crop: keep a rectangle. Rows 1 to 3 and columns 2 to 5 are
[row[2:6] for row in img[1:4]]. - Mirror (flip left to right): reverse every row,
[row[::-1] for row in img]. - Flip upside down: reverse the list of rows,
img[::-1]. - Negative: replace every value v by 255 − v, so dark becomes bright.
- Brightness: add a number to every pixel, then clamp the result into 0 to 255 with
min(255, max(0, v)).
Seeing an image as text. A small image can be printed with a ramp of characters from dark to bright, such as .:-=+*o#%@. A value v from 0 to 255 picks character number v × 10 // 256, which is always between 0 and 9.
Example
RAMP = ".:-=+*o#%@" # 10 characters, from dark to bright
def shade(img):
for row in img:
print("".join([RAMP[v * 10 // 256] for v in row]))
img = [
[10, 30, 60, 90, 120, 150, 180, 210],
[10, 30, 60, 90, 120, 255, 255, 210],
[10, 30, 60, 90, 120, 255, 255, 210],
[10, 30, 60, 90, 120, 150, 180, 210],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
]
height, width = len(img), len(img[0])
print("height", height, "width", width, "pixels", height * width)
print("row 1, column 5 holds", img[1][5])
shade(img)
negative = [[255 - v for v in row] for row in img]
print("negative of row 0:", negative[0])
crop = [row[4:8] for row in img[0:3]]
print("crop of rows 0-2, columns 4-7:", crop)
mirror = [row[::-1] for row in img]
print("mirrored top half:")
shade(mirror[0:3])
total = sum([sum(row) for row in img])
print("mean brightness:", round(total / (height * width), 2))Line by line
RAMPholds ten characters from dark (.) to bright (@).imghas 6 rows of 8 values: a sky that brightens from left to right, a bright sun (255) in rows 1 and 2, columns 5 and 6, and two black rows of ground.len(img)is the height andlen(img[0])is the width, so the image has 48 pixels.img[1][5]reads row 1, column 5: the sun, 255.shadeturns each value into one character. 10 becomes., 120 becomes+and 255 becomes@, so the sun and the black ground stand out.- The negative of row 0 is 255 minus each value: 10 becomes 245 and 210 becomes 45.
- The crop takes rows 0 to 2 (
img[0:3]) and, from each, columns 4 to 7 (row[4:8]): a 3 × 4 piece that contains the sun. - Reversing every row mirrors the picture, so the sun moves to the left side.
- The mean brightness is the total 3760 divided by 48 pixels: 78.33. The black ground pulls it down.
Expected result
height 6 width 8 pixels 48
row 1, column 5 holds 255
.:-=+*#%
.:-=+@@%
.:-=+@@%
.:-=+*#%
........
........
negative of row 0: [245, 225, 195, 165, 135, 105, 75, 45]
crop of rows 0-2, columns 4-7: [[120, 150, 180, 210], [120, 255, 255, 210], [120, 255, 255, 210]]
mirrored top half:
%#*+=-:.
%@@+=-:.
%@@+=-:.
mean brightness: 78.33
In the real world
A 12-megapixel phone photo is a grid of about 4000 × 3000 pixels, and every filter in a photo app works on such grids. Python programs load images with Pillow or OpenCV and hold them as NumPy arrays. The same operations look like this (shown as text: these libraries do not run on this site):
from PIL import Image
import numpy as np
img = np.array(Image.open("photo.jpg").convert("L")) # greyscale: shape (height, width), dtype uint8
print(img.shape, img.dtype)
print(img[100, 250]) # row 100, column 250
crop = img[0:100, 200:400] # rows 0-99, columns 200-399
mirror = img[:, ::-1]
negative = 255 - img
NumPy writes img[y, x] with a comma, but the order is the same: row first.
Common mistakes
- Swapping x and y.
img[x][y]reads the wrong pixel, or crashes when the image is not square. - Thinking y grows upwards. In images, row 0 is the top.
- Letting values leave 0 to 255. In NumPy, uint8 arithmetic wraps around: 250 + 10 becomes 4. Clamp or use a wider type.
- Building a blank image with
[[0] * w] * h. That repeats one row object h times, so changing one row changes them all. Use[[0] * w for _ in range(h)].
Best practices
- Print the height and width as soon as you load an image.
- Name loop variables
randc(oryandx) and keep the orderimg[r][c]everywhere. - Build a new image with a comprehension instead of changing the original in place, so you can compare before and after.
- Test every operation on a tiny image you can check by hand.
Summary
A greyscale image is a grid of intensities from 0 (black) to 255 (white), stored as a list of rows. Coordinates start at the top-left, with y going down, so a pixel is img[y][x]. Cropping and flipping are slices, the negative is 255 − v, and brightness changes need clamping. A ramp of characters turns a small image into text you can read.
Quick check
Key takeaways
- A greyscale image is a list of rows, and each row is a list of brightness values from 0 to 255.
- Index an image as img[row][column], which is img[y][x]: the row comes first.
- Cropping, mirroring and inverting are slices and simple arithmetic on the grid.
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.