YUV与RGB图像的互转

  • RGB图像转YUV420通道图像代码

void rgb2YUV(cv::Mat srcImg)
{
    cv::imshow("srcImg", srcImg);
    int w = srcImg.cols;
    int h = srcImg.rows;
    int bufLen = w*h*3/2;
    unsigned char* pYuvBuf = new unsigned char[bufLen];
    FILE* pFileOut = fopen("test.yuv", "w+");
    cv::Mat yuvImg;
    cv::cvtColor(srcImg, yuvImg, CV_BGR2YUV_I420);
    imwrite("/Users/Guigu/Desktop/testyuv.jpg", yuvImg);
    memcpy(pYuvBuf, yuvImg.data, bufLen*sizeof(unsigned char));

    cv::imshow("YUV", yuvImg);
    waitKey(10);

    fwrite(pYuvBuf, bufLen*sizeof(unsigned char), 1, pFileOut);
    fclose(pFileOut);
    delete[] pYuvBuf; 
}
  • YUV图像转RGB图像代码

void YUV2rgb()
{
    int w = 1024;
    int h = 768;
    printf("yuv file w: %d, h: %d \n", w, h);    
    FILE* pFileIn = fopen("../Desktop/test.yuv", "rb+");
    int bufLen = w*h*3/2;
    unsigned char* pYuvBuf = new unsigned char[bufLen];
    int iCount = 0;

    fread(pYuvBuf, bufLen*sizeof(unsigned char), 1, pFileIn);

    cv::Mat yuvImg;
    yuvImg.create(h*3/2, w, CV_8UC1);
    memcpy(yuvImg.data, pYuvBuf, bufLen*sizeof(unsigned char));
    cv::Mat rgbImg;
    cv::cvtColor(yuvImg, rgbImg, CV_YUV2BGR_I420);

    cv::imshow("rgbimg", rgbImg);
    //imwrite("../Desktop/oupt.jpg", rgbImg);
    cv::waitKey(1);
    delete[] pYuvBuf;
    fclose(pFileIn);  
}
  • Tips: 测试的时候发现图像的长宽需要为偶数,原因可能是因为YUV的图像大小为RGB的weight*height*3/2,所有weight*height需要为2的倍数才行。
    如读入的图像长宽不为偶数,可同过如下方式把它变为偶数。
    Mat img_temp = imread(path,1);
    int x_=0;
    int y_=0;
    int w_=img_temp.cols%2==0?img_temp.cols:img_temp.cols-1;
    int h_=img_temp.rows%2==0?img_temp.rows:img_temp.rows-1;
    Rect roi(x_,y_,w_,h_);

    Mat img=img_temp(roi).clone();

关于YUV空间的详细介绍,可以参考下面这篇文章,看了就知道上面乘的系数为什么是3/2了。
http://www.cnblogs.com/azraelly/archive/2013/01/01/2841269.html

Reference:
http://www.cnblogs.com/azraelly/archive/2013/01/01/2841269.html
http://blog.csdn.net/carson2005/article/details/38351717

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值