spm_ROI.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. function [xY, XYZmm, j] = spm_ROI(xY, XYZmm)
  2. % Region of Interest specification
  3. % FORMAT xY = spm_ROI(xY)
  4. % xY - VOI structure
  5. % xY.def - VOI definition [sphere, box, mask, cluster, all]
  6. % xY.rej - cell array of disabled VOI definition options
  7. % xY.xyz - centre of VOI {mm}
  8. % xY.spec - VOI definition parameters
  9. % xY.str - description of the VOI
  10. %
  11. % FORMAT [xY, XYZmm, j] = spm_ROI(xY, XYZmm)
  12. % XYZmm - [3xm] locations of voxels {mm}
  13. % If an image filename, an spm_vol structure or a NIfTI object is
  14. % given instead, XYZmm will be initialised to all voxels within
  15. % the field of view of that image.
  16. %
  17. % XYZmm - [3xn] filtered locations of voxels {mm} (m>=n) within VOI xY
  18. % j - [1xn] indices of input locations XYZmm within VOI xY
  19. %__________________________________________________________________________
  20. % Copyright (C) 2008-2014 Wellcome Trust Centre for Neuroimaging
  21. % Karl Friston, Guillaume Flandin
  22. % $Id: spm_ROI.m 6079 2014-06-30 18:25:37Z spm $
  23. if nargin < 2 && nargout > 1
  24. error('Too many output arguments.');
  25. end
  26. try, xY; catch, xY = []; end
  27. %-Specify ROI
  28. %==========================================================================
  29. if ~isfield(xY,'def')
  30. def = {'sphere','box','cluster','mask'};
  31. if isfield(xY,'rej')
  32. if ~isfield(xY,'M')
  33. xY.rej = {xY.rej{:} 'cluster'};
  34. end
  35. else
  36. if isfield(xY,'M')
  37. xY.rej = {};
  38. else
  39. xY.rej = {'cluster'};
  40. end
  41. end
  42. [q, i] = setdiff(def,xY.rej);
  43. def = def(sort(i));
  44. xY.def = spm_input('VOI definition...','!+1','b',def,[],1);
  45. end
  46. %-ROI parameters
  47. %--------------------------------------------------------------------------
  48. switch lower(xY.def)
  49. case 'sphere'
  50. %----------------------------------------------------------------------
  51. if ~isfield(xY,'xyz') || isempty(xY.xyz)
  52. xY.xyz = spm_input('sphere centre [x y z] {mm}',...
  53. '!+0','r','0 0 0',3);
  54. end
  55. if ~isfield(xY,'spec')
  56. xY.spec = spm_input('sphere radius (mm)','!+0','r',0,1,[0,Inf]);
  57. end
  58. xY.str = sprintf('%0.1fmm sphere',xY.spec);
  59. case 'box'
  60. %----------------------------------------------------------------------
  61. if ~isfield(xY,'xyz') || isempty(xY.xyz)
  62. xY.xyz = spm_input('box centre [x y z] {mm}',...
  63. '!+0','r','0 0 0',3);
  64. end
  65. if ~isfield(xY,'spec')
  66. xY.spec = spm_input('box dimensions [x y z] {mm}',...
  67. '!+0','r','0 0 0',3);
  68. end
  69. if length(xY.spec) < 3
  70. xY.spec = xY.spec(1)*[1 1 1];
  71. end
  72. xY.str = sprintf('%0.1f x %0.1f x %0.1f mm box',xY.spec);
  73. case 'mask'
  74. %----------------------------------------------------------------------
  75. if ~isfield(xY,'spec')
  76. xY.spec = spm_vol(spm_select(1,'image','Specify Mask'));
  77. else
  78. if ~isstruct(xY.spec)
  79. xY.spec = spm_vol(xY.spec);
  80. end
  81. end
  82. str = spm_file(xY.spec.fname,'short30');
  83. str = regexprep(str, {'\\' '\^' '_' '{' '}'}, ...
  84. {'\\\\' '\\^' '\\_' '\\{' '\\}'}); % Escape TeX special characters
  85. xY.str = sprintf('image mask: %s',str);
  86. case 'cluster'
  87. %----------------------------------------------------------------------
  88. if ~isfield(xY,'xyz') || isempty(xY.xyz)
  89. xY.xyz = spm_input('seed voxel [x y z] {mm}',...
  90. '!+0','r','0 0 0',3);
  91. end
  92. if ~isfield(xY,'M')
  93. xY.M = spm_input('affine transformation matrix',...
  94. '!+0','r','0 0 0',[4 4]);
  95. end
  96. xY.spec = [];
  97. xY.str = sprintf('cluster (seed voxel: %0.1f %0.1f %0.1f)',xY.xyz);
  98. case 'all'
  99. %----------------------------------------------------------------------
  100. xY.str = 'all';
  101. otherwise
  102. %----------------------------------------------------------------------
  103. error('Unknown VOI type.');
  104. end
  105. if nargin < 2, return; end
  106. %-'Estimate' ROI
  107. %==========================================================================
  108. %-Argument check
  109. %--------------------------------------------------------------------------
  110. if ischar(XYZmm) && isempty(XYZmm)
  111. XYZmm = spm_select(1,'image','Specify Image');
  112. end
  113. if ischar(XYZmm), XYZmm = spm_vol(XYZmm); end
  114. if isa(XYZmm,'nifti')
  115. XYZmm = struct('dim',size(XYZmm.dat), 'mat',XYZmm.mat);
  116. end
  117. if isstruct(XYZmm) % spm_vol
  118. [R,C,P] = ndgrid(1:XYZmm.dim(1),1:XYZmm.dim(2),1:XYZmm.dim(3));
  119. RCP = [R(:)';C(:)';P(:)';ones(1,numel(R))];
  120. XYZmm = XYZmm.mat(1:3,:)*RCP;
  121. clear R C P RCP
  122. end
  123. if isempty(XYZmm), XYZmm = zeros(3,0); end
  124. %-Filter location of voxels
  125. %--------------------------------------------------------------------------
  126. Q = ones(1,size(XYZmm,2));
  127. switch lower(xY.def)
  128. case 'sphere'
  129. %----------------------------------------------------------------------
  130. j = find(sum((XYZmm - xY.xyz*Q).^2) <= xY.spec^2);
  131. case 'box'
  132. %----------------------------------------------------------------------
  133. j = find(all(abs(XYZmm - xY.xyz*Q) <= xY.spec(:)*Q/2));
  134. case 'mask'
  135. %----------------------------------------------------------------------
  136. XYZ = xY.spec.mat \ [XYZmm; Q];
  137. j = find(spm_sample_vol(xY.spec, XYZ(1,:), XYZ(2,:), XYZ(3,:),0) > 0);
  138. case 'cluster'
  139. %----------------------------------------------------------------------
  140. [x, i] = spm_XYZreg('NearestXYZ',xY.xyz,XYZmm);
  141. XYZ = round(xY.M \ [XYZmm; Q]);
  142. A = spm_clusters(XYZ);
  143. j = find(A == A(i));
  144. case 'all'
  145. %----------------------------------------------------------------------
  146. j = 1:size(XYZmm,2);
  147. otherwise
  148. %----------------------------------------------------------------------
  149. error('Unknown VOI type.');
  150. end
  151. XYZmm = XYZmm(:,j);
  152. if strcmpi(xY.def,'mask') && ~isempty(XYZmm), xY.xyz = mean(XYZmm,2); end