# Worksheet: Introduction to computer vision

*Tactiverge Academy · Computational Engineering, module 5 · https://tactiverge.win/academy/computational-engineering/#introduction-to-computer-vision*

Name: ______________________    Date: ____________

## Learning objectives

Tick each one when you can do it without looking at your notes.

- [ ] Describe a digital image as a grid of pixel values in one or more colour channels.
- [ ] Apply thresholding to separate an object from its background.
- [ ] Explain how an edge detector responds to changes in brightness.
- [ ] Identify common reasons vision systems fail, such as lighting changes and occlusion.

## Lesson 1: Images as arrays of numbers

A digital image is a grid of numbers. Learn how pixels, channels and resolution work by building and inspecting tiny images as Python lists.

Read online: https://tactiverge.win/academy/computational-engineering/introduction-to-computer-vision/images-as-arrays/

#### Project: Programming project: draw and measure your initials

Using Python 3 and only the standard library:

1. Create a greyscale image as a list of lists, at least 12 columns by 8 rows, that spells one of your initials in bright pixels (around 220) on a dark background (around 20).
2. Print it with the `show` function from this lesson and check it looks right.
3. Write a function that returns the mean brightness, and another that returns the number of pixels brighter than 128.
4. Downsample your image once with the `downsample` function (make the width and height even first). Print the result. Can you still read the letter? Write two sentences on what was lost.

**Deliverables**

- A Python file with your image and at least two functions of your own
- Printed output of the original and downsampled images
- Two or three sentences on the detail-versus-speed trade-off

### Check your understanding

1. In a list-of-lists image, what does image[3][7] refer to?
   A) The pixel at x = 3, y = 7
   B) The pixel in row 3, column 7
   C) The 37th pixel in the image
   D) The brightness of channel 3 at pixel 7

2. Why do many greyscale images use values from 0 to 255?
   A) Cameras cannot detect more than 255 brightness levels
   B) 255 is the number of pixels in a row
   C) One byte holds 2⁸ = 256 different values, numbered 0 to 255
   D) Human eyes can see exactly 256 shades

3. How many numbers does a 320 × 240 RGB image contain?
   A) 76,800
   B) 230,400
   C) 960
   D) 25,600

4. Pure green (0, 255, 0) converts to a much brighter grey than pure blue (0, 0, 255). Why?
   A) The weighted formula gives green a large weight because human eyes are more sensitive to green
   B) Green pixels are physically larger on the sensor
   C) Blue light is always filtered out by the camera
   D) It is a rounding error

5. You halve an image's width and height. Which statement is true?
   A) Processing takes about half as long, and no detail is lost
   B) The image has half the pixels and twice the detail
   C) Nothing changes except the file name
   D) The image has a quarter of the pixels, so it is faster, but detail smaller than about two original pixels is lost

### My notes

______________________________________________

______________________________________________

## Lesson 2: Colour spaces and thresholding

Separate an object from its background by brightness or by colour, and see why hue, saturation and value often work better than raw red, green and blue.

Read online: https://tactiverge.win/academy/computational-engineering/introduction-to-computer-vision/colour-and-thresholding/

### Check your understanding

1. A greyscale histogram has a peak around 40 and another around 190, with almost no pixels between 80 and 150. Which threshold is most sensible?
   A) 20
   B) 115
   C) 190
   D) 250

2. Why does a rule like "red channel above 150" often fail on shadowed parts of a red object?
   A) Shadows change the object's hue to blue
   B) The red channel only works outdoors
   C) Cameras cannot record red in shadows
   D) In RGB, dimmer light lowers all three channels, so a red surface in shadow can have a low red value

3. In HSV, which number changes most when the same surface moves from bright light into shadow?
   A) Hue
   B) Saturation
   C) Value
   D) None of them

4. Why does the red detector test hue < 15 OR hue > 345?
   A) Hue is measured around a circle, and red sits on both sides of 0°/360°
   B) To include green and blue as well
   C) Because colorsys returns negative hues
   D) It is a mistake; one test would be enough

5. What is the purpose of the saturation test (s > 0.5) in the red detector?
   A) It rejects very bright pixels
   B) It makes the code run faster
   C) It converts the image to greyscale
   D) It rejects greys and washed-out colours, whose hue is not meaningful

### My notes

______________________________________________

______________________________________________

## Lesson 3: Convolution and filters

Slide a small grid of weights across an image to blur away noise or pick out edges, and work through both by hand and in Python.

Read online: https://tactiverge.win/academy/computational-engineering/introduction-to-computer-vision/convolution-and-filters/

#### Project: Paper and Python project: filters by hand

1. On squared paper, draw a 6 × 6 greyscale image with a dark square in the middle of a bright background. Write a number in every cell.
2. By hand, apply the 3 × 3 box filter to at least four interior pixels, including two that sit on the square's edge. Show your working.
3. Apply the Sobel kernel to the same four pixels by hand.
4. Type your image into Python and use the `convolve` function to check your answers. If any differ, find out why.
5. Add one noisy pixel and compare the Sobel output with and without blurring first. Describe what you see in two or three sentences.

**Deliverables**

- Hand calculations for four pixels with both kernels
- Python output that confirms (or corrects) your hand results
- A short comparison of edge detection with and without blurring first

### Check your understanding

1. A 3 × 3 box filter is applied to a neighbourhood of eight pixels at 50 and one at 140. What is the output?
   A) 50
   B) 60
   C) 140
   D) 95

2. What is the main cost of smoothing an image with a blur kernel?
   A) It always makes the image brighter
   B) It removes colour
   C) It spreads out sharp edges and fine detail as well as noise
   D) It changes the image's resolution

3. Why does the Sobel kernel give exactly 0 on a perfectly uniform region of brightness 200?
   A) Because its weights add up to zero, so equal values cancel
   B) Because 200 is above the threshold
   C) Because Sobel only works on dark regions
   D) Because borders are skipped

4. Why do practical edge detectors usually blur an image before applying an edge kernel?
   A) Blurring makes edges sharper
   B) Edge kernels only work on blurred images
   C) Blurring reduces the image to black and white
   D) Edge kernels respond to differences, and noise creates many small differences that would look like edges

### My notes

______________________________________________

______________________________________________

## Lesson 4: Finding objects

Turn a binary mask into a list of objects: group connected pixels into regions, then measure each region's area, centroid and bounding box.

Read online: https://tactiverge.win/academy/computational-engineering/introduction-to-computer-vision/finding-objects/

#### Project: Programming project: count the coins

1. Draw a 12 × 10 binary mask in Python containing four "coins" of different sizes, plus two single-pixel specks of noise. Make two of the coins touch only at one diagonal corner.
2. Run `find_regions` and `describe` and print a table of regions.
3. Change the neighbour list to use 8-connectivity (add the four diagonals). How many regions do you find now? Explain the difference.
4. Choose a minimum area that removes the noise but keeps every coin. Then sort the remaining objects from largest to smallest area.
5. Add a fill-ratio calculation and use it to tell round-ish blobs from thin lines.

**Deliverables**

- A Python file with your mask, both connectivity versions and the filtering step
- Printed results for 4- and 8-connectivity
- A short explanation of your chosen minimum area

### Check your understanding

1. Two blobs touch only at one diagonal corner. How many regions does each rule find?
   A) One with 4-connectivity, two with 8-connectivity
   B) Two with 4-connectivity, one with 8-connectivity
   C) Two with both
   D) One with both

2. A region contains the pixels (0, 0), (0, 1), (1, 0) and (1, 1) as (row, col). What is its centroid?
   A) (1, 1)
   B) (0, 0)
   C) (0.5, 0.5)
   D) (2, 2)

3. In find_regions, why is each pixel marked as seen when it is added to to_visit?
   A) So that the same pixel cannot be queued or counted twice, which also ensures the loop ends
   B) To make the image brighter
   C) Because Python requires it for lists
   D) So that the bounding box is calculated correctly

4. You raise the minimum area from 3 to 50 pixels. What is the most likely risk?
   A) The program will crash
   B) More noise will be detected as objects
   C) The centroids will move
   D) Small or distant real objects will be discarded

5. An object's centroid is at pixel column 400. What does that tell the robot on its own?
   A) Where the object appears in the image, but not its position in the room without a camera model
   B) The object's distance in metres
   C) The object's exact position on the table
   D) The object's mass

### My notes

______________________________________________

______________________________________________

## Lesson 5: Camera geometry and the pinhole model

How a 3D point becomes a pixel: the pinhole camera model, focal length in pixels, estimating distance from size, and why every camera needs calibrating.

Read online: https://tactiverge.win/academy/computational-engineering/introduction-to-computer-vision/camera-geometry/

#### Project: Paper project: measure your own camera

You can do this with any phone or laptop camera, a ruler and Python.

1. Tape a sheet of paper with a 10 cm line drawn on it to a wall. Photograph it face-on from 50 cm, 100 cm and 150 cm, measured with a tape measure.
2. On a computer, zoom in on each photo and measure the line's length in pixels (most image viewers show the cursor position). Note the image width in pixels.
3. For each photo, calculate f = h · Z / H. Do the three values agree? Average them.
4. Use your f to predict the line's pixel length at 200 cm, then test the prediction.
5. Estimate how much a one-pixel measuring error changes your distance estimate at each distance.

**Deliverables**

- A table of distance, pixel length and calculated focal length
- Your prediction for 200 cm and the measured result
- A short note on which measurements you trust least, and why

### Check your understanding

1. With f = 500 px and cx = 320, a point at X = 0.2 m, Z = 2 m projects to which u?
   A) 370
   B) 420
   C) 320
   D) 520

2. Why can a single camera image not tell you how far away a point is?
   A) Because cameras do not record brightness
   B) Because the image is upside down
   C) Because every point along a line through the pinhole projects to the same pixel
   D) Because pixels are too large

3. An object 0.10 m tall appears 50 px tall through a camera with f = 600 px. About how far away is it?
   A) 0.6 m
   B) 3.0 m
   C) 1.2 m
   D) 12 m

4. A camera is swapped for one with a wider field of view but the same image width. What happens to f in pixels?
   A) It gets larger
   B) It gets smaller
   C) It stays the same
   D) It becomes negative

5. What does camera calibration with a checkerboard measure?
   A) The colours the camera can see
   B) The speed of the robot
   C) The brightness threshold for segmentation
   D) The camera's focal length, principal point and lens distortion

### My notes

______________________________________________

______________________________________________

## Lesson 6: When vision fails

Lighting changes, occlusion, motion blur and soft, shape-changing objects all break simple vision. See each failure in numbers, and why robots combine vision with other senses.

Read online: https://tactiverge.win/academy/computational-engineering/introduction-to-computer-vision/when-vision-fails/

#### Project: Programming project: a failure-mode report

Using the code from this module (Python standard library only):

1. Build a 12 × 12 greyscale test image of a bright object on a dark background, and a pipeline that thresholds it, finds the largest region and prints its area and centroid.
2. **Lighting:** multiply the image by gains from 1.0 down to 0.3 in steps of 0.1. Record at which gain the area first changes and at which the object disappears.
3. **Occlusion:** set columns of the object to the background value, one column at a time, and record how the centroid moves.
4. **Noise:** add random noise with `random.gauss(0, sigma)` for several values of sigma (use `random.seed(1)` so your results repeat). Record when false regions appear.
5. Write a one-page report listing each failure mode, the condition at which it appeared, and one idea (a different sensor, a different method or a change to the setup) that might reduce it. Label every number as coming from your synthetic test image.

**Deliverables**

- A Python file containing the pipeline and the three tests
- Tables of results for lighting, occlusion and noise
- A one-page failure-mode report with a mitigation idea for each failure

### Check your understanding

1. In the lighting example, the object went from fully detected at 70% light to completely missing at 50%. What does this suggest?
   A) The camera is broken
   B) Dimmer light always improves detection
   C) Fixed thresholds can fail abruptly with modest lighting changes, so lighting must be tested or controlled
   D) The threshold should be set to 255

2. A gripper finger hides the right half of an object. What happens to the centroid measured from the visible pixels?
   A) It stays at the true centre
   B) It shifts towards the right, behind the finger
   C) It shifts towards the visible left part, even though the object has not moved
   D) It cannot be calculated at all

3. With f = 600 px, Z = 1 m and an exposure of 1/60 s, how much blur does an object moving at 1 m/s cause?
   A) 1 px
   B) 60 px
   C) 0.1 px
   D) 10 px

4. Why is estimating distance from known size unreliable for a towel?
   A) Towels are always too far away
   B) A deformable object has no fixed size or shape, so its apparent size does not reliably reveal its distance
   C) Towels have no colour
   D) The pinhole model does not apply to cloth

5. Which statement about the TV-01 research question is accurate?
   A) It is a proposed study with no results, used here as an example of a question shaped by vision's failure modes
   B) It proved that force sensing fixes occlusion
   C) It showed that cameras are unnecessary
   D) It is a finished experiment whose data appear in the explorer

### My notes

______________________________________________

______________________________________________

---

## Answer key (for self-checking after you try)

**Lesson 1: Images as arrays of numbers**

1. B — Images are indexed row first, then column. Row 3 is the fourth row from the top, and column 7 is the eighth from the left, so in x, y terms this is x = 7, y = 3.
2. C — Eight bits give 256 combinations, so 0–255 fits a pixel in a single byte. Some cameras and formats use more bits per pixel for finer brightness steps.
3. B — 320 × 240 = 76,800 pixels, and each has three channels, so 76,800 × 3 = 230,400 numbers.
4. A — The weights 0.299, 0.587 and 0.114 reflect how bright each colour looks to people, so green contributes most and blue least.
5. D — Halving both dimensions leaves ½ × ½ = ¼ of the pixels. That speeds up processing roughly fourfold, at the cost of fine detail that cannot be recovered.

**Lesson 2: Colour spaces and thresholding**

1. B — A threshold in the empty valley between the two peaks separates them cleanly. 115 sits in the middle of the gap, so small changes in brightness will not push pixels across it.
2. D — RGB mixes colour and brightness together. The shadowed red (90, 16, 12) is still red, but its red channel is well below 150.
3. C — Value measures brightness. In the example, the red block's hue stayed at 3° and saturation stayed near 0.86, while value dropped from 0.86 to 0.35.
4. A — The hue wheel wraps from 360° back to 0°. Reds slightly towards blue have hues just below 360°, so a single range starting at 0° would miss them.
5. D — Grey pixels have saturation near 0, and their hue value is arbitrary. Requiring a minimum saturation keeps only pixels that genuinely have a strong colour.

**Lesson 3: Convolution and filters**

1. B — The box filter averages all nine values: (8 × 50 + 140) ÷ 9 = 540 ÷ 9 = 60.
2. C — Averaging cannot tell noise from real detail. A step edge becomes a ramp, which is why blur strength must be chosen with care.
3. A — The weights −1, −2, −1, 0, 0, 0, 1, 2, 1 sum to zero. Multiplying them by nine equal values gives a total of zero, whatever the brightness.
4. D — In the example, one noisy speck reduced the edge response from 720 to as low as 580. Smoothing first suppresses noise so that the remaining strong differences are real edges.

**Lesson 4: Finding objects**

1. B — 4-connectivity only links pixels that share an edge, so a diagonal touch does not join them. 8-connectivity counts diagonals as neighbours, merging the blobs into one region.
2. C — The average row is (0 + 0 + 1 + 1) ÷ 4 = 0.5, and the average column is the same. The centroid sits between the four pixels.
3. A — Without the seen check, neighbours would keep re-adding each other forever. Marking on entry means every pixel is processed exactly once.
4. D — A larger minimum removes more noise but also removes real objects whose regions are small, for example because they are far from the camera.
5. A — Pixel coordinates describe the image. Converting them to real-world positions needs the camera's geometry and usually some depth information.

**Lesson 5: Camera geometry and the pinhole model**

1. A — u = f · X / Z + cx = 500 × 0.2 ÷ 2 + 320 = 50 + 320 = 370.
2. C — Doubling both X and Z gives the same X / Z, and so the same pixel. Depth is lost in the division, and extra information such as known size or a second camera is needed to recover it.
3. C — Z = f · H / h = 600 × 0.10 ÷ 50 = 60 ÷ 50 = 1.2 m.
4. B — f = (W / 2) / tan(θ / 2). A larger θ makes tan(θ / 2) larger, so f falls. Each pixel then covers a wider angle.
5. D — Because the checkerboard's square size is known, software can solve for the camera parameters that best explain where the corners appear in many photos.

**Lesson 6: When vision fails**

1. C — Once the object's pixel values drop below the fixed threshold, they all disappear together. Small lighting changes can push a system past that point.
2. C — The centroid averages only the visible pixels. Hiding the right half moved it from column 3.5 to column 2.5 in the example.
3. D — blur = f · v · exposure / Z = 600 × 1 × (1/60) ÷ 1 = 10 pixels.
4. B — Z = f · H / h assumes the real size H is known and constant. Folding or crumpling a towel changes its size in the image without changing its distance.
5. A — TV-01 is a proposed study. It is an example of turning known failure modes, such as intermittent vision and deformable objects, into a testable question, not evidence for any answer.

---

This work is licensed under the Creative Commons Attribution 4.0 International licence (CC BY 4.0): https://creativecommons.org/licenses/by/4.0/. Attribute as: Tactiverge Academy, https://tactiverge.win.
