0001 function [nmask] = prt_utils_update_mask(files,mask,dir)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 if nargin<1
0021 files=spm_select([1 Inf],'image','Select files to be analyzed in PRoNTo');
0022 end
0023
0024 if nargin<2
0025 mask = spm_select(1,'image','Select maks image to be updated');
0026 end
0027
0028 if nargin<3
0029 dir = uigetdir();
0030 end
0031
0032
0033 try
0034 M = nifti(mask);
0035 catch
0036 error('prt_utils_update_mask:CouldNotLoadFile',...
0037 'Could not load mask file');
0038 end
0039
0040
0041
0042 try
0043 N = spm_vol(files(1,:));
0044 catch
0045 error('prt_utils_update_mask:CouldNotLoadFile',...
0046 'Could not load first image file');
0047 end
0048
0049 if N.dim(3)==1, Npdim = N.dim(1:2); else Npdim = N.dim; end
0050 if any(size(M.dat(:,:,:,1)) ~= Npdim)
0051 warning('prt_utils_update_mask:maskAndImagesDifferentDim',...
0052 'Mask has different dimensions to the image files. Resizing...');
0053
0054 V2 = spm_vol(char(mask));
0055
0056 fl_res = struct('mean',false,'interp',0,'which',1,'prefix','tmp_');
0057 spm_reslice([N V2],fl_res)
0058
0059 [V2_pth,V2_fn,V2_ext] = spm_fileparts(V2.fname);
0060 rV2_fn = [fl_res.prefix,V2_fn];
0061 if strcmp(V2_ext,'.nii')
0062
0063 V_in = spm_vol(fullfile(V2_pth,[rV2_fn,'.nii']));
0064 V_out = V_in; V_out.fname = fullfile(V2_pth,[rV2_fn,'.img']);
0065 spm_imcalc(V_in,V_out,'i1');
0066 end
0067 mfile_new = ['resized_',V2_fn];
0068 movefile(fullfile(V2_pth,[rV2_fn,'.img']), ...
0069 fullfile(dir,[mfile_new,'.img']));
0070 movefile(fullfile(V2_pth,[rV2_fn,'.hdr']), ...
0071 fullfile(dir,[mfile_new,'.hdr']));
0072 mask = fullfile(dir,[mfile_new,'.img']);
0073 end
0074 V2 = spm_vol(mask);
0075 M = spm_read_vols(V2);
0076
0077
0078
0079 uNan = [];
0080 for i=1:size(files,1)
0081 try
0082 N = spm_vol(files(i,:));
0083 catch
0084 error('prt_utils_update_mask:CouldNotLoadFile',...
0085 ['Could not load image file ',num2str(i)]);
0086 end
0087 voxval = spm_read_vols(N);
0088 voxval = voxval(:);
0089 vNan = find(isnan(voxval));
0090 if ~isempty(vNan)
0091 uNan = union(uNan,vNan);
0092 end
0093 end
0094
0095 if ~isempty(uNan)
0096 M(uNan) = 0;
0097 end
0098
0099 [V2_pth,V2_fn,V2_ext] = spm_fileparts(V2.fname);
0100 mfile_new = ['updated_mask_',V2_fn];
0101 nmask = V2;
0102 nmask.fname = fullfile(dir,[mfile_new,V2_ext]);
0103 nmask = spm_write_vol(nmask,M);
0104