[OpenCV] 使用OpenCV进行yuv和rgb的转换

int main()
{
/*
    cout << "Hello World!" << endl;

    Mat rgb = imread("/home/ubuntu/rgb320.jpg");
    Mat nv12;
    Mat rgb2;
    cvtColor(rgb, nv12, COLOR_RGB2YUV_I420);
    //imwrite("/home/ubuntu/nv12.yuv",nv12,0);
    FILE* f = fopen("/home/ubuntu/nv12,yuv","wb+");
    fwrite(nv12.data,1,rgb.rows*rgb.cols*3/2,f);
    cvtColor(nv12, rgb2, COLOR_YUV2RGB_I420);
    imwrite("/home/ubuntu/rgb2.png",rgb2);

    return 0;
*/
    Mat yuvNV12;
    Mat gbr24;
    //1.read nv12 file to nv12 mat
    FILE* f = fopen("/home/ubuntu/rgb320.yuv","r");
    char buff[960000];
    memset(buff,0,960000);
    fread(buff,1,180*320*3/2,f);
    yuvNV12.create(180*3/2,320,CV_8UC1);
    memcpy(yuvNV12.data,buff,320*180*3/2);
    //2.cvt nv12 mat to rgb24 mat
    cvtColor(yuvNV12,gbr24, COLOR_YUV2RGB_I420);
    //fwrite(yuvNV12.data,1,gbr24.rows*gbr24.cols*3/2,f);
    //3.
    imwrite("/home/ubuntu/rgb320_new.jpg",gbr24);
    fflush(f);
    fclose(f);
}

你可能感兴趣的:(#,OpenCV,多媒体)