Skip to main content

Image Pixels, Channels, and Color Spaces

Read an image as an array: inspect RGB channels, grayscale and HSV, and understand coordinates, data types and tensor layouts before preprocessing.

A model receives numbers, not a photograph. Before learning what an image contains, it needs an agreed representation of those numbers: where each sample belongs, which channel it describes, and what its range means. A swapped channel or misplaced dimension can produce a valid tensor that represents the wrong image.

Start with one pixel

An RGB image has a red, green, and blue sample at each location. For an 8-bit image, each sample is an integer from 0 to 255. A pixel such as (255, 0, 0) is red in this encoding; (0, 0, 0) is black. These channel values describe an encoded color, not three separate object labels.

Move the coordinate sliders below, then isolate a channel. Notice that the blue value decreases as you move right, while green changes as you move down. The gray single-channel views make those numerical patterns visible.

Inspect one pixel
Pixel view
Single channel
6 × 6 image · rgb view
2
3
RGB
102, 153, 153
RGB / 255
0.40, 0.60, 0.60
Gray
137.8
HSV
180°, 0.33, 0.60

Authored RGB ramp. Single channels are shown in gray: white means a large channel value. Coordinates are zero-based; hue is in degrees.

Coordinates and dimensions answer different questions

For an array shaped (height, width, channels), the pixel at image coordinate (x, y) is usually indexed as image[y, x]. Row comes first in the array; horizontal position comes first in the coordinate pair. With zero-based indexing, a 640 × 480 image has columns 0 through 639 and rows 0 through 479.

Tensor layout adds another convention. HWC stores channels last, while CHW stores them first. Adding a batch dimension gives NHWC or NCHW. Changing layout requires a dimension permutation; reshaping the same flat numbers usually scrambles which values belong to each pixel.

python
# examples: image is an RGB NumPy array shaped (height, width, 3)
pixel = image[20, 10]             # x=10, y=20
chw = image.transpose(2, 0, 1)    # (3, height, width)
normalized = chw.astype('float32') / 255.0

Dividing by 255 changes the range. It does not reorder channels, resize the image, or apply the mean and standard deviation that a particular pretrained model may expect. Record those preprocessing choices alongside the model.

Grayscale and HSV expose different information

The explorer uses the common weighted grayscale conversion below. Green contributes more than blue; grayscale is therefore not the arithmetic mean of the channels. Once RGB is collapsed to one value, the original color generally cannot be recovered. OpenCV documents these coefficients and its color conversion conventions. OpenCV color conversions

Y=0.299R+0.587G+0.114BY = 0.299R + 0.587G + 0.114B

HSV instead describes hue, saturation, and value. Here hue is an angle in degrees, saturation is 0–1, and value is the largest RGB channel after scaling to 0–1. Gray pixels have zero saturation, so their hue carries no useful color information. Libraries may encode these ranges differently; check before comparing thresholds.

RGB and BGR are channel-order conventions. Reading BGR data as RGB exchanges red and blue without changing the array shape. A useful debugging image contains obvious red and blue patches so this mistake is immediately visible.

Before passing an image to a model

Inspect the shape, dtype, minimum and maximum values, channel order, and coordinate convention. Display a sample after preprocessing, undoing normalization when needed. Use floating-point arithmetic for operations that can go below 0 or above 255, and clip only when converting back to a bounded display representation.

Once the representation is clear, resizing and interpolation explain how a new grid of pixels is constructed. The same distinction between the underlying data and its display will matter when inspecting CNN feature maps.

If you found this explanation helpful, consider sharing it with others.

Mastodon