OpenCV读取yuv420对应的灰度图像

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

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

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值