VOOZH about

URL: https://www.geeksforgeeks.org/python/rgb-color-model-in-python/

⇱ RGB Color Model in Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

RGB Color Model in Python

Last Updated : 23 Jul, 2025

RGB (Red, Green, Blue) color model is widely used in digital image processing, computer graphics and web designing. It represents colors by combining red, green and blue at different intensities to produce a broad spectrum of colors. In this article, we'll take a deeper look at RGB colour model.

Implementation of RGB color model in Python

1. Loading and Displaying the Image

We'll start by setting up the necessary libraries like OpenCV, Numpy and Matplotlib. We will load the example image which can be downloaded from here.

By default, any image in opencv is loaded in the Blue-Green-Red (BGR) model.

Output:

👁 Image-in-BG
Sample image in BGR format

We'll then be converting the same image in RGB mode and displaying it.

Output:

👁 Image-in-RGB
Sample image in RGB format

2. Shape of the Image

The "shape" attribute returns the height, width and number of channels of an image. Since the RGB Model deals with colour images that have 3 channels.

Output:

👁 Screenshot-2025-04-14-110747
Shape of the image

3. Printing first 10 pixels

Any colour image is basically a 3D matrix. In the RGB colour space the first channel represents the Red component, second and third channels represent the Green and Blue components respectively. Thus a pixel which is the smallest unit of an image is made up of three values: R, G and B.

In order to visualize values of pixel we'll be printing the first 10 pixels of this image. For this we need to flatten the 3D matrix into a 1D matrix with 3 columns each where each column represents a channel.

Output:

👁 Screenshot-2025-04-14-110440
First 10 pixel value

4. Extracting RGB Channels

The cv2 module also has a method to separate the channels. We will use cv2.split() function.


Output:

👁 1
Extracted RGB channels

5. Histogram Analysis of RGB Channels

In order to understand the colour distribution of each of the channels of this particular image, we'll look into its histogram.


Output:

👁 Histogram
Histogram

6. Statistical Analysis For Each Channel

We'll look into the mean, median, standard deviation, minimum and maximum values of each channels.

Output:

👁 Screenshot-2025-04-14-113250
Statistical Analysis For Each Channel

7. Channel Correlation Analysis

We can obtain the correlation between each of the channels using correlation matrix.

Output:

👁 corr
Correlation matrix

The color intensity of each cell in the heatmap visually represents correlation values with shades of red indicating higher correlations and blue shades indicating lower correlations. This matrix helps in understanding how the color channels relate to one another in a imagr which is useful in various image processing tasks.

Comment