19,468
社区成员
发帖
与我相关
我的任务
分享
void WriteYuv()
{
cv::VideoCapture vc;
bool flag = vc.open("S1000008.avi");
if (!flag)
{
printf("avi file open error \n");
system("pause");
exit(-1);
}
int frmCount = vc.get(CV_CAP_PROP_FRAME_COUNT);
frmCount -= 5;
printf("frmCount: %d \n", frmCount);
int w = vc.get(CV_CAP_PROP_FRAME_WIDTH);
int h = vc.get(CV_CAP_PROP_FRAME_HEIGHT);
int bufLen = w*h*3/2;
unsigned char* pYuvBuf = new unsigned char[bufLen];
FILE* pFileOut = fopen("result.yuv", "w+");
if (!pFileOut)
{
printf("pFileOut open error \n");
system("pause");
exit(-1);
}
printf("pFileOut open ok \n");
for (int i=0; i<frmCount; i++)
{
printf("%d/%d \n", i+1, frmCount);
cv::Mat srcImg;
vc>>srcImg;
cv::imshow("img", srcImg);
cv::waitKey(1);
cv::Mat yuvImg;
cv::cvtColor(srcImg, yuvImg, CV_BGR2YUV_I420);
memcpy(pYuvBuf, yuvImg.data, bufLen*sizeof(unsigned char));
fwrite(pYuvBuf, bufLen*sizeof(unsigned char), 1, pFileOut);
}
fclose(pFileOut);
delete[] pYuvBuf;
}