opencv realizes image color space conversion

  • 2020-06-19 11:10:08
  • OfStack

Common opencv sample Spaces include RGB, HSV and YUV. RGB color space is formed based on the principle of 3 primary colors (2) and is often used in image display systems. HSV describes the chromaticity, saturation, and brightness of color, often used to describe color changes; YUV describes colors by brightness and chroma, which is a combination of UV channels.

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

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

The default channel in opencv is BGR, so the first byte in a standard 24bit color space image is blue, followed by green, and the last byte is red. There are strict requirements for input of the original image in cvtColor and for non-ES55en types. The input image is normalized to the appropriate type. In most scenarios, in order to make the best use of the information for the image, 1 generally goes to the type required by src before the operation, and after the operation is completed, the transfer occurs.

It should be noted that, in the conversion of color space, the range of RGB channels should be normalized according to actual needs. For example, when RGB switches to LUV channels, RGB needs to be normalized to 32 as a floating point, and the value of each channel varies from 0 to 1. The code is as follows:


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

Example RGB to HSV


#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
  cv::Mat srcImage = cv::imread("C:\\Users\\LP\\Desktop\\C++\\ConsoleApplication4\\ConsoleApplication4\\1.jpg");
  if (srcImage.empty())
  {
    return -1;
  } 
  cv::imshow(" The 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);
  // The separation of HSV Each channel 
  std::vector<cv::Mat> hsvChannels;
  cv::split(image_hsv, hsvChannels);
  //0 Channel for H Component, 1 Channel for S Component, 2 Channel for V component 
  image_H = hsvChannels[0];
  image_S = hsvChannels[1];
  image_V = hsvChannels[2];
  // Display each channel image separately 
  cv::imshow("image_H", image_H);
  cv::imshow("image_S", image_S);
  cv::imshow("image_V", image_V);
  
  cv::waitKey(0);
  return 0;
} 

Related articles: