Sub topic 3
Bit depth: how many values a sample can take
This chapter assumes you know that an image is a grid of pixels, that each pixel holds one or more numbers called samples, and that you can read basic NumPy code. It covers how many different values one sample can hold, what happens when a continuous brightness is forced into that range, and the mistakes that follow.
Bits, levels, and the largest value
A sample is stored in a fixed number of bits. That number is the bit depth. A sample with n bits can take 2**n distinct values, called levels. Unsigned integer storage numbers them from 0 to 2**n - 1.
| Bit depth | Levels | Largest value | Typical use |
|---|---|---|---|
| 1 | 2 | 1 | Binary masks, scanned text |
| 8 | 256 | 255 | JPEG, most PNG, web images |
| 10 | 1,024 | 1,023 | HDR video, some cameras |
| 12 | 4,096 | 4,095 | Raw camera sensors |
| 16 | 65,536 | 65,535 | Medical imaging, editing, some PNG and TIFF |
Bit depth is per sample, not per pixel. An 8-bit RGB image has three samples per pixel, so it uses 24 bits per pixel. It can show 256 × 256 × 256 = 16,777,216 colour combinations, but each channel still has only 256 levels.
for bits in (1, 8, 10, 12, 16):
print(bits, 2 ** bits, 2 ** bits - 1)
print(256 ** 3) # 16777216 colour combinations in 8-bit RGBQuantisation
Real scenes have continuous brightness. A sensor measures a continuous quantity, and the file must store it as one of a finite set of levels. Choosing the nearest level is called quantisation.
With L = 2**n - 1 as the top level, a brightness x between 0.0 and 1.0 maps to round(x * L). The spacing between neighbouring levels is 1 / L. Rounding to the nearest level means the error is at most half a step.
The code below quantises a smooth ramp to 3 bits (8 levels) and measures the error.
import numpy as np
def quantise(x, bits):
top = 2 ** bits - 1
return np.round(np.asarray(x) * top).astype(np.int64)
ramp = np.linspace(0.0, 1.0, 1000)
q = quantise(ramp, 3)
print(np.unique(q)) # [0 1 2 3 4 5 6 7]
restored = q / 7
print(np.abs(ramp - restored).max()) # at most 0.5 / 7, about 0.0714Quantisation is lossy. Once the values are rounded, the original fine differences cannot be recovered.
Why low bit depth shows up as banding
If a smooth gradient, such as a sky at dusk, has more distinct brightnesses than the sample can represent, neighbouring pixels collapse onto the same level. The eye then sees steps with visible edges. This is called banding or posterisation.
Banding is worse in dark regions of a gamma-encoded image than the level count suggests. Gamma encoding is covered in a later chapter. The point here is that 8 levels per channel is enough for many photos but can fall short for smooth gradients, heavy editing, and high dynamic range content. Editing makes it worse. Stretching the contrast of an 8-bit image spreads its existing levels apart, which leaves gaps in the histogram. Editing a 16-bit copy and converting to 8-bit at the end avoids this.
Bit depth is not the same as the storage type
The bit depth in a file and the data type in memory often differ.
- A 10-bit or 12-bit image is usually loaded into a 16-bit integer array. Its values still run from 0 to 1,023 or 0 to 4,095, not to 65,535.
- Floating-point images (
float32) are common in processing. Values are usually nominal 0.0 to 1.0, but they can go below 0 or above 1. This is useful for intermediate results and HDR data. - A 1-bit image is often loaded as a
booloruint8array, which uses a whole byte per sample in memory even though the file packs eight samples per byte.
Always check array.dtype and the actual array.max() before assuming the range.
Common errors
Integer wraparound
NumPy arrays of uint8 wrap around silently when an operation exceeds 255.
import numpy as np
a = np.array([200], dtype=np.uint8)
print(a + 100) # [44], because 300 mod 256 = 44
safe = np.clip(a.astype(np.int16) + 100, 0, 255).astype(np.uint8)
print(safe) # [255]Widen the type before arithmetic, then clip and convert back.
Wrong scale factor between depths
To move from 16-bit to 8-bit, divide by 65,535 and multiply by 255. Dividing by 255 is a common mistake. It leaves values far above 255, which then wrap. Mapping depths also means scaling by L, not by the level count: use 2**n - 1, not 2**n.
import numpy as np
x16 = np.array([0, 32768, 65535], dtype=np.uint16)
x8 = np.round(x16.astype(np.float64) / 65535 * 255).astype(np.uint8)
print(x8) # [ 0 128 255]Truncating instead of rounding
Converting a float image to uint8 with astype truncates toward zero, so 0.999 becomes 253 rather than 255 after scaling by 255 (254.745 truncates to 254). Round first, and clip to the valid range so out-of-range floats do not wrap.
import numpy as np
f = np.array([-0.1, 0.5, 0.999, 1.2])
u8 = np.clip(np.round(f * 255), 0, 255).astype(np.uint8)
print(u8) # [ 0 128 255 255]Confusing bits per pixel with bits per channel
"24-bit colour" means 8 bits per channel across 3 channels. "30-bit colour" means 10 bits per channel. A file described as "32-bit" might be 8-bit RGBA, or it might be a float32 grayscale image. Ask what the unit is.
Choosing a depth
- Use 8 bits for final delivery on the web and for most computer vision models, which usually expect 8-bit input or normalised floats.
- Use 10 to 16 bits when you need headroom for editing, when the source is a raw sensor, or when the signal range is narrow, as in medical scans.
- Use floats for intermediate processing, and convert to integers once at the end.
Each extra bit doubles the number of levels and, in uncompressed storage, grows the raw size: 16-bit samples take twice the memory of 8-bit samples.
Exercise
A camera pipeline stores 10-bit RGB images in a uint16 array. How many levels does each channel have, how many bits per pixel does the image carry, how many bytes per pixel does the array use, and what scale factor converts a stored value to the range 0.0 to 1.0?
Show answer
Each channel has 2**10 = 1,024 levels, with values 0 to 1,023. The image carries 3 × 10 = 30 bits per pixel. The uint16 array uses 3 × 2 = 6 bytes per pixel, because each sample occupies a whole 16-bit container. To convert to 0.0 to 1.0, divide by 1,023, not by 65,535 or 1,024.