if true
clc;
close all;
imtool close all;
clear;
workspace;
format longg;
format compact;
fontSize = 20;
folder = 'C:\Users\Diah\Documents';
baseFileName = 'flower.jpg';
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
fullFileName = baseFileName;
if ~exist(fullFileName, 'file')
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
[rows columns numberOfColorBands] = size(rgbImage);
subplot(2, 2, 1);
imshow(rgbImage, []);
axis on;
title('Original Color Image', 'FontSize', fontSize);
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
binaryImage = rgbImage(:,:,1) > 34;
subplot(2,2,2);
imshow(binaryImage, []);
axis on;
title('Binary Image', 'FontSize', fontSize);
[labeledImage numberOfBlobs] = bwlabel(binaryImage, 8);
blobMeasurements = regionprops(labeledImage, 'Centroid');
xCenter = blobMeasurements(1).Centroid(1);
yCenter = blobMeasurements(1).Centroid(2);
subplot(2,2,3);
imshow(rgbImage);
axis on;
title('Outlines, from bwboundaries()', 'FontSize', fontSize);
hold on;
plot(xCenter, yCenter, 'r+', 'MarkerSize', 20, 'LineWidth', 3);
boundaries = bwboundaries(binaryImage);
numberOfBoundaries = size(boundaries);
for k = 1 : numberOfBoundaries
thisBoundary = boundaries{k};
plot(thisBoundary(:,2), thisBoundary(:,1), 'r', 'LineWidth', 3);
numberOfBoundaryPoints = length(thisBoundary);
angles = zeros(1, numberOfBoundaryPoints);
distances = zeros(1, numberOfBoundaryPoints);
for p = 1 : numberOfBoundaryPoints
xb = thisBoundary(p,2);
yb = thisBoundary(p,1);
angles(p) = atand((yb-yCenter) / (xb-xCenter));
distances(p) = sqrt((xb-xCenter)^2+(yb-yCenter)^2);
end
subplot(4, 2, 6);
plot(angles, 'LineWidth', 3);
caption = sprintf('Angles for blob #%d', k);
title(caption, 'FontSize', fontSize);
grid on;
subplot(4, 2, 8);
plot(distances, 'LineWidth', 3);
grid on;
caption = sprintf('Distances for blob #%d', k);
title(caption, 'FontSize', fontSize);
if numberOfBoundaries > 1
promptMessage = sprintf('Do you want to Continue processing,\nor Cancel processing?');
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmpi(button, 'Cancel')
break;
end
end
end
hold off;
end