欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Matlab 读写yuv文件

程序员文章站 2022-04-01 08:44:48
...
clear all; clc;

fid = fopen('Data/Testing/down.yuv','r'); %读入文件
fod = fopen('Data/Testing/test.yuv','w'); %输出文件
row=768/2;col=1024/2; %图像的高、宽
frames=97; % total=97 %序列的帧数

for frame=1:frames
 %读入文件 将yuv转换为rgb,并用imshow显示
  %  im_l_y=fread(fid,[row,col],'uchar');  %错误的读入
    im_l_y = zeros(row,col); %Y
    for i1 = 1:row 
       im_l_y(i1,:) = fread(fid,col);  %读取数据到矩阵中 
    end

    im_l_cb = zeros(row/2,col/2); %cb
    for i2 = 1:row/2 
       im_l_cb(i2,:) = fread(fid,col/2);  
    end

    im_l_cr = zeros(row/2,col/2); %cr
    for i3 = 1:row/2 
       im_l_cr(i3,:) = fread(fid,col/2);  
    end

    %由于输入的yuv文件为4:2:0,所以CbCr要改变大小,
    %否则im_l_ycbcr(:, :, 2) =im_l_cb;会出现错误
    im_l_cb = imresize(im_l_cb, [row, col], 'bicubic');%改变图像的大小
    im_l_cr = imresize(im_l_cr, [row, col], 'bicubic');
    im_l_ycbcr = zeros([row, col, 3]);
    im_l_ycbcr(:, :, 1) = im_l_y;
    im_l_ycbcr(:, :, 2) = im_l_cb;
    im_l_ycbcr(:, :, 3) = im_l_cr;

    im_l = ycbcr2rgb(uint8(im_l_ycbcr)); %将YCbCr转换为rgb
  %  figure, imshow(im_l);title('Down');

  %输出文件  将rgb转换为yuv
    im_t_ycbcr = rgb2ycbcr(im_l);  %将rgb转换为YCbCr
    im_t_y = im_t_ycbcr(:, :, 1);  %Y
    im_t_cb = im_t_ycbcr(:, :, 2); %Cb
    im_t_cr = im_t_ycbcr(:, :, 3); %Cr
    im_l_cb = imresize(im_l_cb, [row/2, col/2], 'bicubic');%改变图像的大小
    im_l_cr = imresize(im_l_cr, [row/2, col/2], 'bicubic');
    for i1 = 1:row 
       fwrite(fod,im_l_y(i1,:));  %输出到文件中
    end
    for i1 = 1:row/2
       fwrite(fod,im_l_cb(i1,:));  
    end
    for i1 = 1:row/2
       fwrite(fod,im_l_cr(i1,:)); 
    end


end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57