OpenCV 4.x API detailed explanation and C++ example-color space conversion

Section 5 Color Space Conversion

OpenCV provides 143 types of color space conversion, such as RGB2BGR, BGR2GRAY, and so on. In this section, several commonly used color space conversions will be introduced.

1、cv::cvtColor


Convert an image from one color space to another.

void cv::cvtColor(InputArray src,OutputArray dst,int code,int dstCn = 0)

This function converts the input image from one color space to another. In the case of conversion from the RGB color space, the order of the channels (RGB or BGR) should be clearly specified. Please note that the default color format in OpenCV is usually called RGB, but it is actually BGR (bytes are the opposite). Therefore, the first byte in a standard (24-bit) color image will be the 8-bit blue component, the second byte will be the green component, and the third byte will be the red component. The fourth, fifth and sixth bytes will be the second pixel (blue, then green, then red), and so on.

The general range of R, G, and B channel values ​​is:

  • CV_8U image from 0 to 255
  • CV_16U image from 0 to 65535
  • CV_32F image from 0 to 1

In the case of linear transformation, the range does not matter. However, in the case of nonlinear transformation, the input RGB image should be normalized to an appropriate value range to obtain the correct result, for example, for RGB→L*u*v* transformation. For example, if you have a 32-bit floating point image directly converted from an 8-bit image without any scaling, it will have a value range of 0...255 instead of 0...1 assumed by the function. Therefore, before calling cvtColor, you first need to scale down the image:

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

If you use cvtColor with 8-bit images, some information will be lost in the conversion. For many applications, this won't be noticeable, but it is recommended to use 32-bit images in applications that require a full range of colors or convert the image before performing an operation and then convert it back.

If an Alpha channel is added to the conversion, its value will be set to the maximum value of the corresponding channel range: CV_8U is 255, CV_16U is 65535, and CV_32F is 1.

The parameters are as follows:

parameter name Parameter Description
src Input image: 8-bit unsigned, 16-bit unsigned (CV_16UC …) or single-precision floating point.
dst Output an image with the same size and depth as src.
code Color space conversion codes (please refer to ColorConversionCodes ).
dstCn The number of channels in the target image; if the parameter is 0, the number of channels is automatically derived from src and code.
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;

void setToPanel(cv::Mat & panel,const cv::Mat& region,int rowIndex,
                int colsIndex,const string& text){
    // 分割成网格
    int width = panel.cols / 4;
    int height = panel.rows / 2;

    // 计算每个网格的区域位置
    cv::Rect roiRect;
    roiRect.x = colsIndex * width;
    roiRect.y = rowIndex * height;
    roiRect.width = width;
    roiRect.height = height;

    // ROI提取
    cv::Mat roi(panel,roiRect);

    // 重新调整显示图像的大小与网格大小相同
    cv::Mat resized;
    cv::resize(region,roi,cv::Size(width,height));


    // 设置字体参数并显示
    int font_face = cv::FONT_HERSHEY_COMPLEX;
    double font_scale = 1;
    int thickness = 1;
    int baseline;
    cv::Size text_size = cv::getTextSize(text, font_face, font_scale, thickness, &baseline);
    cv::Point center(width / 2 - text_size.width / 2,height / 2 - text_size.height / 2);
    cv::putText(roi,text,center,font_face,font_scale,cv::Scalar(255,255,255),thickness);
}

int main()
{
    // 读取图像
    cv::Mat src = cv::imread("images/f1.jpg");
    if(src.empty()){
        cerr << "cannot read image.\n";
        return EXIT_FAILURE;
    }
    cv::Mat panel(cv::Size(1280,480),CV_8UC3);

    int colorspaces[] = {cv::COLOR_BGR2GRAY,cv::COLOR_BGR2YUV,cv::COLOR_BGR2HLS,
                        cv::COLOR_BGR2HSV,cv::COLOR_BGR2Lab,cv::COLOR_BGR2XYZ,
                        cv::COLOR_BGR2YCrCb,cv::COLOR_BGR2RGB};
    string names[] = {"gray","yuv","HLS","HSV","Lab","XYZ","YCrCb","RGB"};
    for(int i = 0;i<8;i++){
        cv::Mat dst;
        if(i == 0){
            cv::cvtColor(src,dst,cv::COLOR_BGR2GRAY);
            cv::cvtColor(dst,dst,cv::COLOR_GRAY2BGR);
        }else{
            cv::cvtColor(src,dst,colorspaces[i]);
        }
        if(i < 4){
            setToPanel(panel,dst,0,i,names[i]);
        }else{
            setToPanel(panel,dst,1,i-4,names[i]);
        }
    }


    cv::imshow("dst",panel);
    cv::waitKey();
    return 0;
}

Insert picture description here

2、cv::cvtColorTwoPlane


Convert an image from one color space to another, where the source image is stored in two planes.

void cv::cvtColorTwoPlane(InputArray src1,InputArray src2,OutputArray dst,int code)

So far, this function only supports conversion from YUV420 to RGB.

The parameters are as follows:

parameter name Parameter Description
src1 8-bit image of Y plane (CV_8U)
src2 Contains images of interlaced U/V planes.
dst Output image
code Specify the conversion type.

The value of code is as follows:

  • COLOR_YUV2BGR_NV12
  • COLOR_YUV2RGB_NV12
  • COLOR_YUV2BGRA_NV12
  • COLOR_YUV2RGBA_NV12
  • COLOR_YUV2BGR_NV21
  • COLOR_YUV2RGB_NV21
  • COLOR_YUV2BGRA_NV21
  • COLOR_YUV2RGBA_NV21

3、cv::demosaicing


The main function of all demosaicing

void cv::demosaicing(InputArray src,OutputArray dst,int code,int dstCn = 0)

The parameters are as follows:

parameter name Parameter Description
src Input image: 8-bit unsigned or 16-bit unsigned.
dst Output an image with the same size and depth as src.
code Color space conversion code (see description below).
dstCn The number of channels in the target image; if the parameter is 0, the number of channels is automatically derived from src and code.

This function can perform the following conversions:

Guess you like

Origin blog.csdn.net/wujuxKkoolerter/article/details/112426583