getNonLesionframes.m
1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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