Sampling and Quantisation - iffatAGheyas/computer-vision-handbook GitHub Wiki
ποΈ Sampling & Quantization
(Understanding Image Resolution and Bit Depth) :contentReference[oaicite:0]{index=0}
When we digitise a real-world scene, we convert a continuous image into a grid of discrete pixels with discrete intensity (or colour) values. This process involves two key steps:
- Sampling β breaking the image into individual pixels
- Quantization β assigning each pixel a finite set of intensity values based on bit depth
1. Sampling: Breaking the Image into Pixels
Definition:
Sampling refers to how frequently we measure light intensity across the spatial dimensions of the image.
Why it matters:
- A high-resolution image (e.g. 1920Γ1080) has many pixels β finer detail
- A low-resolution image (e.g. 100Γ100) has fewer pixels β blocky appearance
Think of it like zooming in on a LEGOβ’ photo β more pieces = more detail.
Code Example: Simulating Sampling
import cv2
import matplotlib.pyplot as plt
# Load and convert to RGB
img = cv2.imread("bird.jpg")
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Preserve aspect ratio
h, w = img_rgb.shape[:2]
new_w = 50
new_h = int(h * (new_w / w))
# Downsample then upsample
low_res = cv2.resize(img_rgb, (new_w, new_h), interpolation=cv2.INTER_NEAREST)
upscaled = cv2.resize(low_res, (w, h), interpolation=cv2.INTER_NEAREST)
# Plot side by side
plt.figure(figsize=(15, 5))
for i, (title, im) in enumerate([
("Original", img_rgb),
(f"Downsampled ({new_w}Γ{new_h})", low_res),
("Upscaled to Original Size", upscaled)
]):
plt.subplot(1, 3, i+1)
plt.imshow(im)
plt.title(title)
plt.axis("off")
plt.tight_layout()
plt.show()
2. Quantization: Assigning Values to Pixels
Definition:
Quantization maps the continuous range of light intensities to a finite set of discrete values determined by the bit depth of the image sensor or format.
- 8-bit quantization: each channel can take values from 0 to 255
- Higher bit depth (e.g. 10β16 bits) allows smoother tonal transitions (reducing banding)
π Summary
Concept | Meaning | Key Impact |
---|---|---|
Sampling | Number of pixels in the image | Affects spatial resolution |
Quantization | Range of values a pixel can take | Affects intensity/grayscale levels |
Bit Depth | Number of bits used per pixel/channel | Higher bit depth β smoother gradients |