Why is my centroid giving only 1 element per blob using region props?

they should be 2 per blob x and y elements

2 Comments

Without seeing some code or example to replicate your issue, it's difficult for us to say what you're doing wrong.
Iregion=regionprops(bw2,'centroid');
bw2 being a binary image of objects.

Sign in to comment.

 Accepted Answer

It's not clear exactly what you're calling an "element". I'll use more precise terminology. If your Iregion structure array is just one structure, then you have just one blob. The Centroid field of that structure will be a 1-by-2 array with the x and y locations of the centroid of that blob.
If you have multiple blobs, then you need to add an index after the structure variable name to get just one of the blobs, try this:
props = regionprops(bw2,'centroid');
hold on;
for k = 1 : length(props)
% Get x and y centroid of the k'th blob.
xCentroid = props(k).Centroid(1);
yCentroid = props(k).Centroid(2);
plot(xCentroid, yCentroid, 'r+', 'MarkerSize', 40, 'LineWidth', 2);
% Let user know what they are
message = sprintf('The largest blob has area %d and a centroid at (x,y) = (%f, %f)',...
props.Area, xCentroid, yCentroid)
uiwait(msgbox(message));
end
You can also get all the x and y centroids into arrays doing this:
centroids = [props.Centroid];
xCentroids = centroids(1:2:end);
yCentroids = centroids(2:2:end);

10 Comments

so i did apply the following :
Iregion=regionprops(bw2,'centroid');
hold on;
for k = 1 : length(Iregion)
% Get x and y centroid of the k'th blob.
xCentroid = Iregion(k).Centroid(1);
yCentroid = Iregion(k).Centroid(2);
% plot(xCentroid, yCentroid, 'r+', 'MarkerSize', 40, 'LineWidth', 2);
end
centroids = [Iregion.Centroid];
xCentroids = centroids(1:2:end);
yCentroids = centroids(2:2:end);
Afterwards i've done
disp(centroids), disp(xCentroids) and disp(yCentroids)
and got 96 ,48,48 which is rather logic as centroids = xCentroids + yCentroids and xCentroids = yCentroids , taht being said i have 56 objects so was waiting for xCentroids = YCentroids = 56.
what could be the issue?
I have no idea what this means "which is rather logic as centroids = xCentroids + yCentroids and xCentroids = yCentroids , taht being said i have 56 objects so was waiting for xCentroids = YCentroids = 56."
You have the centroids array, which is [x1,y1,x2,y2,x3,y3,....x56,y56], then you have xCentroids which is [x1,x2,x3,...x56] and yCentroids which is [y1,y2,y3,...y56]. So you have the centroids of all 56 blobs. Regionprops() should be giving you 56 structures, each with an area and centroid. disp(centroids) should spit out 112 elements, and disp(xCentroids) should spit out 56 numbers. Why you got only 3 instead of 56 is unknown.
You have either 3 or 56 blobs in your image. I don't know because you haven't shared your image. How many do you think you have to start with? Do you filter any of them out, like take the largest 3 or something? How many do you want to end up with (3 or 56)? Please attach your image for further help.
what i was trying to stay is that the results i got for xCentroids and yCentroids is 48 while i was expecting 56 since i have 56 objects.
how do i know i have 56 objects?
[label,numObjects]=bwlabel(bw2,4);
disp(numObjects) gave 56 in another part of the same code.
Please attach your image for further help.
Not sure how helpful this image can be
Please attach the image, not a screenshot. I want the original binary image. Do this:
imwrite(binaryImage, 'binaryImage.png');
then attach binaryimage.png. I can't do anything with the figure you attached that has toolbar button, gray surround, etc.
Elias, there are 48 blobs, not 56 or 3. The largest blob is blob #28. See this code:
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 = 25;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'binaryImage.png';
% Get the full filename, with path prepended.
folder = []; % Determine where demo folder is (works with all versions).
fullFileName = fullfile(folder, baseFileName);
%===============================================================================
% Read in a demo image.
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, 1, 1);
imshow(grayImage, []);
axis on;
axis image;
caption = sprintf('Original Gray Scale Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
% Threshold the image.
binaryImage = grayImage >= 1;
% Display the image.
subplot(2, 1, 2);
imshow(binaryImage, []);
axis on;
axis image;
% Find connected components (blobs).
[labeledImage, numBlobs] = bwlabel(binaryImage);
caption = sprintf('Final binary image showing %d blob centroids', numBlobs);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
% Make the measurements of the bounding boxes.
props = regionprops(labeledImage, 'Centroid', 'Area');
hold on;
for k = 1 : length(props)
% Get x and y centroid of the k'th blob.
xCentroid = props(k).Centroid(1);
yCentroid = props(k).Centroid(2);
plot(xCentroid, yCentroid, 'r*', 'MarkerSize', 20, 'LineWidth', 2);
% Let user know what they are
message = sprintf('Blob #%2d has area %3d and a centroid at (x,y) = (%6.2f, %6.2f)',...
k, props(k).Area, xCentroid, yCentroid);
fprintf('%s\n', message);
promptMessage = sprintf('%s\n\nDo you want to Continue processing,\nor Quit processing?', message);
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Continue', 'Quit', 'Continue');
if strcmpi(buttonText, 'Quit')
break;
end
end
% Get all the centroids.
centroids = [props.Centroid];
allXCentroids = centroids(1:2:end);
allYCentroids = centroids(2:2:end);
% Find the largest blob
allAreas = [props.Area]
[maxArea, indexOfLargest] = max(allAreas)
% Plot a circle around the largest.
plot(allXCentroids(indexOfLargest), allYCentroids(indexOfLargest), 'go', 'MarkerSize', 40, 'LineWidth', 2);
plot(allXCentroids(indexOfLargest), allYCentroids(indexOfLargest), 'g*', 'MarkerSize', 40);
message = sprintf('The largest blob has an index of %d\nan area of %2d\nand a centroid at (x,y) = (%6.2f, %6.2f)',...
indexOfLargest, maxArea, allXCentroids(indexOfLargest), allYCentroids(indexOfLargest))
uiwait(msgbox(message));
Can you explain why numObjects was 56 running the following
[label,numObjects]=bwlabel(bw2,4);
like was it counting every shape before and now it excludes none blob like shapes?
It is not 56. I doubt it ever was with the image that you supplied me.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!