OpenCV读取yuv420对应的灰度图像

来源:互联网 发布:验血单哪项数据看男女 编辑:程序博客网 时间:2024/05/16 00:27

OpenCV读取yuv420对应的灰度图像

程序代码如下:

#include <OpenCV/highgui.h>#include <iostream>using namespace std;#define nWidth 448#define nHeight 336#define FrameSize nWidth*nHeight*3/2int main(){    FILE *f ;    if(!(f = fopen("C:\\Users\\Administrator\\Desktop\\yuv_pic\\图片_448x336.yuv","rb")))    {        cout << "file open error!" << endl;    }    // 计算帧数    fseek(f, 0, SEEK_END);    int frame_count = 0;    long file_size = 0;    frame_count = (int) ((int)ftell(f)/((nWidth * nHeight * 3) / 2));      cout << "frame num is " << frame_count << endl;    cout << "file length is " << ftell(f) << endl;    fseek(f, 0, SEEK_SET);    IplImage *image = cvCreateImage(cvSize(nWidth, nHeight),IPL_DEPTH_8U,1);  // 控制只显示灰度图像    unsigned char *pBuf = new unsigned char[nWidth*nHeight*3/2];    fread(pBuf, 1, (nWidth * nHeight * 3) / 2, f);    cvSetData(image, pBuf, nWidth);    cvNamedWindow("显示");    cvShowImage("显示", image);    cvWaitKey( 0 );    cvDestroyWindow("显示");    cvReleaseImage(&image);    delete []pBuf;    fclose(f);    return 0;}

程序执行结果如下:
这里写图片描述