Image Processing Series: Part1 → Colorspaces🌈

somuSan
Analytics Vidhya
Published in
5 min readMar 30, 2020

N.B: This is my first blog post which is on Image Processing. This is the 1st part of the series and a lot more to come in near future where I will cover different sub-topics like Morphological Operators, Smoothing and Blurring of Images, Gradients, Histograms, blending and posting images, etc. and how to implement them using Python OpenCV.

Table of Content:

  • What is Colorspace?
  • What are the types of Colorspace?
  • How to implement them in OpenCV?

What is Colorspace?

Color is a continuous phenomenon, it means there is an infinite number of colors. But, the human eye and perception are limited. So, to identify those colors we need a medium or representation of those colors and this representation of color is called color space. In technical terms, a color model or color space is a specification of a 3-D coordinate system and a subspace within that system where each color is represented by a single point.

What are the types of Colorspace?

There are mainly five major color models out there. But, I am going to write about only the frequent ones(RGB, HSV, and HSL).

  1. RGB(Red Green Blue)
  2. HSL(Hue Saturation Lightness)
  3. HSV(Hue Saturation Value)
  4. YUV(Luminance, blue–luminance, red–luminance)
  5. CMYK(Cyan, Magenta, Yellow, Key)

RGB Color Space:

RGB color space is one of the well-known color space represented by Red, Green, and Blue coordinates of the 3-D coordinate system. In more technical terms, RGB describes a color as a tuple of three components. Each component can take a value between 0 and 255, where the tuple (0, 0, 0) represents black and (255, 255, 255) represents white. Zeroth, first and second component of the tuple represents the amount of Red, Green, and Blue respectively.

Source: www.google.com

Python Implementation of RGB color space :

Here we are importing necessary libraries. cv2 for color space conversion, NumPy for array manipulation, Matplotlib for showing the image, os for accessing the image directory and tqdm for showing the loading bar.

Setting two empty lists Z and X for storing labels with the respective images respectively then specifying the image size and the path directory. After that, I have defined two functions for returning the flower type(assign_lable) and for accessing each image, reading and resizing it(make_train_data).

Loading the image, then converting the BGR color space into RGB color space as OpenCV reads an image in BGR formate but Maplotlib uses RGB formate for showing the images. That’s why we need to convert the color space after reading the image into RGB.

Then we make three copies of the fixed image and make any two-color channel of each copy zero for accessing Red, Green, and Blue color channels individually. Like if you make zeroth and the first color channels zero then you will get only Blue color channel.

Showing those images:

The output of the above code

HSL Color Space:

The general meaning of HSL is Hue, Saturation, and Lightness. You can visualize HSL in the form of a cylinder as shown in fig:2(a). All around the cylinder will be different colors, Like green, yellow, red, etc. (the actual hue we are looking for). Saturation is how many hue you end up having and lightness is how dark or how bright the color is. As you can see the top of the cylinder is full of white and the bottom part is full black.

Fig:2 → HSL color Space(Source : www.google.com)

Python Implementation of HSL color space:

Converting the BGR color space into HSL color space using a builtin OpenCV function called cvtColor(), where we need to pass the image, and from which color space to which color space we want to change the image. Then again copying and making two-color channel zero of each copied image for showing each color channel separately.

Now showing three different color channels →

The output of the above code

HSV Color Space:

The name HSV is coming from the three coordinates of the color model which are Hue, Saturation, and Value. It is also a cylindrical color model where the radius of that cylinder implies Saturation, Vertical axis implies the Value and the angle represents the Hue. Hue is the dominant color as perceived by as an observer, Saturation is the amount of white light mixed with a hue and Value is the chromic notion of intensity, lower the Value, the color gets more similar to black and higher the value, the color gets more similar to the color itself. By changing these parameters we can generate different colors.

Fig:3 → HSV color space(Source: www.google.com)

Python Implementation of HSV color space:

Converting the colorspace into HSV color space using cvtColor() function. Then again copying and making two-color channel zero of each copied image for showing each color channel separately.

Showing every individual color channel →

The output of the above code

References:

I followed these two Kaggle notebooks in order to write the blog 1st on is belongs to Henrique Mello and the 2nd one belongs to Sani Kamal.

  1. https://www.kaggle.com/hrmello/intro-to-image-processing-colorspaces

2. https://www.kaggle.com/sanikamal/image-segmentation-using-color-spaces

Thank you for reading. If there is any query please let me know.

--

--