I420转rgb

1.自己写转换方法

unsigned char* I420ToRGB(unsigned char* src, int width, int height){
    const int R = 0;
    const int G = 1;
    const int B = 2;
    int numOfPixel = width * height;
    int positionOfU = numOfPixel;
    int positionOfV = numOfPixel/4 + numOfPixel;
    unsigned char* rgb = new unsigned char[numOfPixel*3];

    for(int i=0; i

2.借用opencv


#include 
#include 

unsigned char* I420ToRGB(unsigned char* src, int width, int height){
    int numOfPixel = width * height;
    unsigned char* rgb = new unsigned char[numOfPixel*3];

    cv::Mat yuvImg;
    cv::Mat rgbImg(height, width, CV_8UC3);
    yuvImg.create(height * 3/2, width, CV_8UC1);
    memcpy(yuvImg.data, src, numOfPixel*3/2);
    cv::cvtColor(yuvImg, rgbImg, cv::ColorConversionCodes::COLOR_YUV2RGB_I420);

    memcpy_s(rgb, numOfPixel*3, rgbImg.data, numOfPixel*3);

    return rgb;
}

顺便opencv实现yuv转rgb

    //unsigned char *src 存的是YUYV的裸数据;
    cv::Mat yuvImg;
    cv::Mat rgbImg(height, width,CV_8UC3);
    yuvImg.create(height, width, CV_8UC2);
    memcpy(yuvImg.data, src, numOfPixel*2);
    cv::cvtColor(yuvImg, rgbImg, cv::ColorConversionCodes::COLOR_YUV2BGR_YUYV);

 

你可能感兴趣的:(C++,opencv)