getframe.m 1.56 KB
inputheader = '..\data\MICCAI_BraTS_2019_Data_Training\HGG_seg_flair\';
outfolder = '..\data\MICCAI_BraTS_2019_Data_Training\total_frame_m255\';

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


for i = 1 : 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
        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
        % range [0, 1]
        %cp_data = flipud(rot90(mat2gray(double(data(:,:,k)))));
        cp_data = flipud(rot90(mat2gray(double(data(:,:,k))))).*255;
%         M = max(cp_data(:));
%         disp(M);
        imwrite(cp_data, outpath);
        
        c = c+ 1; 
    end
    
end


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