博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cv::Mat到YUV420的转换
阅读量:4298 次
发布时间:2019-05-27

本文共 1760 字,大约阅读时间需要 5 分钟。

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

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

[cpp] 
  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格式,并予以显示:

[cpp] 
  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);  
  33. }  
来源:http://blog.csdn.net/carson2005/article/details/38351717
你可能感兴趣的文章
apache+nginx 实现动静分离
查看>>
通过Navicat远程连接MySQL配置
查看>>
phpstorm开发工具的设置用法
查看>>
Linux 系统挂载数据盘
查看>>
Git基础(三)--常见错误及解决方案
查看>>
Git(四) - 分支管理
查看>>
PHP Curl发送数据
查看>>
HTTP协议
查看>>
HTTPS
查看>>
git add . git add -u git add -A区别
查看>>
apache下虚拟域名配置
查看>>
session和cookie区别与联系
查看>>
PHP 实现笛卡尔积
查看>>
Laravel中的$loop
查看>>
CentOS7 重置root密码
查看>>
Centos安装Python3
查看>>
PHP批量插入
查看>>
laravel连接sql server 2008
查看>>
Laravel 操作redis的各种数据类型
查看>>
Laravel框架学习笔记之任务调度(定时任务)
查看>>