SoFunction
Updated on 2025-03-03

OpenCV implements image color space conversion

Commonly used color spaces for opencv include RGB, HSV and YUV. The RGB color space is formed based on the principle of three primary colors and is often used in image display systems. The chromaticity, saturation, and brightness described by HSV are often used to describe color changes; YUV describes color through brightness and chromaticity, and the chromaticity is composed of UV channels.

opencv provides cvtColor (inputArray src, outputArray dst, int code, int dstCn = 0)

src is the input image original, which can be 8-bit CV_8U or 16-bit CV_16U unsigned shaping, or single-precision floating point number CV_32F; code is the color space conversion mode, commonly used include CV_RGB2GRAY, CV_RGB2HSV, CV_BGR2HLS, CV_BGR2YCrCb, etc. dstCn is a multi-channel setting for the target image. Setting to 0 means that the number of channels is automatically obtained from src and code.

The default channel BGR in opencv, so the first thing that stores bytes in the standard 24bit color space image is the blue part, followed by green, and the last part of the bytes is the red part. There are strict requirements for input original image in cvtColor, and non-src-required types. The input image must be normalized to the corresponding type. In most scenarios, in order to better utilize the information of 32 as the image, you generally need to go to the type required by src before the operation, and then transfer after completing the operation.

It should be noted that when performing color space conversion, the range of each RGB channel should be normalized according to actual needs. For example, when RGB is transferred to LUV channel, RGB needs to be normalized to 32 as a floating point number, and the value variation range of each channel is 0 to 1; the code is as follows:

img *= 1./255;
cvtColor(img, img,CV_BGR2Luv);

Example RGB to HSV

#include <opencv2/imgproc/>
#include <opencv2/highgui/>
#include <opencv2/core/>
#include <iostream>
#include <>
using namespace std;
int main()
{
  cv::Mat srcImage = cv::imread("C:\\Users\\LP\\Desktop\\C++\\ConsoleApplication4\\ConsoleApplication4\\");
  if (())
  {
    return -1;
  } 
  cv::imshow("Original Image", srcImage);
  cv::Mat image_hsv, image_H, image_S, image_V, image_col;
  //HSV color space conversion  cv::cvtColor(srcImage, image_hsv, CV_BGR2HSV);
  cv::imshow("image_hsv", image_hsv);
  //YCrCb color space conversion  cv::cvtColor(srcImage, image_col, CV_BGR2YCrCb);
  cv::imshow("image_col", image_col);
  //HLS color space conversion  cv::cvtColor(srcImage, image_col, CV_BGR2HLS);
  cv::imshow("iamge_HLS", image_col);
  //Lab color space conversion  cv::cvtColor(srcImage, image_col, CV_BGR2Lab);
  cv::imshow("image_Lab", image_col);
  //Separate the HSV channels  std::vector<cv::Mat> hsvChannels;
  cv::split(image_hsv, hsvChannels);
  //Channel 0 is H component, 1 is S component, and 2 is V component  image_H = hsvChannels[0];
  image_S = hsvChannels[1];
  image_V = hsvChannels[2];
  //Display images of each channel separately  cv::imshow("image_H", image_H);
  cv::imshow("image_S", image_S);
  cv::imshow("image_V", image_V);
  
  cv::waitKey(0);
  return 0;
} 

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.