Skip to content

Commit f7dd83d

Browse files
author
vijayi
committed
Post v1.1 updates
1 parent 0222ee9 commit f7dd83d

6 files changed

+67
-32
lines changed

.gitattributes

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
*.fig binary
2+
*.mat binary
3+
*.mdl binary
4+
*.mdlp binary
5+
*.mexa64 binary
6+
*.mexw64 binary
7+
*.mexmaci64 binary
8+
*.mlapp binary
9+
*.mldatx binary
10+
*.mlproj binary
11+
*.mlx binary
12+
*.p binary
13+
*.sfx binary
14+
*.sldd binary
15+
*.slreqx binary
16+
*.slmx binary
17+
*.sltx binary
18+
*.slxc binary
19+
*.slx binary merge=mlAutoMerge
20+
*.slxp binary
21+
22+
## Other common binary file types
23+
*.docx binary
24+
*.exe binary
25+
*.jpg binary
26+
*.pdf binary
27+
*.png binary
28+
*.xlsx binary

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
resources/
55

66
ds000228-1.1.0/
7+
2DImageSet*
-40.1 KB
Binary file not shown.

README.md

+2-5
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,10 @@ Requires:
2828
- [Deep Learning Toolbox](https://www.mathworks.com/products/deep-learning.html)
2929
- [Image Processing Toolbox](https://www.mathworks.com/products/image.html)
3030

31-
Optional:
32-
- [Parallel Computing Toolbox](https://www.mathworks.com/products/parallel-computing.html)
33-
3431
## **References**
35-
\[1\] Richardson, H., Lisandrelli, G., Riobueno-Naylor, A., & Saxe, R. (2018). Development of the social brain from age three to twelve years. Nature Communications, 9(1), 1027. https://www.nature.com/articles/s41467-018-03399-2
32+
\[1\] Richardson, H., Lisandrelli, G., Riobueno-Naylor, A., & Saxe, R. (2018). Development of the social brain from age three to twelve years. Nature Communications, 9(1), 1027. https://doi.org/10.1038/s41467-018-03399-2
3633

37-
Copyright 2020-2021 The MathWorks, Inc.
34+
Copyright 2020 The MathWorks, Inc.
3835

3936
[![View Brain-MRI-Age-Classification-using-Deep-Learning on File Exchange](https://www.mathworks.com/matlabcentral/images/matlab-file-exchange.svg)](https://www.mathworks.com/matlabcentral/fileexchange/74941-brain-mri-age-classification-using-deep-learning)
4037

images/overview.png

-36.8 KB
Loading

prepare2DImageDataset.m

+36-27
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
1-
function exemplars = prepare2DImageDataset(srcFolder,dstFolder,augmentDataset,applySkullStripping)
2-
%PREPAREMRIAGECLASSIFICATIONDATASET Prepare 2D midslice image dataset from 3D brain volume dataset
1+
function exemplars = prepare2DImageDataset(srcPath,dstPath,augmentDataset,applySkullStripping)
2+
%PREPAREMRIAGECLASSIFICATIONDATASET Prepare 2D midslice image dataset folders from 3D brain volume dataset folders
3+
%
34
% OUTPUTS
45
% exemplars: cell array of exemplar 2D midslice images (without skull-stripping or augmentation operations) as a reference, one per age class
6+
%
7+
% USAGE
8+
% exemplars = prepare2DImageDataset(srcFlder): returns exemplar images for each class without preparing 2D image dataset folder
9+
% prepare2DImageDataset(srcFolder,dstFolder,...): creates 2D midslice image dataset folder in specified dstFolder
10+
%
511

612
arguments
7-
srcFolder (1,:) char % source folder location of 3D brain volumes, organized by participant
8-
dstFolder (1,:) char = '' % destination folder for 2D axial midslice images, organized by age class
13+
srcPath (1,:) char % source folder location of 3D brain volumes, organized by participant folders
14+
dstPath (1,:) char = '' % destination folder for 2D axial midslice images, organized by age class folders
915
augmentDataset (1,1) logical = false % specify whether to apply offline data augmentation (a copy of image flipped 180 degrees).
1016
applySkullStripping (1,1) logical = false % specify whether to apply skull-stripping
1117
end
1218

1319
% Create classification image set folders
14-
if ~isempty(dstFolder)
15-
assert(~exist(dstFolder,'dir'));
16-
mkdir(dstFolder);
20+
if ~isempty(dstPath)
21+
assert(~exist(dstPath,'dir'));
22+
mkdir(dstPath);
1723
end
1824

1925
% Load 3D image volumes into MATLAB workspace
20-
allFiles = dir(strcat(srcFolder,'\*\*_anat.nii.gz'));
26+
allFiles = dir(fullfile(srcPath,'*','*_anat.nii.gz'));
2127

2228
fileDir = fullfile({allFiles.folder},{allFiles.name});
2329

@@ -32,7 +38,7 @@
3238

3339
% Load skull stripping masks into MATLAB workspace
3440
if applySkullStripping
35-
allFiles = dir(strcat(srcFolder,'\*\*_analysis_mask.nii.gz'));
41+
allFiles = dir(fullfile(srcPath,'*','*_analysis_mask.nii.gz'));
3642
fileDir = fullfile({allFiles.folder},{allFiles.name});
3743

3844
skullMaskDir_3to5 = fileDir(1:65);
@@ -48,38 +54,41 @@
4854
skullMask_Adults = [];
4955
end
5056

51-
% Extract, normalize, and augment 2D image sets. Return exemplar data i
52-
%fileRoot = 'MidSlice_ImageSet\Preprocessed';
57+
% Call the helper function prepare2DImageDataset_ for the set of
58+
% participant folders in each age group. Obtain an exemplar image for each
59+
% age group. If a destination folder is supplied, this helper extracts and
60+
% normalizes 2D image sets, with skull-stripping and augmentation
61+
% optionally applied.
5362
exemplars = cell(3,1);
54-
exemplars{1} = prepare2DImageDataset_(brainVolumes_3to5,fullfile_(dstFolder,'\Ages3-5\'),augmentDataset,applySkullStripping, skullMask_3to5);
55-
exemplars{2} = prepare2DImageDataset_(brainVolumes_7to12,fullfile_(dstFolder,'\Ages7-12\'),augmentDataset,applySkullStripping, skullMask_7to12);
56-
exemplars{3} = prepare2DImageDataset_(brainVolumes_Adults,fullfile_(dstFolder,'\Adults\'),augmentDataset,applySkullStripping, skullMask_Adults);
63+
exemplars{1} = prepare2DImageDataset_(brainVolumes_3to5,fullfile_(dstPath,'\Ages3-5\'),augmentDataset,applySkullStripping, skullMask_3to5);
64+
exemplars{2} = prepare2DImageDataset_(brainVolumes_7to12,fullfile_(dstPath,'\Ages7-12\'),augmentDataset,applySkullStripping, skullMask_7to12);
65+
exemplars{3} = prepare2DImageDataset_(brainVolumes_Adults,fullfile_(dstPath,'\Adults\'),augmentDataset,applySkullStripping, skullMask_Adults);
5766

5867

5968
end
6069

61-
62-
% prepare2DImageDataset is a function that takes in the MRI image volume data for each label. It extracts the axial midslice of each MRI scan volume, applies normalization and other optional processing (skull stripping, augmentation), and saves the reduced 2D image dataset to a specified folder tree organized by label for downstream training, validation, and testing.
63-
% The function's processing options can be used to:
64-
% strip the skull from the 2D images (imType set to 'strip')
65-
% augment the dataset by saving added copies of each 2D image flipped by 180 degrees (imModify set to true)
66-
function exemplar = prepare2DImageDataset_(srcData,dstFolder,applyAugmentation,applySkullStripping,skullStrippingMask)
70+
% Helper function prepare2DImageDataset_ reads from a set (cell array) of
71+
% source folders containing participant 3D volume data. It computes 2D
72+
% image extraction, normalization, and optional processing as described
73+
% above. It returns a single 2D image exemplar and writes the computed 2D
74+
% image files to a destination folder (if specified).
75+
function exemplar = prepare2DImageDataset_(srcFolders,dstFolder,applyAugmentation,applySkullStripping,skullStrippingMask)
6776

6877
if ~isempty(dstFolder)
6978
assert(~exist(dstFolder,'dir'));
7079
mkdir(dstFolder);
7180
end
7281

73-
[~, ~, k, ~] = size(srcData{1});
82+
[~, ~, k, ~] = size(srcFolders{1});
7483

7584
% Extract axial mid-slice from each image volume
76-
mid_slices = cellfun(@squeeze,cellfun(@double,cellfun(@(x)x(:,:,round(k/2),1),srcData,'un',0),'UniformOutput',false),'UniformOutput',false);
85+
mid_slices = cellfun(@squeeze,cellfun(@double,cellfun(@(x)x(:,:,round(k/2),1),srcFolders,'un',0),'UniformOutput',false),'UniformOutput',false);
7786

7887
% Include data from the preprocessed image that does not include the skull
7988
% This section only runs if you wish to 'strip' away the skull
8089
if applySkullStripping
8190
nii_strip_read = skullStrippingMask;
82-
mid_slices_unstrip = cellfun(@squeeze,cellfun(@double,cellfun(@(x)x(:,:,round(k/2),1),srcData,'un',0),'UniformOutput',false),'UniformOutput',false);
91+
mid_slices_unstrip = cellfun(@squeeze,cellfun(@double,cellfun(@(x)x(:,:,round(k/2),1),srcFolders,'un',0),'UniformOutput',false),'UniformOutput',false);
8392
mid_slices_strip = cellfun(@squeeze,cellfun(@double,cellfun(@(x)x(:,:,round(k/2),1),nii_strip_read,'un',0),'UniformOutput',false),'UniformOutput',false);
8493
mid_slices = cellfun(@immultiply,mid_slices_strip,mid_slices_unstrip,'UniformOutput',false);
8594
end
@@ -109,8 +118,8 @@
109118
fileName = sprintf('image_%03d.png', i);
110119
outImg = histeq(convert2img{i},avg_intensity);
111120

112-
if ~isempty(dstFolder)
113-
imwrite(outImg,strcat(dstFolder,fileName),'mode','lossless');
121+
if ~isempty(dstFolder) %
122+
imwrite(outImg,fullfile(dstFolder,fileName),'mode','lossless');
114123
end
115124

116125
if i == 1
@@ -122,7 +131,7 @@
122131
outImg_2 = histeq(convert2img_2{i},avg_intensity_2);
123132

124133
if ~isempty(dstFolder)
125-
imwrite(outImg_2, strcat(dstFolder,fileName_2),'mode','lossless');
134+
imwrite(outImg_2, fullfile(dstFolder,fileName_2),'mode','lossless');
126135
end
127136
end
128137
end
@@ -137,4 +146,4 @@
137146
end
138147
end
139148

140-
% Copyright 2020 The MathWorks, Inc.
149+
% Copyright 2020-2021 The MathWorks, Inc.

0 commit comments

Comments
 (0)