Skip to content
Part IA Michaelmas Term

Image Memory Layout

Storing Images in Memory

A 2D image array must be stored in linear (1D) memory. The mapping from 2D coordinates to 1D memory index uses strides.

Row-Major vs Column-Major Order

Row-Major Order (Most Common)

Elements in the same row are stored contiguously: i(x,y)=x+yncolsi(x, y) = x + y \cdot n_{\text{cols}}

Column-Major Order

Elements in the same column are stored contiguously: i(x,y)=xnrows+yi(x, y) = x \cdot n_{\text{rows}} + y

Interleaved Colour Storage

For colour images with 3 channels (RGB), interleaved row-major: i(x,y,c)=x3+y3ncols+ci(x, y, c) = x \cdot 3 + y \cdot 3 \cdot n_{\text{cols}} + c

where c{0,1,2}c \in \{0, 1, 2\} for the R, G, B channels.

Memory layout showing row-major interleaved RGB storage

General Stride Formula

The most general formula for any memory layout: i(x,y,c)=istart+xsx+ysy+csci(x, y, c) = i_{\text{start}} + x \cdot s_x + y \cdot s_y + c \cdot s_c

where:

  • istarti_{\text{start}} is the starting offset
  • sxs_x, sys_y, scs_c are the strides for each dimension

Common Stride Configurations

Layoutsxs_xsys_yscs_c
Row-major, grayscale1nn-
Column-major, grayscalemm1-
Row-major, interleaved RGB33n3n1
Row-major, planar RGB1nnnmnm

Padded Images

Sometimes images are padded with extra pixels for operations that need neighbourhood access (e.g., convolution).

The stride sys_y is larger than the image width to account for padding.

Regions of Interest (ROI)

A region of interest is a rectangular subset of an image. An ROI borrows the parent’s data array without copying:

  • istarti_{\text{start}} = offset to ROI origin
  • Strides are inherited from parent

Padded image showing allocated memory, actual image, and ROI

Summary

  • Row-major order stores rows contiguously; column-major stores columns contiguously
  • Strides define the step size for each dimension
  • Interleaved RGB alternates channels; planar separates them
  • ROIs share parent memory without copying

Past Paper Questions

2023 Paper 3 Question 4: Design an ExImage class that supports ROIs. What constructor parameters are needed?