Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 46 ยท Images as Data

Color Channels and Color Spaces

Split and merge RGB channels, convert to grayscale with perceptual weights, use HSV and Lab color spaces, and build color masks to pick out objects by hue.

A color photo is three grayscale pictures stacked together: how much red, how much green and how much blue each pixel has. That description (RGB) matches how cameras and screens work, but it is not always the best way to reason about color. If you want to find "all the red apples", the raw RGB numbers change a lot between a bright apple and a shadowed one, while the hue barely moves.

This lesson shows how to split and merge channels, convert color to grayscale in the right way, move to other color spaces such as HSV and Lab, and use them to pick out objects by color. You should be comfortable with the array basics from Images as Arrays.

RGB channels

In an (H, W, 3) array, the last axis holds red, green and blue. Slicing img[:, :, 0] gives the red plane as a normal 2D array. A pure red pixel is (255, 0, 0), white is (255, 255, 255), and yellow is red plus green, (255, 255, 0), because these are additive colors: light mixes toward white.

Python Splitting and merging channels

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The BGR line deserves a note: OpenCV loads images with channels in the order blue, green, red, while matplotlib and most other tools expect RGB. If a picture looks as if blue and orange are swapped, a channel order mix-up is almost always the cause.

Grayscale: not just an average

Converting to grayscale means collapsing three numbers to one brightness. The plain average (R + G + B) / 3 works but looks wrong: human vision is most sensitive to green, then red, and least to blue. A standard weighting is

gray = 0.299 R + 0.587 G + 0.114 B

(the ITU-R BT.601 weights, used by many libraries). A pure green pixel therefore ends up much brighter than a pure blue one. Doing the weighted sum with a dot product keeps it a one-liner.

Python Average versus perceptual grayscale

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With the plain average all three stripes become the same gray (85), and a red-green boundary would disappear. The weighted version keeps blue dark and green bright, much closer to how we perceive them. The result is a float array: round and cast to uint8 if you need to store it as an 8-bit image.

Color spaces: other ways to describe a color

A color space is a coordinate system for colors. The same color has different numbers in each one; the choice depends on the job.

Space Axes Good for
RGB red, green, blue Capturing and displaying images
HSV hue (color angle), saturation (purity), value (brightness) Picking colors, simple segmentation
HSL hue, saturation, lightness Design tools, similar idea to HSV
Lab lightness, a (green-red), b (blue-yellow) Measuring color differences the way people see them
YCbCr luma plus two chroma channels Video and JPEG compression
flowchart LR
    RGB["RGB<br/>(R, G, B)"] --> HSV["HSV<br/>hue, saturation, value"]
    RGB --> Lab["Lab<br/>L, a, b"]
    RGB --> Gray["Gray<br/>one channel"]
    HSV --> M["Color mask<br/>hue in a range"]

Every space is a different view of the same pixels; HSV separates the color from its brightness, which makes color masks easier.

HSV separates what color (hue, an angle around a color wheel: red near 0, green near one third, blue near two thirds) from how vivid (saturation) and how bright (value). Dimming a red apple lowers its value but leaves its hue almost unchanged. Python's standard library has colorsys for single colors; for whole images matplotlib has matplotlib.colors.rgb_to_hsv, which takes floats in 0..1 and returns hue, saturation and value in 0..1 too.

Lab is built so that a given numerical distance looks like a similar perceived difference everywhere in the space, which RGB does not do. It is the right choice when you compare or cluster colors. We use it again in Segmentation by Clustering and Watershed.

Python RGB versus HSV when the light changes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The three oranges have completely different RGB numbers but the same hue (about 33 degrees). Only the value changes. That is why hue is a more robust signal for "is this orange?" than any single RGB channel.

Color masks

Combining a color space with boolean masks gives a simple but real detector: convert to HSV, keep the pixels whose hue falls in a range and whose saturation and value are high enough, and you have a mask of the object. Remember that hue is circular (red wraps around from 360 back to 0), so a red range often needs two conditions.

Python Pick out red objects by hue

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The HSV mask finds both red discs, including the one in shadow (2,016 pixels in total). The naive "red channel above 200" test reports 5,184 pixels, because it matches the light gray background (whose red channel is 235) and the bright disc, yet misses the dark red one. Saturation is what rules out the gray background in the HSV version.

Tip: When a color mask does not work, print the HSV values of a few sample pixels from the object and its background. Then set ranges from the real numbers instead of guessing.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is the weighted grayscale formula (0.299, 0.587, 0.114) preferred over a plain average?

    Human vision is most sensitive to green and least to blue, so a weighted sum keeps contrast between colors that a plain average would flatten to the same gray.
  2. 2. In which situation is HSV more convenient than RGB?

    HSV separates hue from brightness, so a color range on hue (plus a saturation limit) survives shadows and highlights much better than thresholds on R, G or B.
  3. 3. An image looks wrong with blue and red swapped after loading it with OpenCV and showing it with matplotlib. What is the likely cause?

    OpenCV stores channels as BGR. Reverse the last axis (img[..., ::-1]) before showing it with matplotlib.
  4. 4. Why does a red hue range usually need two conditions?

    Reds lie just above 0 degrees and just below 360 degrees, so the mask is (hue < low) OR (hue > high).

Summary

  • A color image is a stack of channel planes; img[..., k] extracts one and np.stack(..., axis=-1) merges them back. Watch for RGB versus BGR order.
  • Grayscale should use perceptual weights (0.299, 0.587, 0.114), not a plain average.
  • Color spaces are different coordinates for the same colors: HSV separates hue from brightness, Lab matches perceived color distance, YCbCr serves compression.
  • A color mask is a boolean array built from range tests in a suitable space, usually hue plus minimum saturation and value.
  • Inspect real pixel values before choosing thresholds.

Next, see what happens when a scene is captured with limited pixels: Resolution, Sampling and Noise.