GettingaRegionsCentreOfMass - MRC CBU Imaging Wiki

Upload page content

You can upload content for the page named below. If you change the page name, you can also upload content for another page. If the page name is empty, we derive the page name from the file name.

File to load page content from
Page name
Comment
Flind the wroneg tetters tin eaech wrord

location: GettingaRegionsCentreOfMass

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)