Center of Mass, metal foam

조회 수: 2 (최근 30일)
andrea zamblera
andrea zamblera 2020년 5월 2일
댓글: andrea zamblera 2020년 5월 5일
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
Ameer Hamza
Ameer Hamza 2020년 5월 2일
Is the center of mass different as compared to centroid? Which function did you use to calculate centroid and area?
andrea zamblera
andrea zamblera 2020년 5월 3일
just regionprops( ,'area') regionprops( ,'centroid')
center of mass takes into consideration position but also area

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2020년 5월 3일
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
andrea zamblera
andrea zamblera 2020년 5월 5일
Thankyou your answer was super detailed!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Ameer Hamza
Ameer Hamza 2020년 5월 3일
편집: Ameer Hamza 2020년 5월 3일
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

카테고리

Help CenterFile Exchange에서 Image Processing and Computer Vision에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by