Why is it detecting 2 centroids in 1 object?

For the single object (shown on right), it is currently detecting two centroids (shown on left).
How do I correct this, such that it only detects 1 centroid per object?

2 Comments

Please share your code.
labelstemp=imclose(labelstemp,se);
se=strel('disk',1);
labelstemp=imdilate(labelstemp,se);
.
.
.
s = regionprops(L, 'Orientation', 'MajorAxisLength', ...
'MinorAxisLength', 'Eccentricity', 'Centroid','solidity');

Sign in to comment.

Answers (2)

Are you sure you included the correct image file ?
I = load("labelstemp.mat");
I = I.labelstemp;
size(I,1)*size(I,2)
ans = 1690000
nnz(I(:))
ans = 388
imshow(I);hold on;
Ibw = imfill(I,'holes');
Ilabel = bwlabel(Ibw);
stat = regionprops(Ilabel,'centroid');
for x = 1: numel(stat)
plot(stat(x).Centroid(1),stat(x).Centroid(2),'ro');
end
I think you're mistaken, or that you attached the wrong data. Your attached data has only one blob in it. See this well commented demo. The demo should also work to mark each blob with a crosshairs in the event you change your binary image to one that has multiple blobs in it. Be aware that centroids don't always lie inside a blob, particularly if they have holes (like a donut which has the centroid in the hole), or if they are non-convex (like a C shape).
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 = 18;
% Load user's binary image.
StoredStucture = load("labelstemp.mat");
% Extract binary image from the structure and display it.
binaryImage = StoredStucture.labelstemp;
imshow(binaryImage, []);
impixelinfo; % Allow user to mouse around and inspect gray levels.
axis('on', 'image'); % Put up axis tick marks and labels.
% Maximize window
g = gcf;
g.WindowState = 'maximized';
% Measure the centroid of all the blobs.
props = regionprops(binaryImage, 'Centroid');
numBlobs = numel(props);
caption = sprintf('Found %d blobs.', numBlobs);
fprintf('%s\n', caption);
% Loop over all found blobs putting a red crosshairs at the centroid.
hold on;
for k = 1 : numBlobs
x = props(k).Centroid(1);
y = props(k).Centroid(2);
fprintf('Marking blob #%d. Centroid at x (column) %.1f and y (row) %.1f.\n', k, x, y);
plot(x, y, 'r+', 'MarkerSize', 60, 'LineWidth', 2);
end
% Put title atop the image.
title(caption, 'FontSize', fontSize)

Tags

Asked:

on 31 Jul 2023

Answered:

on 31 Jul 2023

Community Treasure Hunt

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

Start Hunting!