Color spaces in OpenCV in C++

In this tutorial, we will learn about color spaces in OpenCV in C++. Let us start by getting answers to some basic questions which make the foundation of this topic.

What is a Color Space? 
A color space is a way of organizing the colors for the formation of an image. The most common color space which we use in everyday life is the RGB color space. It uses the combination of red, green, and blue colors to represent all the other colors. We have different color space models that differ in the number and the kinds of parameters used. Each of the color space has its own advantages and disadvantages.

At present OpenCV supports various kinds of color spaces. We will have a look at some of them.

C++: Color spaces in OpenCV

RGB COLOR SPACE

This is the most widely used color space as it uses the three primary colors(Red, Blue, Green) which are perceived by the human eye. The amount of each of the three colors used determines the final color produced. Hence, it an additive color space. The colors range from black(0,0,0) to white(255,255,255). The drawback of this color space is that it fails when we capture the same object but with different intensities of light present. The default colorspace of OpenCV is RGB. Hence, when we load an image it is in RGB color space.

#include<opencv2/opencv.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<iostream>

using namespace std;
using namespace cv;


int main() {
  Mat img;
  img = imread("C:/Users/91987/Pictures/9.png");
        //the location of the image should be mentioned in the imread" "

       //name the window that appears
        namedWindow("imag");

       //imshow is used to display the image
        imshow("imag", img);

  waitKey(0);

  return 0;
}

OUTPUT:

Color spaces in OpenCV in C++

LAB COLOR SPACE

In LAB, L stands for lightness, which varies from 0(black) to 100(white), A stands for the color variation from green to red, and B stands for color variation from blue to yellow. It is a superset model that contains color combinations of both the RGB and the CYMK model and hence is used when one model needs to be transformed to the other. As it contains a separate channel for lightness, it overcomes the non-uniformity faced while used the RGB model. The difference in brightness only affects the L channel and the colors are perceived the same for all amounts of brightness.

#include<opencv2/opencv.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<iostream>

using namespace std;
using namespace cv;


int main() {
  Mat img;
  img = imread("C:/Users/91987/Pictures/img.jpg");
  Mat Lab_image;

  cv::cvtColor(img, Lab_image, cv::COLOR_BGR2Lab);

        namedWindow("imag");
        namedWindow("Lab image");

        imshow("imag", img);
        imshow("Lab image", Lab_image);

        waitKey(0);
        return 0;
}

OUTPUT:

LAB COLOR SPACE

HSV COLOR SPACE

Instead of a cubic model, HSV is a cylindrical model that changes its values as and when the angle from the axis changes. The H stands for hue(radial parameter), S stands for saturation, and V stands for value(the brightness value). It also has an alternative representation called HSL(hue, saturation, lightness) or HSB(hue, saturation, brightness). Instead of using two or three channels to describe the color, this model uses only one channel i.e the H channel. As the brightness channel is different, the color channel remains the same when subjected to different intensities of light. Hence, the color remains consistent. At we have red color, followed by green at 120° and blue at 240°. Pure colors form the edge with a saturation of 1.

#include<opencv2/opencv.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<iostream>

using namespace std;
using namespace cv;


int main() {
  Mat img;
  img = imread("C:/Users/91987/Pictures/img.jpg");
  Mat HSV_image;

  cv::cvtColor(img, HSV_image, cv::COLOR_BGR2HSV);

  namedWindow("imag");
  namedWindow("HSV image");

  imshow("imag", img);
  imshow("HSV image", HSV_image);

  waitKey(0);

  return 0;
}

OUTPUT:

HSV COLOR SPACE

CYMK COLOR SPACE

CMYK stands for Cyan-Magenta-Yellow-Key. Unlike the RGB color space, in which the colors are made by adding up the three primary colors, this model subtracts colors from the white light. White light minus red leaves cyan, white light minus green leaves magenta, and white light minus blue leaves yellow. The K stands for the key which is the black color. It is opposite the RGB space because here the black color is made by the absence of all the colors this model uses. This model is specifically used for printers. It has the disadvantage that while printing it sometimes gives a variation of the color for the same color code. A direct conversion and visualization of this model are not available. We need to do it pixel by pixel which is out of the context of this post.

READ MORE:

C++ : Coin Detection Program using OpenCV
Convert RGB to Hex color code in C++

Leave a Reply

Your email address will not be published. Required fields are marked *