getframe.m 1.4 KB
inputheader = '..\data\MICCAI_BraTS_2019_Data_Training\HGG_seg_flair\';
outfolder = '..\data\MICCAI_BraTS_2019_Data_Training\frame\';

files = dir(inputheader);
id = {files.name};
%  files + dir  file 
flag = ~strcmp(id, '.') & ~strcmp(id, '..'); 
files = files(flag);


for i = 1 : length(files)

    id = split(files(i).name, '.nii');
    id = char(id(1));
    fprintf('ID #%d = %s\n', i, id);
    filename = id; % BraTS19_2013_2_1_seg_flair.nii
    data_path = strcat(inputheader,'\', filename);
    data = niftiread(data_path); %size 240x240x155 

    [x,y,z] = size(data);

    for i = 1 : z 
        [row, col] = find(data(:,:,i) ~= 0);
        if(size(row) > 0)
            st = i;
            break;
        end
    end

    for i = z : -1 : 1 
        [row, col] = find(data(:,:,i) ~= 0);
        if(size(row) > 0)
            en = i;
            break;
        end
    end

    c = 0;
    step = round(((en) - (st))/11);
    for k = st + step : step : st + step*10
        c = c+ 1; 

        type = '.png';
        filename = strcat(id, '_', int2str(c), type); % BraTS19_2013_2_1_seg_flair_c.png
        outpath = strcat(outfolder, filename);
        % typecase int16 to double, range[0, 1], rotate 90 and filp updown
        cp_data = flipud(rot90(mat2gray(double(data(:,:,k)))));
        imwrite(cp_data, outpath);
    end
    
end


% p = 'st: %d\n';
% fprintf(p, st);
% p = 'en: %d\n';
% fprintf(p, en);