clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 22;
folder = pwd;
baseFileName = 'Screenshot (125).png';
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 = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
grayImage = rgb2gray(grayImage);
end
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
hFig = gcf;
hFig.WindowState = 'maximized';
drawnow;
binaryImage = imbinarize(grayImage);
binaryImage = imclearborder(binaryImage);
subplot(2, 2, 2);
imshow(binaryImage, []);
impixelinfo;
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
se = strel('disk', 1, 0);
[labeledImage, numberOfBlobs] = bwlabel(binaryImage, 4);
subplot(2, 2, 3);
imshow(labeledImage, []);
impixelinfo;
title('Labeled Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
coloredLabelsImage = label2rgb (labeledImage, 'hsv', 'k', 'shuffle');
subplot(2, 2, 4);
imshow(coloredLabelsImage);
title('Pseudocolored Labeled Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
props = regionprops(labeledImage, 'Centroid');
xy = vertcat(props.Centroid);
xCentroid = xy(:, 1);
yCentroid = xy(:, 2);
boundaries = bwboundaries(binaryImage, 4);
hold on;
for k = 1 : length(boundaries)
thisBoundary = boundaries{k};
x = thisBoundary(:, 2);
y = thisBoundary(:, 1);
plot(xCentroid(k), yCentroid(k), 'r*');
distances = sqrt((xCentroid(k) - x) .^ 2 + (yCentroid(k) - y) .^ 2);
[maxDistance, indexOfMax] = max(distances);
xFar = x(indexOfMax);
yFar = y(indexOfMax);
line([xCentroid(k), xFar], [yCentroid(k), yFar], 'Color', 'y', 'LineWidth', 2);
end