Clear Filters
Clear Filters

How can I calculate centroid to contour distance for an binary image every after 10 degree?

15 views (last 30 days)
After finding out the centroid I want to draw lines from centroid to contour every after 10 degree. Then want to calculate the distances from centoird to contour and stored it in an array.
Hank on 3 Mar 2020
Edited: Hank on 3 Mar 2020
Can you share what you've tried?
The regionprops function can give you the centroid of objects in a binary image.
im = imread('image.png');
blob = regionprops(im,'Centroid') % run region props on im, requesting centroid of blobs
Zara Khan
Zara Khan on 3 Mar 2020
I have already used regionprops and i have got the centroid too. Now what next ???I have attached image

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 3 Mar 2020
Try this code:
% Initialization steps.
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 = 20;
% Read in demo image.
folder = pwd;
baseFileName = 'is this your image.jpg';
% 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);
% Read in the image from disk. If storedColorMap is not empty, it's an indexed image with a stored colormap.
[grayImage, storedColorMap] = imread(fullFileName);
if ~isempty(storedColorMap)
grayImage = ind2rgb(grayImage, storedColorMap);
% 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.
% Display the image.
hFig = figure;
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
% Binarize the image
mask = ~imbinarize(grayImage);
title('Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
mask = bwareafilt(mask, 1); % Extract largest blob (in case there is more than 1).
props = regionprops(mask, 'Centroid');
xCenter = props.Centroid(1);
yCenter = props.Centroid(2);
boundary = bwboundaries(mask);
boundary = boundary{1};
x = boundary(:, 2);
y = boundary(:, 1);
% Compute all distances from centroid to every boundary point:
distances = sqrt((x - xCenter) .^ 2 + (y - yCenter) .^ 2);
fprintf('The mean distance from centroid is %.2f pixels.\n', mean(distances));
% Compute angles. They go from -180 to +180.
angles = atan2d(y - yCenter, x - xCenter);
% Find out which one is closest to every 10 degrees.
hold on;
counter = 1;
for angle = -180 : 10 : 170
angleDifference = abs(angles - angle);
[minAngle, index] = min(angleDifference);
% Draw a line from the centroid to that index.
line([x(index), xCenter], [y(index), yCenter], 'Color', 'r', 'LineWidth', 2);
indexesAtDeltaAngles(counter) = index;
counter = counter + 1;
% Get the mean of the indexes that belong to the "every 10 degrees" subset (if you want that).
fprintf('The mean distance from centroid at those specific angles is %.2f pixels.\n', mean(distances(indexesAtDeltaAngles)));
You'll see
The mean distance from centroid is 82.25 pixels.
The mean distance from centroid at those specific angles is 73.72 pixels.
Not sure why you're doing just a few angles since it's much easier to just compute the angles and distances of every single boundary point.
Zara Khan
Zara Khan on 4 Mar 2020
Image Analyst: Thank you. After considering angles after every 10 degree You have plotted lines from centroid to contour. Can we store those particulars line distance (centroid to contour) to an array ? here disntances from centroid to all boundaries points have been considered ,this is ok. But I too want those particulars line distances which are drawn after every 10 degree.
Yes I already have computed the angles and distances of every single boundary point. Just trying new things.
Image Analyst
Image Analyst on 4 Mar 2020
If you looked at the last line, you can see that they are in distances(indexesAtDeltaAngles). To extract into a brand new variable, just do
distances10 = distances(indexesAtDeltaAngles);
Like I said, using just this subset will result in worse, coarser data and I don't recommend doing it.

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!