Code covered by the BSD License

### Highlights from Center of Mass

5.0
5.0 | 4 ratings Rate this file 47 Downloads (last 30 days) File Size: 1.92 KB File ID: #41675 Version: 1.0

# Center of Mass

### Jered Wells (view profile)

CENTEROFMASS finds the center of mass of the N-dimensional input array

File Information
Description

centerOfMass(A) finds the gray-level-weighted center of mass of the
N-dimensional numerical array A. A must be real and finite. A warning
is issued if A contains any negative values. Any NaN elements of A will
automatically be ignored. centerOfMass produces center of mass
coordinates in units of pixels. An empty array is returned if the
center of mass is undefined.

The center of mass is reported under the assumption that the first
pixel in each array dimension is centered at 1.

Also note that numerical arrays other than DOUBLE and SINGLE are
converted to SINGLE in order to prevent numerical roundoff error.

Examples:
C = centerOfMass(A);

figure; imagesc(A); colormap gray; axis image
hold on; plot(C(2),C(1),'rx')

Required Products MATLAB
MATLAB release MATLAB 8.1 (R2013a)
07 Jan 2016 Trevor Agus

### Trevor Agus (view profile)

24 Jan 2015 Shaiq Ghorwal

### Shaiq Ghorwal (view profile)

Thank you for this useful script.

I've figured out that by doing a few manipulations then the script is also able to find the geometric center of mass of a 3D HOMOGENEOUS object, assuming it is SYMMETRIC around any of the 3 dimensions: x, y or z.

Say the image file is called image.png, and EVERYTHING in the image, apart from the object of interest, is totally bright, meaning it has pixel value 255, then here is what you need to do:

A(A==255) = NaN;
color = 50;
A(A>0) = color;

C = centerOfMass(A);

% the value of color can be in principle
anything between 0 and 255. If it is close to 50, then you are able to see the image..

23 May 2014 Kahye

### Kahye (view profile)

19 Jan 2014 Lena Veis

### Lena Veis (view profile)

You save me a lot of time...
Thank you very much :)