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.

Best Answer

The key to getting the centroid of all the blobs together instead of getting each individually is to make a labeled image where the image is floating point, not binary. If it's binary, regionprops() will internally label each connected component and report the centroids of each component individually, which you said you don't want when you said "center of mass of the black circles" (at least that's how I'm interpreting it).
Try this.
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 IMAGE
folder = 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;
end
end
grayImage = 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 image
binaryImage = 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 holes
binaryImage = 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 there
hold 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');
If you actually want all the individual centroids, you can do
labeledImage = double(binaryImage);
but you said you already did that and have the individual centroids already.