Matlab 读写yuv文件

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

你可能感兴趣的:(ffmpeg,hevc)