|
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 | +% |
3 | 4 | % OUTPUTS
|
4 | 5 | % 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 | +% |
5 | 11 |
|
6 | 12 | 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 |
9 | 15 | augmentDataset (1,1) logical = false % specify whether to apply offline data augmentation (a copy of image flipped 180 degrees).
|
10 | 16 | applySkullStripping (1,1) logical = false % specify whether to apply skull-stripping
|
11 | 17 | end
|
12 | 18 |
|
13 | 19 | % 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); |
17 | 23 | end
|
18 | 24 |
|
19 | 25 | % Load 3D image volumes into MATLAB workspace
|
20 |
| -allFiles = dir(strcat(srcFolder,'\*\*_anat.nii.gz')); |
| 26 | +allFiles = dir(fullfile(srcPath,'*','*_anat.nii.gz')); |
21 | 27 |
|
22 | 28 | fileDir = fullfile({allFiles.folder},{allFiles.name});
|
23 | 29 |
|
|
32 | 38 |
|
33 | 39 | % Load skull stripping masks into MATLAB workspace
|
34 | 40 | if applySkullStripping
|
35 |
| - allFiles = dir(strcat(srcFolder,'\*\*_analysis_mask.nii.gz')); |
| 41 | + allFiles = dir(fullfile(srcPath,'*','*_analysis_mask.nii.gz')); |
36 | 42 | fileDir = fullfile({allFiles.folder},{allFiles.name});
|
37 | 43 |
|
38 | 44 | skullMaskDir_3to5 = fileDir(1:65);
|
|
48 | 54 | skullMask_Adults = [];
|
49 | 55 | end
|
50 | 56 |
|
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. |
53 | 62 | 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); |
57 | 66 |
|
58 | 67 |
|
59 | 68 | end
|
60 | 69 |
|
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) |
67 | 76 |
|
68 | 77 | if ~isempty(dstFolder)
|
69 | 78 | assert(~exist(dstFolder,'dir'));
|
70 | 79 | mkdir(dstFolder);
|
71 | 80 | end
|
72 | 81 |
|
73 |
| -[~, ~, k, ~] = size(srcData{1}); |
| 82 | +[~, ~, k, ~] = size(srcFolders{1}); |
74 | 83 |
|
75 | 84 | % 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); |
77 | 86 |
|
78 | 87 | % Include data from the preprocessed image that does not include the skull
|
79 | 88 | % This section only runs if you wish to 'strip' away the skull
|
80 | 89 | if applySkullStripping
|
81 | 90 | 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); |
83 | 92 | mid_slices_strip = cellfun(@squeeze,cellfun(@double,cellfun(@(x)x(:,:,round(k/2),1),nii_strip_read,'un',0),'UniformOutput',false),'UniformOutput',false);
|
84 | 93 | mid_slices = cellfun(@immultiply,mid_slices_strip,mid_slices_unstrip,'UniformOutput',false);
|
85 | 94 | end
|
|
109 | 118 | fileName = sprintf('image_%03d.png', i);
|
110 | 119 | outImg = histeq(convert2img{i},avg_intensity);
|
111 | 120 |
|
112 |
| - if ~isempty(dstFolder) |
113 |
| - imwrite(outImg,strcat(dstFolder,fileName),'mode','lossless'); |
| 121 | + if ~isempty(dstFolder) % |
| 122 | + imwrite(outImg,fullfile(dstFolder,fileName),'mode','lossless'); |
114 | 123 | end
|
115 | 124 |
|
116 | 125 | if i == 1
|
|
122 | 131 | outImg_2 = histeq(convert2img_2{i},avg_intensity_2);
|
123 | 132 |
|
124 | 133 | if ~isempty(dstFolder)
|
125 |
| - imwrite(outImg_2, strcat(dstFolder,fileName_2),'mode','lossless'); |
| 134 | + imwrite(outImg_2, fullfile(dstFolder,fileName_2),'mode','lossless'); |
126 | 135 | end
|
127 | 136 | end
|
128 | 137 | end
|
|
137 | 146 | end
|
138 | 147 | end
|
139 | 148 |
|
140 |
| -% Copyright 2020 The MathWorks, Inc. |
| 149 | +% Copyright 2020-2021 The MathWorks, Inc. |
0 commit comments