您的位置:首页 > 运维架构

利用opencv将录制的rgb图像转化为yuv文件并保存。

2014-09-25 22:02 766 查看
#include <iostream>
#include <opencv2/opencv.hpp>
#include <cstdlib>

using namespace std;
using namespace cv;

int main()
{
Mat frame, yuv;

VideoCapture capture;
int w = 320;
int h = 240;
capture.open(0);
capture.set(CV_CAP_PROP_FRAME_WIDTH,w);
capture.set(CV_CAP_PROP_FRAME_HEIGHT,h);

/*
VideoCapture capture;
int w = 1280;
int h = 720;
string filename = "./xx.mp4";
capture.open(filename);
*/
if(!capture.isOpened())
{
cerr << "No camera or video input!\n";
return -1;
}

int bufLen = w*h*3/2;
unsigned char* pYuvBuf = new unsigned char[bufLen];
FILE* pFileOut = fopen("xx.yuv", "wb");
if (!pFileOut)
{
printf("pFileOut open error \n");
system("pause");
exit(-1);
}
int cnt = 0;

while(true)
{
capture >> frame;
if(frame.empty())
continue;
imshow("origin",frame);
cvtColor(frame,yuv,CV_RGB2YUV_I420);

imshow("yuv",yuv);

memcpy(pYuvBuf, yuv.data, bufLen*sizeof(unsigned char));

fwrite(pYuvBuf, bufLen*sizeof(unsigned char), 1, pFileOut);

if(cvWaitKey(10) == 'q')
break;
}

fclose(pFileOut);
delete[] pYuvBuf;
system("pause");
return 0;
}
用yuvviewer可以查看是否最后保存的yuv文件正确。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: