shape_exception_handling_all.m 1.97 KB
getname_path = '..\data\MICCAI_BraTS_2019_Data_Training\zero2\';
inputheader = '..\data\MICCAI_BraTS_2019_Data_Training\HGG_seg_flair\';
outfolder = '..\data\MICCAI_BraTS_2019_Data_Training\zero to valid2\';

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

% get filenames in getname_path folder
for i = 1 : length(files)
    %id = split(files(i).name, '.nii');
    fname = files(i).name;
    id = reverse(extractBetween(reverse(fname), 7, strlength(fname)));
    
    data_path = convertCharsToStrings(strcat(inputheader,'\', id, '.nii'));
    data = niftiread(data_path); 
    
    fprintf('ID #%d = %s\n', i, data_path);
    
    idx = 1;
    frames = zeros(1,1);
    for j = 1 : z 
         n_nonblack = numel(find(data(:,:,j) > 0));
         if(n_nonblack > 70)
              frames(idx,1) = j;
              idx = idx + 1;
         end
    end
    
%     for j = z : -1 : 1 
%          n_nonblack = numel(find(data(:,:,j) > 0));
%          if(n_nonblack > 50)
%              fprintf('%s: %d\n ','number: ', n_nonblack);
%              en = j;
%              break;
%          end
%     end
    

    c = 0;
    [nrow, ncol] = size(frames);
    step = round(nrow/11);
%     fprintf('%s: %d \n', 'nrow', nrow);
%     fprintf('%s: %d\n ','st: ', frames(1, 1));
%     fprintf('%s: %d\n ','end: ', frames(nrow, 1));
    %disp(frames);

    for k = 1 : step : step*10
        %fprintf('%d ', k);
        fprintf('%d ', frames(k, 1));
        type = '.png';
        filename = strcat(id, '_', int2str(c), type); % BraTS19_2013_2_1_seg_flair_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(data(:,:,frames(k, 1))))));
%         M = max(cp_data(:));
%         disp(M);
        imwrite(cp_data, outpath);
        
        c = c+ 1; 
    end

    
end