cv::Mat到YUV420的转换

某些特定场合我们会经常遇到yuv420格式的视频文件,这种视频帧无法直接用于OpenCV,故而,需要进行格式转换;幸运的是,opencv提供了rgb到yuv420的格式转换函数;下面给出基本用法;


函数1:读取avi格式的视频文件,转换成Yuv420格式,并写入文件;

  1. void WriteYuv()  
  2. {  
  3.     cv::VideoCapture vc;  
  4.     bool flag = vc.open("S1000008.avi");  
  5.     if (!flag)  
  6.     {  
  7.         printf("avi file open error \n");  
  8.         system("pause");  
  9.         exit(-1);  
  10.     }  
  11.   
  12.     int frmCount = vc.get(CV_CAP_PROP_FRAME_COUNT);  
  13.     frmCount -= 5;  
  14.     printf("frmCount: %d \n", frmCount);  
  15.   
  16.     int w = vc.get(CV_CAP_PROP_FRAME_WIDTH);  
  17.     int h = vc.get(CV_CAP_PROP_FRAME_HEIGHT);  
  18.     int bufLen = w*h*3/2;  
  19.     unsigned char* pYuvBuf = new unsigned char[bufLen];  
  20.     FILE* pFileOut = fopen("result.yuv""w+");  
  21.     if (!pFileOut)  
  22.     {  
  23.         printf("pFileOut open error \n");  
  24.         system("pause");  
  25.         exit(-1);  
  26.     }  
  27.     printf("pFileOut open ok \n");  
  28.       
  29.     for (int i=0; i<frmCount; i++)  
  30.     {  
  31.         printf("%d/%d \n", i+1, frmCount);  
  32.   
  33.         cv::Mat srcImg;  
  34.         vc>>srcImg;  
  35.   
  36.         cv::imshow("img", srcImg);  
  37.         cv::waitKey(1);  
  38.   
  39.         cv::Mat yuvImg;  
  40.         cv::cvtColor(srcImg, yuvImg, CV_BGR2YUV_I420);  
  41.         memcpy(pYuvBuf, yuvImg.data, bufLen*sizeof(unsigned char));  
  42.   
  43.         fwrite(pYuvBuf, bufLen*sizeof(unsigned char), 1, pFileOut);  
  44.     }  
  45.   
  46.     fclose(pFileOut);  
  47.     delete[] pYuvBuf;  
  48. }  

函数二,读取yuv420格式的文件,转换成cv::Mat格式,并予以显示:
  1. void DisplayYUV()  
  2. {  
  3.     int w = 1280;  
  4.     int h = 720;  
  5.     printf("yuv file w: %d, h: %d \n", w, h);  
  6.   
  7.     FILE* pFileIn = fopen("result.yuv""rb+");  
  8.     int bufLen = w*h*3/2;  
  9.     unsigned char* pYuvBuf = new unsigned char[bufLen];  
  10.     int iCount = 0;  
  11.   
  12.   
  13.     for(int i=0; i<200; i++)  
  14.     {  
  15.         fread(pYuvBuf, bufLen*sizeof(unsigned char), 1, pFileIn);  
  16.   
  17.         cv::Mat yuvImg;  
  18.         yuvImg.create(h*3/2, w, CV_8UC1);   
  19.         memcpy(yuvImg.data, pYuvBuf, bufLen*sizeof(unsigned char));  
  20.         cv::Mat rgbImg;  
  21.         cv::cvtColor(yuvImg, rgbImg, CV_YUV2BGR_I420);  
  22.   
  23.         cv::imshow("img", yuvImg);  
  24.         cv::waitKey(1);  
  25.   
  26.         printf("%d \n", iCount++);  
  27.     }  
  28.   
  29.     delete[] pYuvBuf;  
  30.   
  31.   
  32.     fclose(pFileIn);  


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值