getframe2.m 2.18 KB
% modified from shape_exception_handling_all.m
% get index of 10 frames from seg.nii
% find the same index of flair.nii data and save them to outfolder

inputheader = '..\data\MICCAI_BraTS_2019_Data_Training\HGG\';
outfolder = '..\data\MICCAI_BraTS_2019_Data_Training\HGG_flair_frames\';

files = dir(inputheader);
id = {files.name};
%  files + dir  dir 
dirFlag = [files.isdir] & ~strcmp(id, '.') & ~strcmp(id, '..'); 
subFolders = files(dirFlag);

ecp_cnt = 0;
% get filenames in getname_path folder
for i = 1 : length(subFolders)
    id = subFolders(i).name;
    fprintf('\nSub folder #%d = %s: ', i, id);
    
    type = 'seg.nii';
    filename = strcat(id,'_', type); % BraTS19_2013_2_1_seg.nii
    seg_path = strcat(inputheader, id, '\', filename,'\', filename);
    seg = niftiread(seg_path); %size 240x240x155 
    segdata = seg;
    
    [x,y,z] = size(segdata);
    
    
    type = 'flair.nii';
    filename = strcat(id,'_', type); % BraTS19_2013_2_1_flair.nii
    flair_path = strcat(inputheader, id, '\', filename,'\', filename);
    flair = niftiread(flair_path); %size 240x240x155 
    flairdata = flair;
    
    
    
    idx = 1;
    frames = zeros(1,1);
    for j = 1 : z 
         n_nonblack = numel(find(segdata(:,:,j) > 0));
         if(n_nonblack > 70)
              frames(idx,1) = j;
              idx = idx + 1;
         end
    end

    c = 0;
    [nrow, ncol] = size(frames);
    step = round(nrow/11);

    
    for k = 1 : step : step*10
        %fprintf('%d ', k);
        if k > size(frames, 1)
            k = size(frames, 1);
            fprintf('%s', 'EXCEPTION occured');
            ecp_cnt = ecp_cnt +1;
        end
        fprintf('%d ', frames(k, 1));
        type = '.png';
        filename = strcat(id, '_', int2str(c), type); % BraTS19_2013_2_1_c.png
        outpath = convertCharsToStrings(strcat(outfolder, filename));
        % typecase int16 to double, range[0, 1], rotate 90 and filp updown
        % range [0, 1]
        cp_data = flipud(rot90(mat2gray(double(flairdata(:,:,frames(k, 1))))));
%         M = max(cp_data(:));
%         disp(M);
        imwrite(cp_data, outpath);
        
        c = c+ 1; 
    end

    
end
fprintf('\n%s: %d', 'num exception', ecp_cnt);