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

OpenCV读取yuv420对应的灰度图像

2017-06-23 16:06 225 查看

OpenCV读取yuv420对应的灰度图像

程序代码如下:

#include <OpenCV/highgui.h>
#include <iostream>
using namespace std;

#define nWidth 448
#define nHeight 336
#define FrameSize nWidth*nHeight*3/2

int 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;
}


程序执行结果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息