欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

将cv::Mat转换为YUV420

最编程 2024-08-15 15:27:49
...
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;  
}  

推荐阅读