Images as Functions

Images as 2D Functions

A grayscale image is a function \(f(x, y)\) mapping from \(\mathbb{R}^2\) to \(\mathbb{R}\), where the output is the intensity at position \((x, y)\). Visualizing this function as a 3D surface (height = intensity) makes the analogy concrete: bright regions are peaks, dark regions are valleys.

Image processing operates on these functions — for example, smoothing the function corresponds to blurring the image.

Bounded Domain and Range

Images have finite extent and intensity range:

\[f : [a, b] \times [c, d] \to [\text{min}, \text{max}]\]
  • \(x \in [a, b]\), \(y \in [c, d]\) — spatial bounds

  • Intensity range is typically \([0, 255]\) for 8-bit images (an artifact of byte-sized storage; \([0, 1]\) is equally valid). Later, image derivatives and other computations require negative values.

Color Images

A color image is a vector-valued function:

\[f : \mathbb{R}^2 \to \mathbb{R}^3\]

Each pixel maps to a vector of three values (e.g., R, G, B or L, u, v). Equivalently: \(f : \mathbb{R}^2 \to \mathbb{R}^3\).

Digital Images

Continuous functions must be discretized for computation:

  1. Sampling — evaluate on a regular grid of pixels (picture elements).

  2. Quantization — represent each value with a finite number of bits (e.g., 8-bit → 0–255, 12-bit, 16-bit).

Images are stored as matrices. Indexing conventions:

  • Matrix notation: img(i, j) = row i, column j (i down, j across).

  • Coordinate notation: \(f(x, y)\) where x is horizontal (column) and y is vertical (row) — the axes are swapped relative to matrix indexing.

Image size for bounds \(x \in [x_1, x_2]\), \(y \in [y_1, y_2]\):

  • Width = \(x_2 - x_1\), Height = \(y_2 - y_1\)

  • Area = Width × Height = total pixels

  • A color image with 3 channels requires 3 × Area bytes (at 1 byte/channel).

Note

Always use floating-point images for computation. Integer arithmetic (e.g., uint8) silently clips and truncates values.

MATLAB/Octave Basics

img = imread('peppers.png');      % load image
imshow(img);                       % display
green = img(:, :, 2);             % extract green channel (1=R, 2=G, 3=B)
plot(green(256, :));              % plot intensity profile of row 256

MATLAB uses 1-based indexing: channel 1 = Red, 2 = Green, 3 = Blue.

Image Arithmetic

Addition and Averaging

Adding two same-sized images is element-wise. Direct addition often causes clipping at 255 (for uint8), producing washed-out results.

Averaging (dividing each image by 2 first, then adding) avoids overflow:

result = img1 / 2 + img2 / 2;

Warning

With uint8, the order of operations matters. Adding first and dividing later ((img1 + img2) / 2) clips intermediate sums at 255, losing information. Always divide before adding, or convert to floating point.

Scalar Multiplication

Multiplying by a scalar scales intensity: values > 1 brighten, values < 1 darken. Values exceeding 255 clip for uint8.

function out = scaleImage(value, image)
    out = value .* image;
end

Alpha Blending

A weighted combination of two images with weights summing to 1:

\[\text{blend}(A, B, \alpha) = \alpha \cdot A + (1 - \alpha) \cdot B\]
function out = blend(a, b, alpha)
    out = alpha .* a + (1 - alpha) .* b;
end

Image Subtraction

uint8 subtraction clips negative results to 0, making naive abs(a - b) identical to a - b. Workarounds:

  • Use (a - b) + (b - a) — each term is correct where its operand is larger and zero otherwise; their sum is the absolute difference.

  • Convert to floating point before subtracting.

  • Use the built-in imabsdiff(a, b) (Image Processing Toolbox / Octave image package).

Noise Models

Noise is an additive function:

\[I'(x, y) = I(x, y) + \eta(x, y)\]

Common Types

  • Salt-and-pepper noise — random pixels set to black or white.

  • Impulse noise — random bright specks only.

  • Gaussian noise — each pixel’s noise value is drawn i.i.d. from \(\mathcal{N}(0, \sigma^2)\). This is the most common model.

Generating Gaussian Noise

randn produces samples from \(\mathcal{N}(0, 1)\). Multiplying by \(\sigma\) scales the standard deviation (spread), not the count:

noise = sigma * randn(size(img));
noisy_img = im2double(img) + noise;

Effect of Sigma

\(\sigma\) controls noise magnitude relative to the image’s intensity range. For an image in \([0, 1]\), \(\sigma = 0.1\) represents 10% of the dynamic range. For \([0, 255]\), the equivalent is \(\sigma \approx 25\).

When displaying noise alone (mean = 0, includes negative values), map the minimum to black and maximum to white, with zero as mid-gray:

imshow(noise, []);    % auto-scale display range

Small \(\sigma\) → nearly uniform gray; large \(\sigma\) → heavy speckle that obscures image content.

Displaying Images

Display range must match the image’s actual value range:

imshow(img, [low high]);   % map low→black, high→white
imshow(img, []);           % auto-scale to [min, max] of img
imagesc(img);              % older function, auto-scales

Normalize images only for display, never before computation (e.g., before computing gradients), or information is destroyed.