How to do it...

The conversion of a BGR image into a phenomenal color space is done using the cv::cvtColor function that was explored in the previous recipe. The following steps will help us conduct the conversion:

  1. Here, we will use the cv::COLOR_BGR2HSV conversion code:
    // convert into HSV space 
    cv::Mat hsv; 
    cv::cvtColor(image, hsv, cv::COLOR_BGR2HSV); 
  1. We can go back to the BGR space by using the cv::COLOR_BGR2HSV code. We can visualize each of the HSV components by splitting the converted image channels into three independent images, as follows:
    // split the 3 channels into 3 images 
    std::vector<cv::Mat> channels; 
    cv::split(hsv,channels); 
    // channels[0] is the Hue 
    // channels[1] is the Saturation 
    // channels[2] is the Value 

Since we are working on 8-bit images, OpenCV rescales the channel values to cover the 0 to 255 range (except for the hue, which is rescaled between 0 and 180, as will be explained in the next section). This is very convenient, as we are able to display these channels as gray-level images. The value channel of the castle image will then look as follows:

The same image in the saturation channel will look as follows:

Finally, the image in the hue channel looks as follows:

These images will be interpreted in the next section.