How can I find the centre of gravity of an image?

23 visualizaciones (últimos 30 días)
Tola
Tola el 19 de Mzo. de 2013
Movida: DGM el 13 de Feb. de 2023
I tried regionprops but it is giving out more than one centroid and I need the centre of gravity of the entire image. Thank you.
  1 comentario
Walter Roberson
Walter Roberson el 19 de Mzo. de 2013
Do the different colors correspond to different densities? If so then you need to know how color maps to density in order to find the center of gravity.

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 19 de Mzo. de 2013
You need to have the binary image be the entire image. Just do something like this (untested):
binaryImage = true(size(grayImage));
measurements = regionprops(binaryImage, grayImage, 'WeightedCentroid');
cog = measurements(1).WeightedCentroid;
  13 comentarios
Image Analyst
Image Analyst el 19 de Mzo. de 2022
Movida: DGM el 13 de Feb. de 2023
OK, here's a nested for loop way of doing it that doesn't use regionprops():
grayImage = imread('moon.tif');
[rows, columns, numberOfColorChannels] = size(grayImage);
imshow(grayImage);
axis('on', 'image');
sumgx = 0;
sumgy = 0;
sumg = 0;
for col = 1 : columns
for row = 1 : rows
gl = double(grayImage(row, col));
sumg = sumg + gl;
sumgx = sumgx + col * gl;
sumgy = sumgy + row * gl;
end
end
xCOG = sumgx / sumg
xCOG = 148.8500
yCOG = sumgy / sumg
yCOG = 263.7007
hold on
xline(xCOG, 'LineWidth', 2, 'Color','r');
yline(yCOG, 'LineWidth', 2, 'Color','r');
caption = sprintf('Center of Gravity: (%.2f, %.2f)', xCOG, yCOG);
title(caption, 'FontSize', 18)
Lieu-Kim Dang
Lieu-Kim Dang el 19 de Mzo. de 2022
Movida: DGM el 13 de Feb. de 2023
This code works perfect for me! Thank you so much for your incessantly efforts!
I appreciate a lot your help.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Image Processing Toolbox en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by