What is computer vision?
Computer vision is the part of artificial intelligence (AI) that lets a computer work with pictures and video. AI means computer programs that do jobs that normally need human thinking. With computer vision, those jobs include sorting photos, spotting a stop sign, or flagging a scan for a doctor to look at.
The surprise is that a computer never "sees" anything. A camera turns light into numbers. Everything after that is math on numbers. The whole trip looks like this:
Let's walk through each step, and you will get to play with steps 2, 3 and 4 yourself.
How does a computer see a picture?
Zoom in far enough on any digital photo and you will see tiny squares. Each square is a pixel (short for "picture element"). A pixel is just one dot of color. A photo is thousands or millions of those dots in rows and columns, like graph paper.
The computer stores each pixel as a number. In a grayscale (black and white) picture, the number says how bright the dot is: 0 is pure black, 255 is pure white, and numbers in between are shades of gray. A color picture uses three numbers per pixel: how much red, green and blue light to mix. Those are the "RGB" values you may have seen when picking colors.
Lab 1 / Pixel editor
Paint on the 8 by 8 grid. The panel on the right shows the exact numbers the computer would store. Drag to paint, or use the arrow keys and Enter.
Start from an example
Try this: press Dark | bright edge and look at the numbers. The left side is all low numbers and the right side is all high numbers. Right in the middle, the numbers jump. Hold that thought. That jump is how a computer finds an edge.
Here is how big the real thing is. A modest 1,000 by 1,000 pixel photo has one million pixels. If it is in color, that is three million numbers for one picture.
How does a computer find edges and shapes?
An edge is the line where one thing stops and another starts, like the outline of a ball against the grass. To a computer, an edge is not a line. It is a place where the numbers change a lot between neighbors.
ROW OF 10 PIXELS // BOTTOM ROW = CHANGE FROM THE PIXEL BEFORE IT
Most neighbors differ by only a few points. Between the fifth and sixth pixel the number jumps by 185. That big jump is the edge. Do this for every pixel and you get an outline map of the picture.
Edges are only the start. Vision systems stack simple patterns into more complex ones:
- edges
- corners and curves
- parts, like a wheel
- whole objects, like a bike
Think of building with LEGO. Single bricks (edges) make walls (shapes), walls make a house (an object). Next you will use the tool that finds those patterns.
What is a filter in computer vision?
A filter (also called a kernel) is a tiny grid of numbers, usually 3 by 3. It slides across the picture one pixel at a time. At every stop it does the same small job:
- Lay the filter over the 9 pixels underneath it.
- Multiply each pixel by the filter number on top of it.
- Add up all 9 results. That total is one pixel of the new picture.
This slide-multiply-add trick is called convolution. Different filter numbers find different patterns. A filter with all the same small numbers blurs. A filter with a big center and negative neighbors sharpens. A filter that subtracts the left side from the right side finds vertical edges.
Lab 2 / Filter lab
The picture on the left is a small 12 by 12 grayscale image: a bright square and a gray circle on a dark background. Pick a filter, or type your own numbers. Click or arrow through the output to see the math for any pixel.
Choose a filterThings to try: pick Edge detect and notice the flat areas go black while the outlines glow. Turn on noise, then pick Blur and watch the speckles fade. Then pick Sharpen and see the speckles get louder. Finally, type your own numbers and see what you can invent.
Here is the key point for AI. In an old-style program, a person picks filter numbers by hand, like you just did. In a modern vision model, called a convolutional neural network, the model learns its own filters from examples. It may use thousands of them in layers, so early layers find edges and later layers find parts and objects. To learn more about how those layers work, read What Is a Neural Network?.
How does a model learn to recognize things?
Nobody types out rules like "a cat has pointy ears and whiskers." Instead, people collect thousands or millions of example pictures and label them, meaning they attach the right answer, such as "cat" or "not a cat". This is called training data.
The model starts out guessing. After each guess it checks the label, sees how wrong it was, and nudges its inner numbers to be a little less wrong next time. Repeat that a huge number of times and the model gets good at the patterns that go with each label. That loop is the heart of how computers learn.
A model learns from features, which are measurable clues in a picture, such as color, shape, or the background. Big models find their own features. In the lab below, you choose the features for a tiny model, so you can see what happens when the clues are good or bad.
Lab 3 / Teach the model
Goal: teach a model to tell apples from bananas. It trains on the 10 labeled photos below, then gets tested on 7 new ones. Pick which clues it may use.
Training photos (labeled)The shortcut trap: in these training photos, every banana sits on a blue plate and every apple on white. A model that picks Background scores perfectly in training, because it found an easy shortcut, then fails on new photos. Real models do this too. A model trained to spot a husky in snow may really have learned "snow".
Where is computer vision used?
You probably use computer vision every day without noticing. Here are four common places, described in a general way. Details vary a lot between products.
Photo apps
Your phone can group pictures by what is in them, such as beaches, pets or food. That is how you can search "dog" and get dog photos.
Self-checkout
Some self-checkout lanes use cameras to recognize fruit and vegetables, or to notice when an item was not scanned.
Medical scans
Software can point out spots on X-rays and other scans that may need a closer look. A trained doctor still makes the decision.
Driver assistance and self-driving
Cameras help cars find lane lines, signs, people and other vehicles. Cars usually combine cameras with other sensors, such as radar.
One more use you may have heard of: recognizing a specific person's face. It is a special and sensitive case, so it has its own tutorial: How Does Facial Recognition Work?
What can go wrong with computer vision?
Computer vision is powerful but fragile. It matches number patterns. It does not understand what it is looking at. That causes these problems:
- // LIGHTING AND ANGLEThe same object can produce very different numbers in bright sun, in shadow, at night, or when it is blurry or tilted. A model that only saw sunny photos may struggle in the dark.
- // UNREPRESENTATIVE TRAINING DATAA model can only learn from what it was shown. If the examples leave out certain people, places or conditions, the model tends to do worse on them. Researchers have found this with some face-analysis tools, which worked less well for groups that were under-represented in the training pictures.
- // SHORTCUTSAs you saw in Lab 3, a model may learn the background instead of the object, and nobody notices until it fails on new photos.
- // NO COMMON SENSEA model does not know what a "ball" is for. It only knows which patterns tended to carry that label.
The fix starts with people: collect varied training data, test on many kinds of situations, and keep a human in the loop for important decisions. See AI Bias and Fairness for how bias gets in and what to do about it.
What about privacy?
Computer vision needs pictures, and pictures can show people, homes, school buildings and where you were. Photos can also carry hidden details, like the time and sometimes the place they were taken. Good questions to ask about any camera-based system:
- Who is collecting the images, and why?
- Are they saved, and for how long?
- Did the people in the pictures know, and could they say no?
- Who else can see the pictures or the results?
Using a camera to find ball shapes is very different from using it to identify people. The more personal the use, the more care it needs.
Common questions about computer vision
What is computer vision?
Computer vision is the part of artificial intelligence that lets computers work with pictures and video. A computer turns an image into a grid of numbers, looks for patterns in those numbers, and uses the patterns to do a job such as sorting photos, reading a road sign, or flagging a scan for a doctor to check.
Do computers see images the way people do?
No. A computer does not see a picture. It receives a grid of numbers, one for each tiny dot called a pixel. It has no understanding of what the picture means. It only finds number patterns that match patterns it learned from many labeled examples.
How does a computer learn to recognize objects?
A model is shown thousands or millions of example images that people have labeled, such as cat or not cat. After each guess, it adjusts itself to make fewer mistakes. Over time it picks up the patterns that go with each label. If the examples are narrow or unfair, the model can learn the wrong patterns.
Is computer vision the same as facial recognition?
No. Facial recognition is one use of computer vision. Computer vision also covers many other jobs, such as spotting objects, reading text in photos, and finding problems in medical images. Read more in How Does Facial Recognition Work?
// Key takeaways
- A computer sees a picture as a grid of numbers, one per pixel (three per pixel in color).
- An edge is a place where neighboring numbers change a lot.
- A filter is a small grid of numbers that slides over the picture to find a pattern.
- A model learns from labeled examples, and it can pick up shortcuts if the examples are narrow.
- Lighting, unrepresentative data and privacy are the big issues to watch.
Quick quiz
1. What does a computer actually receive when it "looks" at a photo?
2. What does an edge-detecting filter highlight?
3. A model trained on banana photos that all had a blue plate fails on a banana on a white table. What is the most likely reason?
Keep learning about AI
Computer vision is one way AI learns patterns from data. Go deeper with What Is a Neural Network?, see the big picture in What Is AI?, or explore the learning loop in How Do Computers Learn?. For the human side, read AI Bias and Fairness and How Does Facial Recognition Work?.
Want a full course? Browse the CodeHS courses to find AI and cybersecurity options, or see the CodeHS AI page.