Table of content

Color Models with OpenCV

RGB (Red Green Blue)

The RGB (Red, Green, Blue) color model is the most known, and the most used every day. It defines a color space in terms of three components:

• Red, which ranges from 0-255
• Green, which ranges from 0-255
• Blue, which ranges from 0-255

The RGB color model is an additive one. In other words, Red, Green, and Blue values (known as the three primary colors) are combined to reproduce other colors.

For example, the color "Red" can be represented as [R=255, G=0, B=0], "Violet" as [R=238, G=130, B=238], etc.

Its common graphic representation is the following image:

By default, most of the images will are in RGB color as OpenCV reads images as BGR color, so we need to convert before performing the operations on the image.

Commons code :

``````import cv2
import matplotlib.pyplot as plt

Convert Image to RGB color:

``````img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img)``````

The HSB model is also known as HSV (Hue, Saturation, Value) model. The HSV model was created in 1978 by Alvy Ray Smith. It is a nonlinear transformation of the RGB color space. In other words, color is not defined as a simple combination (addition/subtraction) of primary colors but as a mathematical transformation.

The HSB (Hue, Saturation, Brightness) color model defines a color space in terms of three constituent components:

• Hue : the color type (such as red, blue, or yellow). Ranges from 0 to 360° in most applications. (each value corresponds to one color : 0 is red, 45 is a shade of orange and 55 is a shade of yellow).
• Saturation : the intensity of the color. Ranges from 0 to 100% (0 means no color, that is a shade of grey between black and white; 100 means intense color). Also, sometimes called the "purity" by analogy to the colorimetric quantities excitation purity.
• Brightness (or Value) : the brightness of the color. Ranges from 0 to 100% (0 is always black; depending on the saturation, 100 may be white or a more or less saturated color).

Its common graphic representation is the following image:

Convert Images to HSV color coding

``````img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
plt.imshow(img)``````

HSL color space

The HSL color space, also called HLS or HSI, stands for:

• Hue : the color type (such as red, blue, or yellow). Ranges from 0 to 360° in most applications (each value corresponds to one color : 0 is red, 45 is a shade of orange, and 55 is a shade of yellow).
• Saturation : variation of the color depending on the lightness. Ranges from 0 to 100% (from the center of the black&white axis).
• Lightness (also Luminance or Luminosity or Intensity). Ranges from 0 to 100% (from black to white).

Its common graphic representation is the following image:

Convert image to HLS color coding

``````img = cv2.cvtColor(img, cv2.COLOR_BGR2HLS)
plt.imshow(img)``````

0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions