Diff for "GettingaRegionsCentreOfMass" - MRC CBU Imaging Wiki
location: Diff for "GettingaRegionsCentreOfMass"
Differences between revisions 1 and 2
Revision 1 as of 2009-10-27 16:47:26
Size: 990
Editor: JohanCarlin
Comment:
Revision 2 as of 2013-03-07 21:23:04
Size: 990
Editor: localhost
Comment: converted to 1.6 markup
No differences found!

Getting the centre of mass for an activation

Occasionally it is useful to get a blob's centre of mass, for instance in a binary mask where peaks are not available. This function presents a simplistic solution.

% Returns the centre of mass for blob(s) in a volume. Input either
% handle from spm_vol, or path to volume. If you aren't happy with
% the way the function divides the volume into blobs, you can mask
% off the blobs you like manually and save each blob to its own
% volume.
% Use: CoM = getCentreOfMass(vol)
% 27/10/2009 J Carlin, with thanks to Danny Mitchell and Russell
% Thompson

function CoM = getCentreOfMass(vol)

if isstr(vol)
    vol = spm_vol(vol);
end

v = spm_read_vols(vol);

v(isnan(v)) = 0;

% Identify and number code blobs
blobs = bwlabeln(v);

for i = 1:max(blobs(:))
    % Blob indices
    [x,y,z] = ind2sub(size(blobs),find(blobs == i));
    XYZ = [x, y, z];
    CoM(i,:) = mean(XYZ,1);
end

(JohanCarlin)

CbuImaging: GettingaRegionsCentreOfMass (last edited 2013-03-07 21:23:04 by localhost)