getNonLesionframes.m 1.99 KB
inputheader = '..\data\MICCAI_BraTS_2019_Data_Training\HGG\';
outfolder = '..\data\MICCAI_BraTS_2019_Data_Training\NonLesion_flair_frame\';

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


% 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_seg_nonblack = numel(find(segdata(:,:,j) > 0));
         n_flair_nonblack = numel(find(flairdata(:,:,j) > 0));
         if((n_seg_nonblack == 0) && n_flair_nonblack > 12000) % frames without lesions
              frames(idx,1) = j;
              idx = idx + 1;
         end
    end

    c = 0;
    [nrow, ncol] = size(frames);

    if frames(1, 1) ~= 0 % n(non lesion frames) > 0
        for k = 1 : nrow
            fprintf('%d ', frames(k, 1));        
            type = '.png';
            filename = strcat('nml_', 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

    
end