getframe2.m
2.18 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
67
68
69
70
71
72
73
74
75
76
% 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);