spm_clusters.m 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. function A = spm_clusters(L,n)
  2. % Return the cluster index for a point list
  3. % FORMAT A = spm_clusters(L,n)
  4. % L - locations [x y x]' {in voxels} ([3 x m] matrix)
  5. % n - connectivity criterion (see spm_bwlabel) [Default: 18]
  6. %
  7. % A - cluster index or region number ([1 x m] vector)
  8. %__________________________________________________________________________
  9. %
  10. % spm_clusters characterises a point list of voxel values defined with
  11. % their locations (L) in terms of edge, face and vertex connected
  12. % subsets, returning a list of indices in A, such that the ith location
  13. % belongs to cluster A(i) (using an 18 connectivity scheme).
  14. %__________________________________________________________________________
  15. % Copyright (C) 1994-2012 Wellcome Trust Centre for Neuroimaging
  16. % Jesper Andersson
  17. % $Id: spm_clusters.m 4929 2012-09-17 14:21:01Z guillaume $
  18. if isempty(L), A = []; return; end
  19. if nargin < 2, n = 18; end
  20. % Turn location list to binary 3D volume
  21. %--------------------------------------------------------------------------
  22. dim = [max(L(1,:)) max(L(2,:)) max(L(3,:))];
  23. vol = zeros(dim(1),dim(2),dim(3));
  24. indx = sub2ind(dim,L(1,:)',L(2,:)',L(3,:)');
  25. vol(indx) = 1;
  26. % Label each cluster in 3D volume with its own label using an 18
  27. % connectivity criterion
  28. %--------------------------------------------------------------------------
  29. cci = spm_bwlabel(vol,n);
  30. % Map back to list
  31. %--------------------------------------------------------------------------
  32. A = cci(indx);
  33. A = A(:)';