Image Array Layout & Memory Structure
๐ง Why Image Array Structure Matters
In computer vision, bugs often come not from algorithms,
but from wrong assumptions about image memory layout.
Understanding array structure is essential for:
- Correct pixel access
- Fast processing
- Writing your own image loader
- Debugging strange artifacts
๐งฑ Image as Raw Memory
An image in memory is a 1D byte array, interpreted as 2D or 3D.
Conceptually:
1
memory[] โ rows โ pixels โ channels
But rows are not always tightly packed.
๐ Basic Image Dimensions
| Symbol | Meaning |
|---|---|
| W | Width (pixels) |
| H | Height (pixels) |
| C | Channels |
| B | Bytes per channel |
| RowBytes | Actual bytes per row |
| Stride | Distance between row starts |
๐ Stride (Pitch)
Stride = number of bytes between the start of two consecutive rows.
1
row y starts at: base + y * stride
Stride โฅ (W ร C ร B)
Why larger?
- Alignment
- Padding
- SIMD / GPU constraints
๐งฉ Padding
Padding = unused bytes at the end of each row.
Purpose:
- Memory alignment
- Faster access
- Hardware constraints
Padding bytes do not represent pixels.
๐ฆ Bitmap (BMP) Row Alignment Rule
The Rule
Each row in a BMP file must be aligned to 4-byte boundaries.
Meaning:
1
RowBytes = ceil((W ร bitsPerPixel) / 32) ร 4
Example: 24-bit BMP
- Width = 3 pixels
- 24 bits per pixel = 3 bytes
Raw row size:
1
3 ร 3 = 9 bytes
BMP requires multiple of 4:
1
2
Padding = 3 bytes
RowBytes = 12 bytes
Visual Layout
1
[B G R][B G R][B G R][P][P][P]
P = padding byte
๐ Bottom-Up Storage (BMP)
Most BMP files store rows bottom to top.
Memory order:
1
2
3
Last row
...
First row
If ignored:
- Image appears vertically flipped
๐จ Channel Interleaving
Interleaved (HWC)
1
[R G B][R G B][R G B]
Planar (CHW)
1
2
3
RRR...
GGG...
BBB...
BMP / PNG / JPEG files:
- Usually interleaved
Deep learning frameworks:
- Often planar
๐ฆ Pixel Size Calculation
Bytes per pixel:
1
PixelBytes = C ร B
Total memory (excluding padding):
1
H ร W ร C ร B
Actual memory:
1
H ร stride
โ ๏ธ Common Bugs
โ Assuming stride == width ร channels
- Fails on BMP
- Fails on aligned buffers
โ Ignoring bottom-up layout
- Flipped images
โ Forgetting padding when copying
- Shifted or torn images
โ Mixing RGB and BGR
- Color swapped output
๐งช Safe Pixel Access Formula
To read pixel (x, y):
1
address = base + y * stride + x * PixelBytes
Never assume contiguous rows.
๐ง Practical CV Insight
Most libraries expose:
widthheightchannelsstride(or step)
Always trust stride, not width.
๐ Summary Table
| Concept | Why It Exists |
|---|---|
| Stride | Row alignment |
| Padding | Hardware efficiency |
| Bottom-up | Legacy BMP |
| Interleaving | Simplicity |
| Planar | Vectorization |
๐ฏ Takeaway
Images are memory structures first, pictures second.
If you understand:
- Stride
- Padding
- Alignment
- Layout
You can:
- Write loaders
- Avoid silent bugs
- Optimize performance
This knowledge separates CV users from CV engineers ๐