| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function SegmentPhotosWithSAMSEG(inputVol,inputVolMask,inputWarpedRef,outputDir,nthread) |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| ROB_REG='/usr/local/freesurfer/stable6_0_0/bin/mri_robust_register'; |
| EXVIVO_SAMSEG_FS_DIR='/autofs/space/panamint_005/users/iglesias/fsdevsamsegexvivo/'; |
|
|
| |
| BG_NOISE_MAX=3; |
| SAMSEG_CHANNELS=1; |
|
|
| |
|
|
| if nargin < 5 |
| nthread = 4; |
| end |
|
|
| |
|
|
| addpath functions |
|
|
| |
| if exist(outputDir,'dir')==0 |
| mkdir(outputDir); |
| end |
|
|
| tempdir=[outputDir '/temp/']; |
| if exist(tempdir,'dir')==0 |
| mkdir(tempdir); |
| end |
|
|
| disp('Reading in input volume and matching histogram of photos ...'); |
| mri=MRIread(inputVol); |
| mriMask=MRIread(inputVolMask); |
| mri.vol(repmat(mriMask.vol<.5,[1 1 1 3]))=0; |
| mriCorr=mri; |
| mid=round(size(mri.vol,3)/2); |
| for c=1:3 |
| REF=uint8(mri.vol(:,:,mid,c)); |
| REF(mriMask.vol(:,:,mid)<0.5)=0; |
| for z=[1:mid-1 mid+1:size(mri.vol,3)] |
| I=uint8(mri.vol(:,:,z,c)); |
| I(mriMask.vol(:,:,z)<.5)=0; |
| Imatched = matchHistoMasked(I, REF); |
| mriCorr.vol(:,:,z,c)=Imatched; |
| end |
| end |
| MRIwrite(mriCorr,[outputDir '/corrected.nii.gz']); |
|
|
|
|
| disp('Preparing input scans...'); |
| inputs=[]; |
| if SAMSEG_CHANNELS==1 |
| mri.vol=mean(mriCorr.vol,4); |
| aux=mri.vol(mri.vol==0); aux=aux+BG_NOISE_MAX*rand(size(aux)); mri.vol(mri.vol==0)=aux; |
| inputs{1}=[outputDir '/corrected_gray.nii.gz']; |
| MRIwrite(mri,inputs{1}); |
| |
| elseif SAMSEG_CHANNELS==2 |
| X=zeros([numel(mriCorr.vol)/3 3]); |
| for c=1:3 |
| aux=mriCorr.vol(:,:,:,c); |
| X(:,c)=aux(:); |
| end |
| C=cov(X); |
| [V,D]=eig(C); |
| [~,idx]=sort(diag(D),'descend'); |
| idx=idx(1:2); |
| P=V(:,idx); |
| mu=mean(X)'; |
| b=P'*(X'-repmat(mu,[1 size(X,1)])); |
| b=b+0.1*randn(size(b)); |
| |
| aux=mriCorr; |
| aux.vol=aux.vol(:,:,:,1); |
| aux.vol(:)=b(1,:); |
| aux.vol=aux.vol-min(aux.vol(:))+1e-3; |
| inputs{1}=[outputDir '/corrected_c1.nii.gz']; |
| MRIwrite(aux,inputs{1}); |
| |
| aux=mriCorr; |
| aux.vol=aux.vol(:,:,:,1); |
| aux.vol(:)=b(2,:); |
| aux.vol=aux.vol-min(aux.vol(:))+1e-3; |
| inputs{2}=[outputDir '/corrected_c2.nii.gz']; |
| MRIwrite(aux,inputs{2}); |
| |
| else % run on RGB |
| mri.vol=mriCorr.vol(:,:,:,1); |
| aux=mri.vol(mri.vol==0); aux=aux+BG_NOISE_MAX*rand(size(aux)); mri.vol(mri.vol==0)=aux; |
| inputs{1}=[outputDir '/corrected_red.nii.gz']; |
| MRIwrite(mri,inputs{1}); |
| mri.vol=mriCorr.vol(:,:,:,2); |
| aux=mri.vol(mri.vol==0); aux=aux+BG_NOISE_MAX*rand(size(aux)); mri.vol(mri.vol==0)=aux; |
| inputs{2}=[outputDir '/corrected_green.nii.gz']; |
| MRIwrite(mri,inputs{2}); |
| mri.vol=mriCorr.vol(:,:,:,3); |
| aux=mri.vol(mri.vol==0); aux=aux+BG_NOISE_MAX*rand(size(aux)); mri.vol(mri.vol==0)=aux; |
| inputs{3}=[outputDir '/corrected_blue.nii.gz']; |
| MRIwrite(mri,inputs{3}); |
| end |
| |
| |
| disp('Initializing affine registration'); |
| cmd=[ROB_REG ' --leastsquares --dst ' inputWarpedRef ' --mov ./data/template.nii --nosym --mapmovhdr ' tempdir '/rigid.mgz --lta ' tempdir '/rigid.lta']; |
| a=system([cmd ' >/dev/null']); |
| if a, error('Error in rigid registration of template'); end |
| cmd=[ROB_REG ' --leastsquares --affine --dst ' inputWarpedRef ' --mov ' tempdir '/rigid.mgz --nosym --mapmovhdr ' tempdir '/template.mgz --lta ' tempdir '/affine.lta']; |
| a=system([cmd ' >/dev/null']); |
| if a, error('Error in affine registration of template'); end |
| |
| % copy rest of atlas files |
| atlasDir=[EXVIVO_SAMSEG_FS_DIR '/average/samseg/20Subjects_smoothing2_down2_smoothingForAffine2/']; |
| copyfile([atlasDir '/atlas_level1.txt.gz' ],tempdir); |
| copyfile([atlasDir '/atlas_level2.txt.gz' ],tempdir); |
| copyfile([atlasDir '/compressionLookupTable.txt' ],tempdir); |
| copyfile([atlasDir '/modifiedFreeSurferColorLUT.txt' ],tempdir); |
| copyfile('./data/sharedGMMParameters.NoBSorCB.photo.txt',[tempdir '/sharedGMMParameters.txt']); |
| |
| % samseg call |
| disp('Running samseg') |
| cmd=['setenv FREESURFER_HOME ' EXVIVO_SAMSEG_FS_DIR ' && source $FREESURFER_HOME/SetUpFreeSurfer.csh && samseg ']; |
| for j=1:length(inputs) |
| cmd=[cmd ' --i ' inputs{j}]; |
| end |
| cmd=[cmd ' --o ' outputDir]; |
| cmd=[cmd ' --threads ' num2str(nthread)]; |
| cmd=[cmd ' --ssdd ' tempdir]; |
| cmd=[cmd ' --save-posteriors']; |
| cmd=[cmd ' --no-save-warp']; |
| disp(cmd); |
| system(cmd); |
| |
| disp('All done! Cleaning up...'); |
| system(['rm -rf ' tempdir]); |
| |
| |
| |
| |
| |
| |
| |
| |