spm_mean.m 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. function spm_mean(P)
  2. % Compute a mean image from a set
  3. % FORMAT spm_mean(P)
  4. % P - list of images to average [Default: GUI]
  5. %__________________________________________________________________________
  6. %
  7. % spm_mean_ui simply averages a set of images to produce a mean image
  8. % that is written as type int16 to "mean.img" (in the current directory).
  9. %
  10. % The images must have the same dimensions, orientations and the same
  11. % voxel sizes.
  12. %
  13. % This is not a "softmean" - zero voxels are treated as zero.
  14. %__________________________________________________________________________
  15. % Copyright (C) 1998-2014 Wellcome Trust Centre for Neuroimaging
  16. % John Ashburner, Andrew Holmes
  17. % $Id: spm_mean.m 6081 2014-07-01 18:19:42Z guillaume $
  18. persistent runonce
  19. if isempty(runonce)
  20. warning('ImCalc should be preferred to spm_mean.');
  21. runonce = 1;
  22. end
  23. SVNid = '$Rev: 6081 $';
  24. %-Say hello
  25. %--------------------------------------------------------------------------
  26. SPMid = spm('FnBanner',mfilename,SVNid);
  27. %-Select images & check dimensions, orientations and voxel sizes
  28. %--------------------------------------------------------------------------
  29. if ~nargin
  30. [P, sts] = spm_select([2 Inf],'image','Select images to be averaged');
  31. if ~sts, return; end
  32. end
  33. Vi = spm_vol(P);
  34. spm_check_orientations(Vi);
  35. %-Compute mean and write image
  36. %--------------------------------------------------------------------------
  37. Vo = struct('fname', ['mean' spm_file_ext],...
  38. 'dim', Vi(1).dim(1:3),...
  39. 'dt', [spm_type('int16') spm_platform('bigend')],...
  40. 'mat', Vi(1).mat,...
  41. 'pinfo', [1 0 0]',...
  42. 'descrip', 'spm - mean image');
  43. %-Adjust scalefactors by 1/n to effect mean by summing
  44. n = numel(Vi);
  45. for i=1:n
  46. Vi(i).pinfo(1:2,:) = Vi(i).pinfo(1:2,:)/n;
  47. end
  48. Vo = spm_create_vol(Vo);
  49. Vo.pinfo(1,1) = spm_add(Vi,Vo);
  50. Vo = spm_create_vol(Vo);
  51. %-End - report back
  52. %--------------------------------------------------------------------------
  53. fprintf('Mean image written to file ''%s'' in current directory\n',Vo.fname);