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:
- Here, we will use the cv::COLOR_BGR2HSV conversion code:
// convert into HSV space cv::Mat hsv; cv::cvtColor(image, hsv, cv::COLOR_BGR2HSV);
- 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:
![](https://epubservercos.yuewen.com/87DAA0/19470379101492006/epubprivate/OEBPS/Images/037806a3-dcea-4ae1-9b0d-e5e13234d270.png?sign=1739609359-EazJLlrN2nFg2SxIWJAmH6pmK19nI8He-0-3d0dd5065cd809b616f30d0827f67764)
The same image in the saturation channel will look as follows:
![](https://epubservercos.yuewen.com/87DAA0/19470379101492006/epubprivate/OEBPS/Images/ab0b4a04-dac0-444d-aa74-47935f2b57c1.png?sign=1739609359-VaeajzjmmTAeBDyqXXNrXxFnpFGUVL54-0-8788f400c25243ff0a8688904156abde)
Finally, the image in the hue channel looks as follows:
![](https://epubservercos.yuewen.com/87DAA0/19470379101492006/epubprivate/OEBPS/Images/4fe3d923-d18c-4b56-a2d5-f09ba7205a95.png?sign=1739609359-Ok2joychkXl5yKXrhqIHrOiZ9vJ5JZx3-0-96327ed2f497aad89daf2322e76429a6)
These images will be interpreted in the next section.