Center of Mass, metal foam

2 vues (au cours des 30 derniers jours)
andrea zamblera
andrea zamblera le 2 Mai 2020
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.
  2 commentaires
Ameer Hamza
Ameer Hamza le 2 Mai 2020
Is the center of mass different as compared to centroid? Which function did you use to calculate centroid and area?
andrea zamblera
andrea zamblera le 3 Mai 2020
just regionprops( ,'area') regionprops( ,'centroid')
center of mass takes into consideration position but also area

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 3 Mai 2020
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.
  1 commentaire
andrea zamblera
andrea zamblera le 5 Mai 2020
Thankyou your answer was super detailed!

Connectez-vous pour commenter.

Plus de réponses (1)

Ameer Hamza
Ameer Hamza le 3 Mai 2020
Modifié(e) : Ameer Hamza le 3 Mai 2020
If you are using regionprops, then you can directly call it using the image without specifying a specific property. It will return a struct with area, centroid, and boundingbox for each region
regions = regionprops(image);
It will allow you to find corresponding Centroid and area of all region.
regions(1).Centroid % Centroid of first region
regions(1).Area % Area of first region
regions(2).Centroid % Centroid of second region
regions(2).Area % Area of second region
..
..
regions(end).Centroid % Centroid of last region
regions(end).Area % Area of last region

Catégories

En savoir plus sur Image Processing and Computer Vision dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by