clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 20;
folder = pwd;
baseFileName = 'is this your image.jpg';
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
fullFileNameOnSearchPath = baseFileName;
if ~exist(fullFileNameOnSearchPath, 'file')
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
[grayImage, storedColorMap] = imread(fullFileName);
if ~isempty(storedColorMap)
grayImage = ind2rgb(grayImage, storedColorMap);
end
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
grayImage = rgb2gray(grayImage);
end
hFig = figure;
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
hFig.WindowState = 'maximized';
drawnow;
mask = ~imbinarize(grayImage);
imshow(mask);
title('Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
hFig.WindowState = 'maximized';
drawnow;
mask = bwareafilt(mask, 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);
distances = sqrt((x - xCenter) .^ 2 + (y - yCenter) .^ 2);
fprintf('The mean distance from centroid is %.2f pixels.\n', mean(distances));
angles = atan2d(y - yCenter, x - xCenter);
hold on;
counter = 1;
for angle = -180 : 10 : 170
angleDifference = abs(angles - angle);
[minAngle, index] = min(angleDifference);
line([x(index), xCenter], [y(index), yCenter], 'Color', 'r', 'LineWidth', 2);
indexesAtDeltaAngles(counter) = index;
counter = counter + 1;
end
fprintf('The mean distance from centroid at those specific angles is %.2f pixels.\n', mean(distances(indexesAtDeltaAngles)));