function preprocessLungTSdataset('finallyproccessed','Task06_Lung');
volLoc = fullfile('Task06_Lung', 'imagesTr');
lblLoc = fullfile('Task06_Lung','labelsTr');
if ~exist(volLoc,'dir') || ~exist(lblLoc,'dir')
error(['Please unzip Task06_Lung file to Task06_Lung'])
moveHiddenFiles(Task06_Lung,volLoc,lblLoc);
if ~exist(finallyproccessed,'dir') || proceedWithPreprocessing('finallyproccessed')
mkdir(fullfile('finallyproccessed','imagesTr'));
mkdir(fullfile('finallyproccessed','labelsTr'));
mkdir(fullfile('finallyproccessed','imagesVal'));
mkdir(fullfile('finallyproccessed','labelsVal'));
mkdir(fullfile('finallyproccessed','imagesTest'));
mkdir(fullfile('finallyproccessed','labelsTest'));
labelReader = @(x) (niftiread(x) > 0);
volReader = @(x) niftiread(x);
volds = imageDatastore(volLoc, ...
'FileExtensions','.gz','ReadFcn',volReader);
classNames = ["background","tumor"];
pxds = pixelLabelDatastore(lblLoc,classNames, pixelLabelID, ...
'FileExtensions','.gz','ReadFcn',labelReader);
NumFiles = length(pxds.Files);
outL = readNumeric(pxds);
reg = regionprops3(temp,'BoundingBox');
ROI = ceil(reg.BoundingBox(1,:));
ROIend = ROI(1:3) + ROI(4:6) + tol;
ROIend(ROIend>sz)=sz(ROIend>sz);
tumorRows = ROIst(2):ROIend(2);
tumorCols = ROIst(1):ROIend(1);
tumorPlanes = ROIst(3):ROIend(3);
tcropVol = outV(tumorRows,tumorCols, tumorPlanes,:);
tcropLabel = outL(tumorRows,tumorCols, tumorPlanes);
ind = floor(size(tcropVol)/8)*8;
incropVol = tcropVol(1:ind(1),1:ind(2),1:ind(3),:);
cropVol = channelWisePreProcess(incropVol);
cropLabel = tcropLabel(1:ind(1),1:ind(2),1:ind(3));
if (id < floor(0.83*NumFiles))
imDir = fullfile('finallyproccessed','imagesTr','LungTS');
labelDir = fullfile('finallyproccessed','labelsTr','LungTS');
elseif (id < floor(0.89*NumFiles))
imDir = fullfile('finallyproccessed','imagesVal','LungTS');
labelDir = fullfile('finallyproccessed','labelsVal','LungTS');
imDir = fullfile('finallyproccessed','imagesTest','LungTS');
labelDir = fullfile('finallyproccessed','labelsTest','LungTS');
save([imDir num2str(id,'%.3d') '.mat'],'cropVol');
save([labelDir num2str(id,'%.3d') '.mat'],'cropLabel');
function out = channelWisePreProcess(in)
chn_Mean = mean(in,[1 2 3]);
chn_Std = std(in,0,[1 2 3]);
out = (in - chn_Mean)./chn_Std;
out(out > rangeMax) = rangeMax;
out(out < rangeMin) = rangeMin;
out = (out - rangeMin) / (rangeMax - rangeMin);
function moveHiddenFiles('Task06_Lung',volLoc,lblLoc)
hiddenDir = fullfile('Task06_Lung','HiddenFiles');
if ~exist(hiddenDir,'dir')
!move ._* ../HiddenFiles/
!move ._* ../HiddenFiles/
function out = proceedWithPreprocessing('finallyproccessed')
if exist(fullfile('finallyproccessed','imagesTr'),'dir')
tmp1 = dir(fullfile('finallyproccessed','imagesTr'));
numFiles = numFiles + sum(~vertcat(tmp1.isdir));
if exist(fullfile('finallyproccessed','imagesVal'),'dir')
tmp1 = dir(fullfile('finallyproccessed','imagesVal'));
numFiles = numFiles + sum(~vertcat(tmp1.isdir));
if exist(fullfile('finallyproccessed','imagesTest'),'dir')
tmp1 = dir(fullfile('finallyproccessed','imagesTest'));
numFiles = numFiles + sum(~vertcat(tmp1.isdir));
out = (numFiles ~= totalNumFiles);