Hi everyone, in this tutorial we look at what are exactly color spaces and applying grayscale examples. What are color spaces and where we use them?. Color spaces are also known as color models and color systems. We use color spaces to model colors mathematically. We can produce countless color combinations using these color spaces. There are multiple color spaces used in image processing. If you are interested in image processing, you may have heard of RGB, CMYK, or HSV color spaces.

RGB Color Space

Usually uses the OpenCV library in the field of image processing. Opencv stores color in RGB format. Let’s get a closer look at what the RGB color space is. As the name suggests, the RGB color space consists of three main colors. These are red, green, and blue colors. Other colors consist of combinations of these three colors. These three primary colors take a number between 0-255. Using these three color combinations, we can create countless different colors.

RGB is an additive color model:

  • Red (0-255)
  • Green (0-255)
  • Blue (0-255)

opencv rgb

HSV Color Space

Although the HSV color space is not as popular as the RGB color space, it is quite useful. When you want to filter out colors in image processing, HSV color space works quite well. While you can easily define the RGB color space, defining the HSV color space is a bit different. The HSV color space is shown in the picture below. Here, the HSV color space takes three different parameters. These are defined as Hue, Saturation, Value / Brightness. They take values between Hue 0-179, Saturation 0-255, and Value 0-255.

HSV Colar Space:

  • Hue (0-179)
  • Saturation (0-255)
  • Value/ Brighness (0-255)

opencv hsv

HSV color space allows us to filter colors, unlike the RGB color space. Hue takes color values between 0-180, not 0-360.

Color Range Filters:

  • Red (165-15)
  • Green (45-75)
  • Blue (90-120)

Grayscalling

Normally, images consist of three dimensions. These dimensions are height, width, and depth or channel. The third parameter determines whether an image is colored or not. The third parameters of the pictures take either the value 1 or the value 3. If this third parameter is 1, the picture appears as grayscale, if it is 3, it is colored. The third parameter of color pictures consists of different combinations of red, green, and blue. Gray images, on the other hand, consist of a single channel since they only take values between 0-255.

RGB or Grayscale:

  • RGB [512, 512, 3]
  • Grayscale [512, 512, 1]

Let’s take a closer look at how to code these color spaces using opencv.

RGB Color Space
import cv2
import numpy as np
# Load the image using "imread" function
image = cv2.imread("/images/img.jpg")
image = cv2.resize(image, (512,512))
cv2.imshow("image",image)
opencv bgr format

Images are stored in OpenCV in BGR format instead of RGB. If you look at any pixel of the image you have, you will see that it has the format [Blue, Green, Red].

RGB Format:

  • Red [0, 0, 255]
  • Green [0, 255, 0]
  • Blue [255, 0, 0]

BGR Format:

  • Blue [255, 0, 0]
  • Green [0, 255, 0]
  • Red [0, 0, 255]
# image consist three equal part
# first part is blue
# second part is green
# thirt part is red

print(image.shape)

# image[0:100,:] = blue
# image[100:200, :] = green
# image[200:300, :] = red

print("----------------")

print("blue : ",image[50,50])
print("green : ",image[150,50])
print("red : ",image[250,50])
(300, 100, 3) 
---------------- 
blue :  [255   0   0] 
green :  [  0 255   0] 
red :  [  0   0 255]
HSV Color Space
# H-(0,179), S-(0,255), V-(0,255)
import cv2
import numpy as np

# Load the image using "imread" function
image = cv2.imread("/images/car.jpg")

cv2.imshow("image",image)
# convert the image from BGR format to the HSV format
hsv_img = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

cv2.imshow("hsv_img",hsv_img)
opencv open image
opencv hsv color space

When we want to perform image filtering operations, HSV color space gives more successful results than others. Let’s take a closer look at how image filtering is done.

# Our image consists mostly of blue colors.
# So we can filter colors by blue color.
# we need to blue color's low and upper of hsv values

lower_blue = np.array([90,100,0])
upper_blue = np.array([125, 255,255])

mask = cv2.inRange(hsv_img, lower_blue, upper_blue)

cv2.imshow("mask",mask)
opencv mask
Grayscalling
import cv2
import numpy as np

# Load the image using "imread" function
image = cv2.imread("/images/car.jpg")
# Colorful images consists three channel 
print("colorful : ", image.shape)
print("image[0,0] : ", image[0,0])
print("------------------------------")

# convert the image from BGR format to the Grayscale format
# grayscale images consists single channel
gray_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
print("grayscale : ", gray_img.shape)
print("gray_img[0,0] : ", gray_img[0,0])
colorful :  (424, 640, 3) 
image[0,0] :  [39 37 37] 
------------------------------ 
grayscale :  (424, 640) 
gray_img[0,0] :  37
# show colorful and grayscale images on the screen
cv2.imshow("image",image)
cv2.imshow("gray_img", gray_img)
opencv image
opencv grayscale
# If you want to load images as grayscale

gray_image = cv2.imread("images/car.jpg", 0)
cv2.imshow("gray_image",gray_image)
opencv grayscale