I want to fined the centroid , then distance from centroid to boundary for each object in image

4 views (last 30 days)
I want to fined the centroid, then the distance from centroid to boundaries for each object inside, down there is a code it's work but for one object can any one help to fined for all object, i think i must put a for loop to pass on each object to calculate the centroid then distance , and after finish go to next object and so on , please can any body help with thanks.
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 = 12;
%=============================================================================== grayImage = imread('Enew.png'); imshow(grayImage); title('Original');
% Threshold the image to make it binary. binaryImage = grayImage > 128; binaryImage = bwareafilt(binaryImage, 1); % Take largest blob. binaryImage = imfill(binaryImage, 'holes'); % fill holes. % Display the image. subplot(2, 2, 1); imshow(binaryImage, []); title('Original Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
% 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')
% bwboundaries() returns a cell array, where each cell contains the row/column coordinates for an object in the image. % Plot the borders of all the coins on the original grayscale image using the coordinates returned by bwboundaries. subplot(2, 2, 2); imshow(binaryImage); title('Outlines, from bwboundaries()', 'FontSize', fontSize); axis image; % Make sure image is not artificially stretched because of screen's aspect ratio. hold on; boundaries = bwboundaries(binaryImage); numberOfBoundaries = size(boundaries, 1) for k = 1 : numberOfBoundaries thisBoundary = boundaries{k}; plot(thisBoundary(:,2), thisBoundary(:,1), 'r', 'LineWidth', 3); end x = thisBoundary(:, 2); y = thisBoundary(:, 1); hold off;
% Find centroid. labeledImage = bwlabel(binaryImage); measurements = regionprops(labeledImage, 'Centroid'); xCentroid = measurements.Centroid(1); yCentroid = measurements.Centroid(2); subplot(2, 2, 3); imshow(binaryImage); title('Binary Image with Lines', 'FontSize', fontSize); hold on; plot(xCentroid, yCentroid, 'r*', 'MarkerSize', 10, 'LineWidth', 2);
% Find distances. distances = sqrt((x-xCentroid).^2 + (y-yCentroid).^2);
% Display the histogram of distances. subplot(2, 2, 4); histogram(distances, 20); caption = sprintf('Histogram of distances\nfrom center to all other points.'); title(caption, 'FontSize', fontSize, 'Interpreter', 'None'); xlabel('Distance', 'FontSize', fontSize); ylabel('Count', 'FontSize', fontSize); % Compute the mean distance meanDistance = mean(distances) % Put a red line there on the histogram. line([meanDistance, meanDistance], ylim, 'Color', 'r', 'LineWidth', 2); grid on; % Label the mean on the plot. yl = ylim(); % Get range of y axis. caption = sprintf(' Mean distance = %.2f pixels', meanDistance) text(meanDistance, 0.95*yl(2), caption, 'FontSize', 14, 'FontWeight', 'bold', 'Color', 'r');

Accepted Answer

Image Analyst
Image Analyst on 23 Jul 2018
Simply label the image, extract each region one at a time, and compute the centroid and boundaries and distances. Full code is attached.
  14 Comments
alaa shamasneh
alaa shamasneh on 4 Aug 2018
Dear Image analyst, thanks for your answer i will check and comeback to you. So sorry cause i didn't comeback to you before just cause i was travelling. I will check and comeback to you soon. Really thanks for you
QuestionsAccount
QuestionsAccount on 5 Jun 2020
dear @Image Analyst i know that question is asked a very long time ago and also asked by someone else i want to put my question here becasue my problem was similar to the above one.
Image Analyst i tried your above provided code https://www.mathworks.com/matlabcentral/answers/411550-i-want-to-fined-the-centroid-then-distance-from-centroid-to-boundary-for-each-object-in-image#answer_329876 I just want to ask you that instead of histograme that we display in this code at the end if i want to display the image which showing distances from the centroid then how can i do that? what i wanted to do or what i m asking is attached in image actually i want to find distance from center to boundry points and then display them as after connecting them with center in order to make skeleton of human body.
your help in this regard is highly appriciated.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!