<?xml version="1.0" encoding="utf-8"?><!DOCTYPE article  PUBLIC '-//OASIS//DTD DocBook XML V4.4//EN'  'http://www.docbook.org/xml/4.4/docbookx.dtd'><article><articleinfo><title>GettingaRegionsCentreOfMass</title><revhistory><revision><revnumber>2</revnumber><date>2013-03-07 21:23:04</date><authorinitials>localhost</authorinitials><revremark>converted to 1.6 markup</revremark></revision><revision><revnumber>1</revnumber><date>2009-10-27 16:47:26</date><authorinitials>JohanCarlin</authorinitials></revision></revhistory></articleinfo><section><title>Getting the centre of mass for an activation</title><para>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. </para><screen><![CDATA[% 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
]]><![CDATA[
function CoM = getCentreOfMass(vol)
]]><![CDATA[
if isstr(vol)
    vol = spm_vol(vol);
end
]]><![CDATA[
v = spm_read_vols(vol);
]]><![CDATA[
v(isnan(v)) = 0;
]]><![CDATA[
% Identify and number code blobs
blobs = bwlabeln(v);
]]><![CDATA[
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]]></screen><para>(<ulink url="https://imaging.mrc-cbu.cam.ac.uk/imaging/GettingaRegionsCentreOfMass/imaging/JohanCarlin#">JohanCarlin</ulink>) </para></section></article>