yuv420文件通过C语言分别读取出Y,U,V

YUV总共有三种格式:YUV444,YUV422,YUV420
YUV 4:4:4,每一个 Y 分量对于一对 UV 分量,每像素占用 (Y + U + V = 8 + 8 + 8 = 24bits)3 字节;各采样分量在扫面每个像素点时,都不会降低采样率
YUV 4:2:2,每两个 Y 分量共用一对 UV 分量,每像素占用 (Y + 0.5U + 0.5V = 8 + 4 + 4 = 16bits)2 字节;水平方向Y分量与UV分量2:1采样,垂直方向不降低采样率。
YUV 4:2:0,每四个 Y 分量共用一对 UV 分量,每像素占用 (Y + 0.25U + 0.25V = 8 + 2 + 2 = 12bits)1.5 字节。水平和垂直方向上Y分量和UV分量对的采样比都是2:1。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <malloc.h>

void readYUV(const char* path, int width, int height)
{
  FILE* f = fopen(path, "rb+");
  FILE* f1 = fopen("yuv420y.y", "wb+");
  FILE* f2 = fopen("yuv420y.u", "wb+");
  FILE* f3 = fopen("yuv420y.v", "wb+");

/*yuv420就是每4个y共享一组uv,也就是说每个像素点都有一个y与之对应,每4个像素点共用同一组uv,
这样Y的大小为width*height,而uv两个分量的个数总共为1/2的Y为width*height*1/2,相加就是总的字节数,
了解了这个就可以很容易计算出图像的yuv420的大小为 size(yuv420)=width*height*3/2,就是宽高大小的1.5倍
yuv444的大小为size(yuv444)=width*height*3;
yuv422的大小为size(yuv422)=width*height*2;
*/
  char* data = (char*)malloc(width*height * 3 / 2);

  fread(data, 1, width*height * 3 / 2, f);//图像总的字节大小
  fwrite(data, 1, width*height, f1);//每个像素点都有一个y,所以y的大小为w*h
  fwrite(data+width*height, 1, width*height/4, f2);
  fwrite(data+width*height*5/4, 1, width*height/4, f3);
  free(data);
  data = nullptr;
  fclose(f);
  fclose(f1);
  fclose(f2);
  fclose(f3);
}


void main()
{
  readYUV("test.yuv", 720, 480);
}
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值