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;
}
程序执行结果如下:

本文介绍了一个使用OpenCV从YUV420文件中读取并显示灰度图像的示例程序。该程序计算了文件中的帧数,并将图像加载到OpenCV的数据结构中进行展示。
8765

被折叠的 条评论
为什么被折叠?



