博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
opencv 读取 yuv(4:2:0)
阅读量:4055 次
发布时间:2019-05-25

本文共 1796 字,大约阅读时间需要 5 分钟。

转自:

#include <opencv/highgui.h>

#include <stdio.h>

 

//#include <>

#define nWidth 352

#define nHeight 288
#define FrameSize nWidth*nHeight*3/2
int _tmain(int argc, _TCHAR* argv[])
{
 /*IplImage *img = cvLoadImage("D:\\workspace\\cv_yun\\DSC_1598.JPG");
 cvNamedWindow("a");
 cvShowImage("a",img);
 cvWaitKey(0);
 */

 FILE *f ;
 if(!(f = fopen("目录")))
 {
  printf("file open error!");
 }

 // calculate the frame num
 fseek(f, 0, SEEK_END);
 int frame_count = 0;
 long file_size = 0;
 frame_count = (int) ((int)ftell(f)/((nWidth * nHeight * 3) / 2));  // ftell 用于求文件大小
 printf("frame num is %d \n", frame_count);
 printf("file length is   %d",ftell(f));

 fseek(f, 0, SEEK_SET);//文件内位置定位到文件头
 //IplImage *img = cvCreateImage(cvSize(352,288),IPL_DEPTH_8U,1);
 //IplImage *grey;
 IplImage *yimg = cvCreateImage(cvSize(nWidth, nHeight),IPL_DEPTH_8U,1);
 //IplImage *uimg = cvCreateImageHeader(cvSize(nWidth/2, nHeight/2),IPL_DEPTH_8U,1);  
    //IplImage *vimg = cvCreateImageHeader(cvSize(nWidth/2, nHeight/2),IPL_DEPTH_8U,1);
 

 //----------------------------------------------------------------------实现读取一个文件 显示照片

 unsigned char *pBuf = new unsigned char[nWidth*nHeight*3/2];
 fread(pBuf, 1, (nWidth * nHeight * 3) / 2, f);// nWidth*nHeight*3/2  means 本程序使用 yuv:4:2:0 yuv分为 Y Cb Cr 三部分
 cvSetData(yimg, pBuf, nWidth);

 cvNamedWindow("a");

 cvShowImage("a", yimg);
 cvWaitKey( 0 );

 //----------------------------------------------------------------------读取yuv文件的y部分 类似于播放器效果 
 /*unsigned char *pBuf = new unsigned char[nWidth*nHeight*3/2];
 int pos = 0;
 for(int i = 0; i<frame_count-1; i++ )
 {
  fseek(f, pos, SEEK_SET);
  fread(pBuf,1 , FrameSize, f );
  cvSetData(yimg, pBuf, nWidth);
  cvNamedWindow("a");
  cvShowImage("a", yimg);
  cvWaitKey( 33 );
  pos += FrameSize;
 }
 */

 cvDestroyWindow("a");
 cvReleaseImage(&yimg);
 delete []pBuf;

 fclose(f);

 return 0;
}

转载地址:http://xuqci.baihongyu.com/

你可能感兴趣的文章
React Native(四):布局(使用Flexbox)
查看>>
React Native(七):Android双击Back键退出应用
查看>>
Android自定义apk名称、版本号自增
查看>>
adb command not found
查看>>
Xcode 启动页面禁用和显示
查看>>
【剑指offer】q50:树中结点的最近祖先
查看>>
二叉树的非递归遍历
查看>>
【leetcode】Reorder List (python)
查看>>
【leetcode】Linked List Cycle (python)
查看>>
【leetcode】Linked List Cycle (python)
查看>>
【leetcode】Candy(python)
查看>>
【leetcode】Clone Graph(python)
查看>>
【leetcode】Sum Root to leaf Numbers
查看>>
【leetcode】Pascal's Triangle II (python)
查看>>
java自定义容器排序的两种方法
查看>>
如何成为编程高手
查看>>
本科生的编程水平到底有多高
查看>>
AngularJS2中最基本的文件说明
查看>>
从头开始学习jsp(2)——jsp的基本语法
查看>>
使用与或运算完成两个整数的相加
查看>>