Digital Images

An image can be modeled as a function , where gives the intensity at position . A real image is defined only over a rectangle and has a finite range, . A color image is three such functions pasted together, written as a vector-valued function

where , and give the red, green and blue intensities. A digital image comes from sampling on a grid of pixel positions and quantizing each sample to a finite set of levels. With 8 bits per channel, the levels are the integers .

A pixel value measures light. A source emits light, a fraction of it reflects off a surface into the camera, and the lens focuses it onto the sensor. A simple model of the result is

with reflectance in and illumination in . The acquisition pipeline turns scene radiance (in W/sr/m²) into sensor irradiance through the lens, then into sensor exposure through the shutter (open for a time ). The sensor (a CCD in the slides) turns exposure into analog voltages. An ADC (analog-to-digital converter) turns the voltages into digital values. A final remapping curve gives the stored pixel values. Real scenes span a dynamic range of about to in brightness, while an 8-bit picture stores to . An exposure records only a window of the scene’s range.

In code, an image is an array of shape for height (rows), width (columns) and channels, called channel-last or HWC. PyTorch uses channel-first, CHW. The pixel at column and row is img[y, x]. Two dtypes are common: uint8 with values –, and float with values –. Convert with img.astype(np.float32) / 255. OpenCV loads color images in BGR order. Each channel shown alone is a grayscale map of one primary color. The standard grayscale (luminance) conversion weights the channels by the eye’s sensitivity:

Here , and are the channel values of one pixel, and is its gray value on the same scale.

Casting to uint8

Casting floats to uint8 truncates instead of rounding: becomes and becomes . A float image in cast directly becomes almost all zeros. Out-of-range values wrap around: the discussion shows becoming , though NumPy does not promise any particular result for out-of-range casts. Arithmetic on uint8 arrays also wraps modulo . Do arithmetic in float, then clip to and round before casting back.

Intuition

Treating an image as a function lets us use the mathematics of functions on it. Filtering changes the values that takes, and warping changes where the values sit, as in affine transformations. A grayscale image can even be drawn as a surface, with brightness as height. A digital image is that function after two losses. Sampling keeps values only at grid points, as in sampling of signals, and the space between them must later be filled by guessing (resampling). Quantization rounds each kept value to the nearest allowed level. The figure shows both steps on one row of an image.

\usepackage{tikz}
\begin{document}
\definecolor{hot}{HTML}{D9483B}
\definecolor{cold}{HTML}{2F80ED}
\definecolor{mid}{HTML}{8A8A8A}
\begin{tikzpicture}[x=0.75cm,y=0.55cm,>=latex]
\foreach \l in {0,...,7} {
  \draw[mid!50,thin] (-0.2,\l) -- (10.4,\l);
  \node[mid,left] at (-0.2,\l) {\scriptsize $\l$};
}
\draw[->] (-0.2,0) -- (10.8,0) node[right] {$x$};
\draw[->] (0,-0.2) -- (0,7.8) node[above] {intensity level};
\draw[thick,cold,domain=0:10,samples=120] plot (\x,{3.5+2.5*sin(deg(0.6*\x))+0.8*sin(deg(1.7*\x))});
\foreach \x in {0,...,10} {
  \pgfmathsetmacro{\v}{3.5+2.5*sin(deg(0.6*\x))+0.8*sin(deg(1.7*\x))}
  \pgfmathsetmacro{\q}{round(\v)}
  \draw[mid,dashed] (\x,0) -- (\x,\v);
  \fill[cold] (\x,\v) circle (2.2pt);
  \fill[hot] (\x-0.13,\q-0.13) rectangle (\x+0.13,\q+0.13);
}
\node[cold,above] at (2.2,7.1) {continuous $f(x)$};
\fill[cold] (5.6,7.4) circle (2.2pt); \node[right] at (5.7,7.4) {\small sample};
\fill[hot] (7.67,7.27) rectangle (7.93,7.53); \node[right] at (7.9,7.4) {\small quantized};
\end{tikzpicture}
\end{document}

Pixel values measure light, so they mix two causes: the surface (reflectance) and the lighting (illumination). A gray shirt in sunlight and a white shirt in shade can produce the same pixel value. The sensor also stores only a narrow window of the brightness range found in the world. Choosing the exposure chooses which window. Tone adjustments that remap pixel values one at a time are the subject of point processing.

Properties

(Array Layout) NumPy stores an image as img[row, column, channel], and the row index grows downward.

So the first index is , the second is , and the origin is the top-left corner. Converting HWC to CHW for PyTorch is img.transpose(2, 0, 1). A list of images becomes a batch with a new leading axis, np.stack(images, axis=0), of shape , and its CHW version is batch.transpose(0, 3, 1, 2). Because points down, a rotation matrix that turns counterclockwise in a -up frame turns clockwise on screen, as noted in 3.1. Affine Transformations.

(Dtype Ranges) The same picture is stored as uint8 in or as float in , and mixing the two scales breaks results silently.

JPG files load as uint8. TIFF files may load as uint16, with values up to . Dividing a uint16 image by gives values up to , which display as almost pure white. Divide by the maximum of the actual dtype, or use a helper such as skimage.img_as_float, before combining images. Image writers expect floats in or uint8 in , so clip before saving.

(Channel Order) OpenCV returns BGR, while most other libraries expect RGB.

Showing a BGR array with an RGB viewer swaps red and blue: skies turn orange and red signs turn blue. Reverse the last axis with img[:, :, ::-1] or np.flip(img, axis=2). In a BGR array the red channel is index , so zeroing the red component is img[:, :, 2] = 0. The Prokudin-Gorskii glass plates used for color alignment with template matching are also stacked as B, G, R from top to bottom.

(Luminance Weights) Equal weights would make pure blue look as bright as pure green.

The human eye is most sensitive to green light, less to red, and least to blue. The weights , and follow this response. Pure green gets and pure blue gets , while the plain average gives both . The weights sum to , so white stays .

(Exposure Window) An 8-bit image stores a brightness ratio of at most , far less than a real scene contains.

The slides list relative scene brightnesses from up to , from a dim room to the disk of the sun. A long exposure maps the dark part of the scene into – and clips everything brighter to . A short exposure keeps the bright part and crushes the shadows to . The final remapping step of the pipeline is a nonlinear curve, so stored values are not proportional to the light that hit the sensor. Pixel arithmetic that assumes proportionality, such as averaging two exposures, is therefore only approximate.

Examples

(Easy) Averaging Two Photos Stored as uint8

Two aligned photos a and b are uint8 arrays. At one pixel, a is and b is . The code computes (a + b) / 2.

(Easy) The Channels of a Stop Sign

A photo of a red stop sign has the RGB value on the sign. The same photo loaded with OpenCV stores the pixel as [40, 30, 200].

(Medium) Splitting a Prokudin-Gorskii Scan

A scanned glass plate loads as a uint16 array of shape . The three exposures are stacked vertically in the order B, G, R.