Modules / Computer Vision / RGB Processing

RGB Image Logic

Draw with colors to understand how images are composed of Red, Green, and Blue channels.

Pixel Matrix Breakdown

LIVE CHANNELS

Understanding RGB Image Processing

A deep dive into how digital color is represented and manipulated. Read this, experiment with the tool, and solidify your understanding.

Start here

RGB Image Processing is a fundamental concept in computer vision. It's the basis for how computers "see" and manipulate color images. Every colored pixel on your screen is a combination of three values: Red, Green, and Blue.

The Core Idea: Channels as Layers

Think of a color image not as a single flat picture, but as three separate grayscale images stacked on top of each other. Each of these "layers" is a channel, representing the intensity of Red, Green, or Blue light for every pixel.

  • The Red Channel shows where the image is most red.
  • The Green Channel shows where it's most green.
  • The Blue Channel shows where it's most blue.

When combined, their values mix to create the full spectrum of colors we see. A value of (0, 0, 0) is black, (255, 255, 255) is white, and (255, 0, 0) is pure red.

Three numbers per pixel

A colour image stores three values per pixel — red, green and blue — each usually 0 to 255. Mixing them additively produces every colour the display can show.

RGBColour
25500Red
02550Green
2552550Yellow
255255255White
000Black
128128128Mid grey

Equal values give a shade of grey; unequal values give a hue. That simple fact is the basis of several quick image checks.

A 1920×1080 photograph is therefore 1920 × 1080 × 3 = 6.2 million values, about 6MB uncompressed. In code it is an array of shape (height, width, 3) — and note the order: height first, then width, which is the opposite of how image sizes are usually quoted.

Two conventions cause endless confusion. OpenCV stores images as BGR, while almost everything else uses RGB, so an image loaded with cv2.imread and displayed with matplotlib comes out with red and blue swapped. And PyTorch expects channels-first tensors, (3, H, W), while NumPy and PIL use channels-last, (H, W, 3).

Other colour spaces, and why they exist

RGB is how displays work, not how colour is best reasoned about. Separating colour from brightness is often far more useful.

HSV — hue (which colour, 0–360°), saturation (how vivid), value (how bright). Selecting "all the red pixels" in RGB means writing rules across three interacting channels; in HSV it is a range on one. This is why HSV is the standard choice for colour-based thresholding, and why colour-jitter augmentation is implemented in it.

LAB — lightness plus two colour axes, designed so that equal numeric distances correspond to roughly equal perceived differences. Used for colour matching, and its L channel is what CLAHE is usually applied to for contrast enhancement without shifting hues.

YCbCr — brightness plus two chroma channels. JPEG and video compression store chroma at lower resolution than luma, because human vision is far more sensitive to brightness detail than to colour detail. That asymmetry is a large part of why images compress as well as they do.

import cv2
img  = cv2.imread("photo.jpg")                  # BGR
rgb  = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
hsv  = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, (0, 120, 70), (10, 255, 255))   # reds

Feeding colour images to a network

Three preprocessing steps, in this order.

Convert to the model's expected channel order. RGB for torchvision, and channels-first for the tensor.

Scale to 0–1 by dividing by 255.

Standardise per channel using the pretraining dataset's statistics — for ImageNet, mean [0.485, 0.456, 0.406] and standard deviation [0.229, 0.224, 0.225]. These are per channel because the channels genuinely have different distributions; natural images have more energy in red and green than in blue.

Skipping the standardisation is a silent error: the model trains and works, several points below where it should be, with nothing to indicate why.

Guided Experiments with This Interactive

Use the tool above to build a strong mental model. Follow these steps:

  1. Draw a Pure Color:
    • Set the brush to pure red (#FF0000). Draw a line.
    • Now, switch the "Pixel Matrix Breakdown" to the Red Channel. You'll see high values (like 255).
    • Switch to the Green and Blue channels. They will show values of 0. This proves the color is purely red.
  2. Create a Mixed Color:
    • Clear the canvas. Select a yellow color (#FFFF00). Draw something.
    • Check the channels. The Red and Green channels will have high values, while the Blue channel will be 0. This is because yellow is an equal mix of red and green light.
  3. Understand Grayscale Conversion:
    • Draw a colorful image. Then, click the "Convert Grayscale" button.
    • Notice how the image loses its color. Now, inspect the channels. The Red, Green, and Blue channels are now identical! A grayscale image is simply an RGB image where R=G=B for every pixel.
  4. Explore Inversion:
    • Draw a green (#00FF00) shape. Click "Invert Colors". The shape turns magenta.
    • Why? The original pixel was (R:0, G:255, B:0). The inverted pixel is (R:255-0, G:255-255, B:255-0), which results in (R:255, G:0, B:255) — the color magenta.

Thinking in Pseudocode

When a computer applies a filter, it iterates through every pixel and applies a mathematical rule. For a brightness adjustment, it looks like this:

function adjust_brightness(image, factor):
  for each pixel (x, y) in image:
    original_color = image.get_pixel(x, y) // e.g., (R:50, G:100, B:200)
    
    new_R = original_color.R * factor
    new_G = original_color.G * factor
    new_B = original_color.B * factor
    
    // Clamp values to stay within the 0-255 range
    new_R = min(255, new_R)
    new_G = min(255, new_G)
    new_B = min(255, new_B)
    
    image.set_pixel(x, y, (new_R, new_G, new_B))
  
  return image

This is exactly what happens when you use the "Brightness" slider and click "Apply".

Common Mistakes & Pitfalls

  • Confusing Additive vs. Subtractive Color: Digital color is additive (mixing light). Adding R, G, and B makes white. This is the opposite of mixing paint (subtractive color), where mixing colors makes black.
  • Ignoring Data Types: Pixel values are almost always stored as 8-bit unsigned integers (0-255). If an operation results in a value like 300 or -50, it must be "clamped" to 255 or 0, respectively. Forgetting this leads to visual artifacts.
  • Thinking Filters "See" Objects: A blur or brightness filter doesn't know it's looking at a car or a face. It's just a simple mathematical rule applied to a grid of numbers. The "intelligence" of computer vision comes from more complex models that learn patterns from these numbers.

What the three channels actually hold

An RGB image is three greyscale images stacked, and almost every colour operation is a question about how they relate. This works through channel arithmetic, the difference between per-channel and joint operations, and the two mistakes that cause most colour bugs.

example_01.pyNumPy
Output

Summing up

  • A color image is a 3D array of numbers: (Height x Width x 3).
  • The three layers are the Red, Green, and Blue channels.
  • Image processing operations are just mathematical functions applied to the numerical values of pixels.
  • By isolating and observing the channels, you can understand exactly how any color or filter works.

When colour carries the answer, and when it misleads

Colour is essential when the task depends on it — ripeness, traffic signals, quality control on painted parts, medical staining, species identification. Discarding it there throws away the signal.

But colour is also the most common shortcut a model will latch onto. If every photograph of one class in your dataset was taken in a particular lighting, or every image of a defect happens to come from a camera with a different white balance, the model will learn the colour cast rather than the object. It will score well on your test split and fail immediately on new data.

Three defences:

  • Colour augmentation — jitter brightness, contrast, saturation and hue during training so the model cannot rely on exact values.
  • Check with Grad-CAM whether the model is attending to the object or to the background.
  • Test on data from a different source — a different camera, a different day, a different site.

The related failure is white balance: the same object photographed under daylight and under tungsten lighting has genuinely different RGB values. Histogram equalisation on the L channel of LAB, or a grey-world white-balance correction, normalises much of this away.

Common mistakes

  • BGR/RGB confusion, which makes images look wrong and models underperform for no visible reason.
  • Forgetting the channel order for tensors — a (H, W, 3) array passed where (3, H, W) is expected either errors or silently transposes the image.
  • Skipping normalisation with a pretrained model.
  • Averaging channels for greyscale instead of using the perceptual weighting.
  • Assuming 8 bits. Medical and scientific images are often 16-bit, and dividing them by 255 gives values in the thousands.
  • Applying colour augmentation to a colour-dependent task, destroying the very signal the model needs.

Questions people ask

Why does my image look blue-tinted? BGR loaded as RGB. Convert with cv2.cvtColor.

Should I convert to greyscale? Only if colour genuinely carries no information for the task. Test both — it is cheap to check.

What is alpha? A fourth channel for transparency, giving RGBA. Most models expect three channels, so composite onto a background first.

Which colour space for thresholding? HSV, almost always — hue is one axis there and three interacting ones in RGB.

Do CNNs learn colour features? Yes — the first layer of trained networks contains colour-opponent filters alongside the edge detectors.

Why per-channel normalisation rather than one global mean? Because the three channels have genuinely different distributions in natural images, and treating them identically leaves a systematic offset.

Recap in one screen

  • An RGB image is three grids of intensities; equal values are grey, unequal values are a hue.
  • OpenCV uses BGR and PyTorch uses channels-first — two conventions that cause most image-loading bugs.
  • HSV separates colour from brightness and is the right space for colour thresholding and jitter.
  • Scale to 0–1 and standardise per channel with the pretrained model's statistics.
  • Colour is often the signal, and just as often the shortcut a model will exploit — augment and verify.

Recall check

0 of 3

Say the answer out loud before you reveal it — recalling it is what makes it stick, and rereading it is not.

  1. What does this module say about “Start here”?

  2. What does this module say about “The Core Idea: Channels as Layers”?

  3. What does this module say about “Three numbers per pixel”?

Cheat sheet

RGB Image Processing

RGB Image Processing is a fundamental concept in computer vision. It's the basis for how computers "see" and manipulate color images. Every colored pixel on your screen is a combination of three values: Red, Green, and Blue.

COMPUTER VISION · vizlearn.in/computer_vision/rgb_image_processing.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.