yuv420转为cv::Mat




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


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

[cpp]  view plain copy
  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]  view plain copy
  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




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


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

[cpp]  view plain copy
  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]  view plain copy
  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



  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个示例代码,演示了如何在YUV_Y422类型的帧缓冲上使用OpenCVcv::Mat进行BGR格式的图像处理,并绘制线条,最后将结果换回YUV_Y422类型的帧缓冲: ```cpp #include <opencv2/opencv.hpp> int main() { // 假设帧缓冲的宽度和高度 int width = 1920; int height = 1080; // 创建帧缓冲,用于存储YUV_Y422图像数据 unsigned char* frameBuffer = new unsigned char[width * height * 2]; // 每个像素占2个字节 // 创建cv::Mat对象,与帧缓冲共享数据 cv::Mat yuvImage(height, width, CV_8UC2, frameBuffer); // CV_8UC2表示每个像素占2个字节 // 在cv::Mat对象上进行图像处理 cv::cvtColor(yuvImage, yuvImage, cv::COLOR_YUV2BGR_Y422); // 将YUV_Y422图像换为BGR图像 // 在BGR图像上绘制线条 cv::line(yuvImage, cv::Point(0, 0), cv::Point(width - 1, height - 1), cv::Scalar(0, 255, 0), 2); // 将cv::Mat对象换为YUV_Y422帧缓冲数据 unsigned char* convertedFrameBuffer = yuvImage.data; // 进行其他操作... // 释放帧缓冲内存 delete[] frameBuffer; return 0; } ``` 在这个示例中,我们首先创建了一个YUV_Y422类型的帧缓冲数组。然后,我们使用`cv::Mat`的构造函数将帧缓冲数据与`cv::Mat`对象共享。接下来,我们可以在`cv::Mat`对象上进行图像处理操作,例如将YUV_Y422图像换为BGR图像,然后使用`cv::line`函数在BGR图像上绘制线条。最后,我们可以通过访问`cv::Mat`对象的`data`成员将其换回YUV_Y422帧缓冲数据。 请注意,示例中的代码假设每个像素占2个字节(YUV_Y422格式)。如果您使用其他图像格式,请根据实际情况进行调整。另外,确保在将帧缓冲数据复制到`cv::Mat`对象时,分配的内存大小和数据布局与帧缓冲一致。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值