View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Center of Mass

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

Center of Mass



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

| Watch this File

File Information

    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.
        A = rgb2gray(imread('saturn.png'));
        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)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (4)
07 Jan 2016 Trevor Agus

24 Jan 2015 Shaiq Ghorwal

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 = rgb2gray(imread('image.png'));
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

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

Contact us