Color Space
A color space defines how pixel values represent color. RGB, HSV, and Lab support tasks in computer vision, from thresholding to measurement.
A color space is a coordinate system that defines how numbers represent colors. A pixel's color is a point in that system. The same pixel can be described by its red, green, and blue components or by its hue, saturation, and value. In computer vision, the choice of coordinates affects which properties are easy to isolate and what each input channel means to a model.
Color Space, Color Model, and Gamut#
A color model specifies the components used to describe a color, such as red, green, and blue in RGB. A specific RGB color space, such as sRGB or Display P3, also defines the primary colors, a reference white, and a transfer function that relates encoded values to linear light. The gamut is the range of colors the space can represent. Display P3 has a wider gamut than sRGB. For example, full green in each space refers to that space's own green primary, so the same numbers encode different colors.
An embedded ICC profile gives color-managed software the information it needs to interpret an image's values. Assigning a profile changes how the existing numbers are interpreted. Converting to another profile recalculates the numbers to preserve the original colors as closely as the destination gamut allows. In data preprocessing, profile conversion can put tagged images from different sources into a common working space.
Common Color Representations in Computer Vision#
RGB and BGR store the same red, green, and blue components in different orders. OpenCV loads color images in BGR order by default. Swapping the first and last channels changes how the components are stored while leaving the underlying primaries and gamut unchanged.
HSV (hue, saturation, value) makes color detection easier to express as a set of thresholds. Hue is a position around the color wheel, saturation describes how vivid the color is, and value is the largest of the three RGB components. Hue wraps around at red, so a range that straddles that boundary takes two intervals. A minimum saturation threshold helps reject near-gray pixels, whose hue can change sharply with small differences in the RGB values.
CIELAB (Lab) is designed so that distances between coordinates approximate perceived color differences. L* represents lightness, while a* and b* describe color along roughly green–red and blue–yellow axes. In a stained-tissue image segmentation workflow, k-means clustering groups pixels using a* and b*, then L* helps isolate dark nuclei within the blue-stained group.
Grayscale retains intensity in a single channel and discards chromatic information. It works for thresholding when the foreground is brighter or darker than the background. Two different colors that map to the same gray level become indistinguishable by intensity alone.
How HSV Thresholding Works#
Thresholding on a range of HSV values produces a binary mask. The hue interval selects a color family, while the minimum saturation and value cutoffs exclude near-gray or dark pixels. OpenCV's standard 8-bit HSV encoding uses hue values from 0 to 179, so the bounds of 100 and 130 below correspond to roughly 200° and 260° around the color wheel.
from urllib.request import urlretrieve
import cv2
path, _ = urlretrieve("https://ultralytics.com/images/bus.jpg", "bus.jpg")
bgr = cv2.imread(path)
hsv = cv2.cvtColor(bgr, cv2.COLOR_BGR2HSV)
# Keep pixels in the blue hue range with enough saturation and value.
mask = cv2.inRange(hsv, (100, 80, 60), (130, 255, 255))
cv2.imwrite("blue_mask.png", mask)blue_mask.png shows matching pixels in white and everything else in black. Pixels from different objects can appear in the same white region when they fall within the same color range.
Color in Training and Deployment#
HSV gives data augmentation separate controls for hue, saturation, and value. Ultralytics YOLO's RandomHSV training transform converts BGR images to HSV, adjusts those components, and converts the result back to BGR for subsequent preprocessing. When color defines a class, a large hue shift can make the training image disagree with its ground truth.
During model deployment, passing a BGR array to an RGB input preserves the expected shape while swapping the meaning of the first and last channels. Range normalization scales the values in place: dividing the 8-bit BGR red pixel (0, 0, 255) by 255 produces (0, 0, 1), which an RGB input interprets as blue.










