Computer Vision Basics
Teach a computer to "see" — turn images into numbers, sharpen and blur them by hand, then understand how convolutional neural networks (CNNs) recognise objects.
Learn Computer Vision Basics in our free AI & Machine Learning course — a beginner-friendly interactive lesson with worked examples, a practice exercise and…
Part of the free AI & Machine Learning course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
When you look at a dog, your eyes do not send "DOG" to your brain. They send millions of tiny light measurements. Your visual system then builds meaning in layers : first it spots edges (where light meets dark), then it groups edges into shapes (an ear, a snout), then it recognises textures (fur), and only at the end does it conclude "that's a dog."
A computer starts in exactly the same place: an image arrives as a grid of brightness numbers. A convolutional neural network (CNN) then rebuilds the same ladder — early layers find edges, middle layers find shapes, deep layers find whole objects. The whole lesson is about understanding that ladder, one rung at a time, starting from raw numbers.
A pixel ("picture element") is one dot of an image. In a grayscale image each pixel is a single number from 0 (black) to 255 (white), with greys in between. You store the whole image as a list of rows — a nested list — and read any pixel with image[row][col] .
A colour image adds a third dimension: every pixel becomes three numbers — Red, Green and Blue (RGB). That is why a 224×224 colour photo is 224 × 224 × 3 = 150,528 numbers . Run the worked example below to read pixels and measure an image's size.
Once an image is numbers, editing it is just arithmetic. Brightening adds a fixed amount to every pixel — but you must clip the result to the 0–255 range, because a pixel can never be darker than black or brighter than white. Thresholding turns the image pure black-and-white: any pixel at or above a cutoff becomes 255 , everything else becomes 0 . That is the simplest way to separate a bright object from a dark background.
Convolution is the single most important idea in computer vision, and it is much simpler than it sounds: slide a small grid of numbers (a "filter" or "kernel") across the image and combine the pixels underneath it. A 2×2 average filter replaces each region with the average of its four pixels — that softens the image (a blur ). The result is slightly smaller than the input, because the window cannot hang off the edge.
Swap the filter's numbers and the same sliding machinery does something else entirely. An edge-detection kernel gives a strong response wherever brightness changes sharply, and near zero across flat regions. That is the intuition behind "finding edges." The numpy example below applies a real edge kernel to a 5×5 image.
A CNN stacks the idea you just built. A convolution layer applies many filters at once — but instead of you choosing the numbers, the network learns them during training. Each filter produces a feature map : a grid showing where that pattern was found. Early layers learn edge filters, deeper layers learn shape and object filters — exactly the eye/brain ladder from the analogy.
A pooling layer (usually MaxPool 2×2 ) then shrinks each feature map by keeping only the strongest value in each 2×2 block. This throws away precise positions but keeps "was the feature here, roughly?", which makes the network smaller and more robust. Conv → Pool → Conv → Pool repeats until a Flatten turns the maps into a vector for a final Dense classifier.
In real frameworks you describe this stack in a few lines. The example below sketches an OpenCV pre-processing step (note the BGR→RGB fix) and a small Keras CNN. The shapes shrink layer by layer — read the Expected output to see how.
Almost every computer-vision product is one of these three jobs, in increasing difficulty:
These four mistakes trip up nearly every beginner. Spotting them saves hours.
Feeding raw 0–255 pixels into a network. The large values make training unstable and slow.
OpenCV's cv2.imread returns pixels in BGR order, but most models and display libraries expect RGB . Colours come out swapped (blue skies look orange).
CNNs expect every input to be the same shape (e.g. 224×224). Passing mixed sizes raises a shape error like expected (224,224,3), got (480,640,3) .
Flattening a raw image straight into a Dense layer creates millions of weights and overfits instantly.
✅ Fix: use Conv2D + MaxPooling2D first to shrink and share weights, and flatten only near the end.
Time to fly solo. Snap every pixel to the nearest of three levels ( 0 , 128 , 255 ) — a classic poster-art effect. The starter below gives only the brief and the data; you write the logic. Check yourself against the expected output in the comments.
Lesson 10 complete — you can make a computer see!
You now know that images are grids of numbers, you can brighten, threshold, blur and find edges by hand, and you understand how convolution, pooling and feature maps stack into a CNN that classifies, detects, or segments. The "magic" of computer vision is just arithmetic on pixels, repeated in layers.
🚀 Up next: Advanced Neural Networks — regularisation, batch normalisation, and the tricks that make deep models train reliably.
Practice quiz
In a grayscale image, what does a pixel value of 0 represent?
- White
- Red
- Black
- Transparent
Answer: Black. Grayscale pixels run from 0 (black) to 255 (white), with greys in between.
How many numbers represent each pixel in an RGB image?
- 3
- 1
- 2
- 4
Answer: 3. RGB stores three values per pixel — Red, Green, and Blue — so it has three channels.
What does convolution do?
- Sorts pixels by brightness
- Deletes the image edges
- Converts colour to grayscale
- Slides a small filter across the image and combines the pixels underneath
Answer: Slides a small filter across the image and combines the pixels underneath. Convolution slides a small kernel over the image, combining the pixels it covers to produce each output value.
An averaging (mean) filter applied to an image produces:
- Sharper edges
- A blur
- A rotation
- A brighter image only
Answer: A blur. A 2x2 average filter replaces each region with the mean of its pixels, softening (blurring) the image.
What does a max-pooling layer do in a CNN?
- Keeps the strongest value in each block, shrinking the feature map
- Adds more filters
- Normalises pixels to 0..1
- Flattens the image into a vector
Answer: Keeps the strongest value in each block, shrinking the feature map. MaxPool 2x2 keeps the largest value in each 2x2 block, shrinking the map while keeping the strongest signal.
Why do CNNs use far fewer parameters than a dense layer over a raw image?
- They use smaller images
- They skip the output layer
- A convolution shares one small filter across the whole image
- They ignore colour channels
Answer: A convolution shares one small filter across the whole image. Convolution shares a small filter across all positions, so it learns with thousands of weights, not millions.
What does an edge-detection kernel respond strongly to?
- Flat, uniform regions
- Places where brightness changes sharply
- The image corners only
- The darkest pixel
Answer: Places where brightness changes sharply. An edge kernel gives a strong response where brightness changes sharply and near zero across flat regions.
What does the Flatten layer do in a CNN?
- Blurs the feature maps
- Adds padding to the edges
- Converts RGB to grayscale
- Turns 2D feature maps into a 1D vector for the dense layer
Answer: Turns 2D feature maps into a 1D vector for the dense layer. Flatten reshapes the 2D feature maps into a 1D vector so a final Dense classifier can process them.
Which computer-vision task labels every individual pixel?
- Classification
- Segmentation
- Object detection
- Normalisation
Answer: Segmentation. Segmentation labels each pixel, giving an exact outline; classification gives one label, detection draws boxes.
Why normalise pixels to 0..1 before training a CNN?
- It makes images larger
- It converts colour to grayscale
- Large 0..255 values make training unstable and slow
- It is required to read the file
Answer: Large 0..255 values make training unstable and slow. Dividing by 255 scales pixels to 0..1, keeping gradients well-behaved so training is stable and faster.