# MATLAB: Center of Mass, metal foam

image analysisImage Processing Toolboxmasscenter

Hi!
I am trying to calculate the center of mass of the black circles of this image, is a section of a metal foam, the black circles are the holes of the (white) cilinder.
Is there some function?
I already calculated centroid position of the black holes and their areas but their not related, i mean i have a column of areas and a column of position but i dont know which areas has (x,y) position etc….,they are mixed up.

clc;    % Clear the command window.close all;  % Close all figures (except those of imtool.)clear;  % Erase all existing variables. Or clearvars if you want.workspace;  % Make sure the workspace panel is showing.format long g;format compact;fontSize = 22;%--------------------------------------------------------------------------------------------------------%    READ IN IMAGEfolder = pwd;baseFileName = 'image.png';% Get the full filename, with path prepended.fullFileName = fullfile(folder, baseFileName);% Check if file exists.if ~exist(fullFileName, 'file')	% The file doesn't exist -- didn't find it there in that folder.	% Check the entire search path (other folders) for the file by stripping off the folder.	fullFileNameOnSearchPath = baseFileName; % No path this time.	if ~exist(fullFileNameOnSearchPath, 'file')		% Still didn't find it.  Alert user.		errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);		uiwait(warndlg(errorMessage));		return;	endendgrayImage = imread(fullFileName);% Get the dimensions of the image.% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.[rows, columns, numberOfColorChannels] = size(grayImage);if numberOfColorChannels > 1	% It's not really gray scale like we expected - it's color.	% Use weighted sum of ALL channels to create a gray scale image.	grayImage = rgb2gray(grayImage);	% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,	% which in a typical snapshot will be the least noisy channel.	% grayImage = grayImage(:, :, 2); % Take green channel.end% Display the image.subplot(2, 2, 1);imshow(grayImage, []);title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');impixelinfo;hFig = gcf;hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.drawnow;%--------------------------------------------------------------------------------------------------------%    EXTRACTION OF IMAGE% Get a binary imagebinaryImage = grayImage < 128;% Get rid of the surround.binaryImage = imclearborder(binaryImage);subplot(2, 2, 2);imshow(grayImage, []);impixelinfo;title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');impixelinfo;% Fill holesbinaryImage = imfill(binaryImage, 'holes');% Display the image.subplot(2, 2, 3);imshow(binaryImage, []);title('Black Blobs', 'FontSize', fontSize, 'Interpreter', 'None');axis('on', 'image');% Find the center of mass, which is the same as the centroid since all we have is a binary image.% Casting the binary image to double is the KEY to making sure we get just one% centroid rather than a centroid for each and every blob individually.labeledImage = double(binaryImage);props = regionprops(labeledImage, 'Centroid')xCentroid = props.Centroid(1);yCentroid = props.Centroid(2);% Plot a red crosshairs therehold on;plot(xCentroid, yCentroid, 'r+', 'MarkerSize', 200, 'LineWidth', 3);caption = sprintf('Centroid of black blobs at (%.2f, %.2f)', xCentroid, yCentroid);title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
labeledImage = double(binaryImage);