File size: 4,854 Bytes
72417f2 | 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | % script to run SAMSEG on the MRIs
% For each volume:
% First, it tries to register the SAMSEG atlas to the ex vivo MRI, and
% shows you the result with freeview. If you like it, then it uses this
% registration to initilize SAMSEG. If you don't like it, then it asks you
% to register the template directly
% After that, you need to use a modified version of SAMSEG as in
% SegmentPhotosWithSAMSEG, but without switching off the bias field
% correction. I know, this is superugly. This is why you should totally
% come up with something better, e.g., by adding personalized flags to SAMSEG,
% to a) bypass bias field correction and b) skipping affine registration
% and using a provided affinely registered atlas directly.
% In addition, I must say that the automatic registration almost never
% works, and you need to manually align in most cases. This is bad, we need
% something better and more reproducible. I was thinking that a possibility
% would be to kill the extracerebral tissue from the SAMSEG tamplate
% (Nellie can help you with that; she would know what semiautomated tools
% to use, and you don't need anything superperfect)
clear
inputDir='/autofs/cluster/vive/UW_photo_recon/FLAIR_Scan_Data/';
outputDir='/autofs/cluster/vive/UW_photo_recon/FLAIR_Scan_Data/SAMSEG/';
EXVIVO_SAMSEG_FS_DIR='/autofs/space/panamint_005/users/iglesias/fsdevsamsegexvivo/';
ALADIN='/autofs/space/panamint_005/users/iglesias/software/niftyreg-kcl/build/reg-apps/reg_aladin -ln 3 -lp 2 -speeeeed ';
nthread=16;
cases={'17-0333', % no missing photos in these ones
'18-1045',
'18-1132',
'18-1196',
'18-1680',
'18-1913',
'18-1930',
'18-2128',
'18-2259'};
if exist(outputDir,'dir')==0
mkdir(outputDir);
end
% pass 1: register atlas and prepare SAMSEG dirs
T=MRIread([EXVIVO_SAMSEG_FS_DIR '/average/samseg/20Subjects_smoothing2_down2_smoothingForAffine2/template.orig.nii']);
atlasDir=[EXVIVO_SAMSEG_FS_DIR '/average/samseg/20Subjects_smoothing2_down2_smoothingForAffine2/'];
template=[tempdir '/template.nii.gz'];
MRIwrite(T,template);
T=MRIread(template);
for c=1:length(cases)
disp(['Preparing grounds for case ' num2str(c) ' of ' num2str(length(cases))]);
aux=cases{c};
aux(aux=='-')='_';
inputScan=[inputDir '/NP' aux '.rotated.nii.gz'];
odir=[outputDir '/' cases{c} '/'];
if exist(odir,'dir')==0
mkdir(odir);
end
adir=[odir '/atlas/'];
if exist(adir,'dir')==0
mkdir(adir);
end
if exist([adir '/template.mgz'],'file')==0
cmd=[ALADIN ' -ref ' inputScan ' -flo ' template ' -res /tmp/res.nii.gz -aff /tmp/aff.txt' ];
system([cmd ' >/dev/null']);
system(['freeview ' inputScan ' /tmp/res.nii.gz']);
a=input('Do you like the output? ');
if a>0
fid = fopen('/tmp/aff.txt');
M = fscanf(fid,'%f',[4,4])';
fclose(fid);
TT=[];
TT.vol=T.vol;
TT.vox2ras0=inv(M)*T.vox2ras0;
TT.volres=sum(sqrt(TT.vox2ras0(1:3,1:3).^2));
MRIwrite(TT,'/tmp/template.mgz');
else
system(['mri_convert ' template ' /tmp/template.mgz >/dev/null']);
disp(['Align in freeview and save using only header']);
system(['freeview ' inputScan ' /tmp/template.mgz']);
end
movefile('/tmp/template.mgz',adir);
end
% copy files to atlas directory
copyfile([atlasDir '/atlas_level1.txt.gz' ],adir);
copyfile([atlasDir '/atlas_level2.txt.gz' ],adir);
copyfile([atlasDir '/compressionLookupTable.txt' ],adir);
copyfile([atlasDir '/modifiedFreeSurferColorLUT.txt' ],adir);
if exist([adir '/sharedGMMParameters.txt'],'file')==0
a=input('Does the case have cerebellum and brainstem? ');
if a > 0
copyfile('./data/sharedGMMParameters.exvivoMRI.txt',[adir '/sharedGMMParameters.txt']);
else
copyfile('./data/sharedGMMParameters.NoBSorCB.exvivoMRI.txt',[adir '/sharedGMMParameters.txt']);
end
end
% samseg call
dd=dir([odir '/*crispSegmentation.nii']);
if length(dd)==0
cmd=['setenv FREESURFER_HOME ' EXVIVO_SAMSEG_FS_DIR ' && source $FREESURFER_HOME/SetUpFreeSurfer.csh && samseg '];
cmd=[cmd ' --i ' inputScan];
cmd=[cmd ' --o ' odir];
cmd=[cmd ' --threads ' num2str(nthread)];
cmd=[cmd ' --ssdd ' adir];
cmd=[cmd ' --no-save-warp'];
disp(cmd);
else
cmd=[];
end
cmds{c}=cmd;
end
scriptName=[pwd() '/script.sh'];
fid = fopen(scriptName,'w');
for j=1:length(cmds)
if ~isempty(cmds{j})
fprintf(fid,'%s\n',cmds{j});
end
end
fclose(fid);
system(['chmod u+x ' scriptName]);
edit(scriptName);
disp(scriptName);
|