MATLAB随手记

MATLAB随手记

1. 读写文件

简单读写

fp = fopen('record.txt', 'r');
while ~feof(fp) % 只要没读完
    line = fgetl(fp); % 读下一行
    if contains(line, 'Hello')
        pass
    end
end

将rgb保存为yuv文件

%mov2yuv creates a Matlab-Movie from a YUV-File.
%	mov2yuv('Filename',mov, format) writes the specified file
%	using format for YUV-subsampling.
%	
%	Filename --> Name of File (e.g. 'Test.yuv')
%   mov      --> Matlab-Movie
%   format   --> subsampling rate ('400','411','420','422' or '444')
%
%example: mov2yuv('Test.yuv',mov,'420');

function mov2yuv(File,mov,format)

    %set factor for UV-sampling
    fwidth = 0.5;
    fheight= 0.5;
    if strcmp(format,'400')
        fwidth = 0;
        fheight= 0;
    elseif strcmp(format,'411')
        fwidth = 0.25;
        fheight= 1;
    elseif strcmp(format,'420')
        fwidth = 0.5;
        fheight= 0.5;
    elseif strcmp(format,'422')
        fwidth = 0.5;
        fheight= 1;
    elseif strcmp(format,'444')
        fwidth = 1;
        fheight= 1;
    else
        display('Error: wrong format');
    end
    %get Resolution and Framenumber
    resolution = size(mov(1).cdata);
    framenumber = size(mov);
    framenumber = framenumber(2);

    fclose(fopen(File,'w')); %Init File
    h = waitbar(0,'Please wait ... ');
    %write YUV-Frames
    for cntf = 1:1:framenumber
        waitbar(cntf/framenumber,h);
        YUV = rgb2ycbcr(mov(cntf).cdata);
        save_yuv(YUV,File,resolution(2),resolution(1),fheight,fwidth);
    end
    close(h);


%Save YUV-Data to File
function save_yuv(data,video_file,BreiteV,HoeheV,HoehenteilerV,BreitenteilerV)

    %get Resolution od Data
    datasize = size(data);
    datasizelength = length(datasize);

    %open File
    fid = fopen(video_file,'a');

    %subsampling of U and V
    if datasizelength == 2 | HoehenteilerV == 0
        %4:0:0
        y(1:HoeheV,1:BreiteV) = data(:,:,1);
    elseif datasizelength == 3
        y(1:HoeheV,1:BreiteV) = double(data(:,:,1));
        u(1:HoeheV,1:BreiteV) = double(data(:,:,2));
        v(1:HoeheV,1:BreiteV) = double(data(:,:,3));
        if BreitenteilerV == 1
            %4:1:1
            u2 = u;
            v2 = v;
        elseif HoehenteilerV == 0.5
            %4:2:0
            u2(1:HoeheV/2,1:BreiteV/2) = u(1:2:end,1:2:end)+u(2:2:end,1:2:end)+u(1:2:end,2:2:end)+u(2:2:end,2:2:end);
            u2                         = u2/4;
            v2(1:HoeheV/2,1:BreiteV/2) = v(1:2:end,1:2:end)+v(2:2:end,1:2:end)+v(1:2:end,2:2:end)+v(2:2:end,2:2:end);
            v2                         = v2/4;
        elseif BreitenteilerV == 0.25
            %4:1:1
            u2(1:HoeheV,1:BreiteV/4) = u(:,1:4:end)+u(:,2:4:end)+u(:,3:4:end)+u(:,4:4:end);
            u2                       = u2/4;
            v2(1:HoeheV,1:BreiteV/4) = v(:,1:4:end)+v(:,2:4:end)+v(:,3:4:end)+v(:,4:4:end);
            v2                       = v2/4;
        elseif BreitenteilerV == 0.5 & HoehenteilerV == 1
            %4:2:2
            u2(1:HoeheV,1:BreiteV/2) = u(:,1:2:end)+u(:,2:2:end);
            u2                       = u2/2;
            v2(1:HoeheV,1:BreiteV/2) = v(:,1:2:end)+v(:,2:2:end);
            v2                       = v2/2;
        end
    end

    fwrite(fid,uint8(y'),'uchar'); %writes Y-Data

    if HoehenteilerV ~= 0
        %writes U- and V-Data if no 4:0:0 format
        fwrite(fid,uint8(u2'),'uchar');
        fwrite(fid,uint8(v2'),'uchar');
    end

    fclose(fid);

从yuv文件中读取Y通道

function [y] = y_import(filename,dims,range_frames,yuvformat)
% Import Y channel from a yuv video.
% dims: [width, height]
% range_frames: 1:nf
% return: a cell (nfs * height * width)

%% Read yuv
fid=fopen(filename,'r');
if (fid < 0)
    error('File does not exist!');
end

%% Fill up default
if (nargin < 4)
    yuvformat = 'YUV420_8';
end

%% Calculate inprec and sampl
inprec = 'ubit8';
sampl = 420;
if (strcmp(yuvformat,'YUV420_16'))
    inprec = 'uint16'; %'ubit16=>uint16'
elseif (strcmp(yuvformat,'YUV444_8'))
    sampl = 444;
end

%% Calculate frelem
if (sampl == 420)
    dimsUV = dims / 2;
else
    dimsUV = dims;
end

Yd = zeros(dims(1),dims(2));
frelem = numel(Yd) + 2 * dimsUV(1) * dimsUV(2);

%% extraction

num_frames = length(range_frames);
y = cell(1,num_frames);

for ite_frame = 1:num_frames

    startfrm = range_frames(ite_frame) - 1; % the first frame start from 0 frame.
    fseek(fid, startfrm * frelem , -1); % go to the starting frame

    Yd = fread(fid,dims,inprec);
    y{ite_frame} = Yd'; % height * width

end

fclose(fid);

return

将TIFF图片拼接为yuv文件

dir_img = "G:\SCI\Database\RAISE_8k";
image_list = dir(fullfile(dir_img, "*.TIF"));
image_list = {image_list.name}';

num_image = length(image_list);
random_order = randperm(num_image); % randperm(N)将会使得[1 2 ... N]序列打乱并返回该行向量。
index_list_tra = random_order(1:4900);
index_list_val = random_order(4900+1:4900+1628);
index_list_test = random_order(4900+1628+1:8156);

tar_w = 1920;
tar_h = 1080;

dir_store = ".\Database";

%% Training set
filename = "RAISE-8156_raw_1920x1080_4900_tra.yuv";
fid= fopen(fullfile(dir_store, filename),'w');
nfs = length(index_list_tra);
for i = 1:nfs
    disp(string(i) + " | " + string(nfs));
    
    index = index_list_tra(i);
    img = Tiff(fullfile(dir_img, image_list(index)),'r');
    img = read(img);
    
    YUVimg = rgb2ycbcr(img);
    %imshow(YUVimg)
    
    [imgHeight imgWidth imgDim] = size(YUVimg);
    Y = YUVimg(imgHeight/2-(tar_h/2-1):imgHeight/2+tar_h/2,imgWidth/2-(tar_w/2-1):imgWidth/2+tar_w/2,1);     % Y 矩阵  % Cut to 832x480
    U = YUVimg(imgHeight/2-(tar_h/2-1):imgHeight/2+tar_h/2,imgWidth/2-(tar_w/2-1):imgWidth/2+tar_w/2,2);     % U 矩阵
    V = YUVimg(imgHeight/2-(tar_h/2-1):imgHeight/2+tar_h/2,imgWidth/2-(tar_w/2-1):imgWidth/2+tar_w/2,3);     % V 矩阵
    [imgHeight imgWidth] = size(Y);
    
    y = double(Y);
    u = double(U);
    v = double(V);
    
    u2(1:imgHeight/2,1:imgWidth/2) = u(1:2:end,1:2:end)+u(2:2:end,1:2:end)+u(1:2:end,2:2:end)+u(2:2:end,2:2:end);
    u2                         = u2/4;
    v2(1:imgHeight/2,1:imgWidth/2) = v(1:2:end,1:2:end)+v(2:2:end,1:2:end)+v(1:2:end,2:2:end)+v(2:2:end,2:2:end);
    v2                         = v2/4;
    
    %imshow(Y);
    %fwrite(fid,yuv420out,'uint8');
    fwrite(fid,uint8(y'),'uchar');
    fwrite(fid,uint8(u2'),'uchar');
    fwrite(fid,uint8(v2'),'uchar');
end
fclose(fid);

2. 字符串操作

  • pos = strfind(str_t, "Hello"):会返回所有搜寻到的Hello的首字母位置。

  • num = str2double(str_t)

3. 画图

  • 依次图例:legend(["a","b","c"])
posted @ 2019-11-23 12:58  RyanXing  阅读(469)  评论(0编辑  收藏  举报