Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 46 ยท Images as Data

Images as Arrays

Understand pixels, (H, W, C) shapes, uint8 versus float images, row and column coordinates, cropping with slices and drawing synthetic images with imshow.

A digital image is a rectangular grid of pixels (picture elements). Each pixel stores one number for brightness in a grayscale image, or a few numbers for color. Put the grid in a NumPy array and every vision operation becomes array code: cropping is slicing, brightening is arithmetic, finding bright regions is a comparison.

This lesson sets the vocabulary you will use for the rest of the course: shape, dtype, coordinates and value ranges. Most beginner bugs in vision code, from upside-down results to washed-out pictures, come from mixing these up. A bit of NumPy background helps; see Indexing and Slicing and Data Types if you want a refresher.

Shape: height, width, channels

A grayscale image is a 2D array with shape (H, W): H rows (the height) and W columns (the width). A color image adds a third axis for channels, giving (H, W, C), and C is usually 3 for red, green and blue (or 4 with a transparency channel).

flowchart LR
    A["Grayscale<br/>shape (H, W)"] --> B["img[r, c]<br/>one number"]
    C["Color<br/>shape (H, W, 3)"] --> D["img[r, c]<br/>three numbers"]
    C --> E["img[:, :, 0]<br/>a (H, W) channel plane"]

Grayscale pixels are single numbers; color pixels are short vectors along the last axis.

Be careful with the order: NumPy puts height first. A picture that is 640 pixels wide and 480 tall has shape (480, 640) or (480, 640, 3). Some libraries (PyTorch, for example) put channels first, (C, H, W); we always use (H, W, C) unless we say otherwise.

Python Shape, size and dtype

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last line shows why dtype matters: the same photo takes eight times more memory as float64 than as uint8.

Pixel values: uint8 and float

Two conventions dominate:

Convention dtype Range Black / white
Integer uint8 0 to 255 0 / 255
Float float32 or float64 0.0 to 1.0 0.0 / 1.0

Files and cameras give you uint8: compact and exact. Float images are better for math, because uint8 arithmetic wraps around (200 + 100 is 44, not 255). A common workflow is to convert to float on load, work in 0..1, and convert back to uint8 at the end.

Python Why you convert to float

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

When you convert back, round and clip before astype(np.uint8). Casting floats to uint8 truncates, and out-of-range values wrap around. The lesson Brightness, Contrast and Gamma covers this trap in depth.

Warning: An image with values 0..255 stored as float, or 0..1 stored as uint8, displays wrongly (almost all white or almost all black). When something looks off, print img.dtype, img.min() and img.max() first.

Coordinates: row first, then column

In math you write a point as (x, y), with x to the right and y up. In image arrays, you index img[row, column], so the vertical coordinate comes first, and row 0 is the top of the picture. That is why many image libraries use (x, y) for drawing but your array code uses [y, x].

flowchart TB
    subgraph Grid["img[row, col], origin at the top-left"]
      direction LR
      A["[0,0]  [0,1]  [0,2]"]
      B["[1,0]  [1,1]  [1,2]"]
      C["[2,0]  [2,1]  [2,2]"]
    end
    A --> B --> C

Row index grows downward, column index grows to the right.

Python Reading and writing single pixels

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Slicing: crops, strips and flips

A rectangular region of interest (ROI) is a slice: img[top:bottom, left:right]. Slices are views, not copies, so writing into a crop changes the original; call .copy() when you want independence (see Views vs Copies). Steps of -1 flip the picture.

Python Crop, flip and paste

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Building synthetic images

You do not need image files to practice. With np.mgrid (or np.indices) you get a row-index array and a column-index array, and from them you can write shapes as conditions: a disc is "distance from the centre is less than r". Assigning to the boolean mask draws it. Every example in this course starts from this idea.

Python Drawing a disc and a gradient with imshow

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Two imshow details to remember:

  • For a 2D array, set cmap="gray"; otherwise matplotlib applies a color map that makes grayscale data look colorful.
  • Pass vmin and vmax to fix the black and white levels. Without them, matplotlib stretches the data's own minimum and maximum across the display, which hides real brightness differences between two images.

For more about plotting arrays, see Arrays as Images and Plots.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A photo is 480 pixels tall and 640 pixels wide, in RGB. What is its NumPy shape?

    NumPy images use (height, width, channels), so rows (height) come first. The (3, 480, 640) layout is channels-first, used by some libraries but not by default in this course.
  2. 2. What does np.array([200], dtype=np.uint8) + 100 give?

    uint8 arithmetic wraps around modulo 256, so 300 becomes 44. Convert to float or use a wider dtype before doing arithmetic.
  3. 3. Which expression reads the pixel in row 10 and column 25?

    Image arrays are indexed [row, column], that is [y, x]. Swapping them is a common mistake.
  4. 4. You write crop = img[10:20, 10:20] and then crop[:] = 0. What happens to img?

    Basic slices share memory with the original array. Use .copy() if you want a separate crop.

Summary

  • A grayscale image has shape (H, W); a color image has shape (H, W, C), with height first.
  • Pixels are indexed img[row, col], with the origin at the top-left.
  • uint8 images use 0..255 and wrap around on overflow; float images conventionally use 0..1. Convert, compute, then round, clip and convert back.
  • Crops and flips are slices: cheap views that share memory unless you .copy().
  • np.mgrid plus boolean masks lets you draw test images without files; imshow with cmap="gray", vmin and vmax displays them faithfully.

Next, add color to the picture: Color Channels and Color Spaces.