clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 20;
image_folder = pwd;
filenames = dir(fullfile(image_folder, '*.jpg'));
totalNumberOfImages = numel(filenames);
desiredAngles = 0: 20 : 360;
allDistances = zeros(totalNumberOfImages, length(desiredAngles));
for k = 1:totalNumberOfImages
fullImageFileName= fullfile(image_folder, filenames(k).name);
grayImage = imread(fullImageFileName);
subplot (2,2,1);
imshow(grayImage);
axis('on', 'image');
caption = sprintf('Original image : "%s"', filenames(k).name);
title(caption, 'FontSize', fontSize);
if ndims(grayImage) == 3
grayImage = rgb2gray(grayImage);
end
grayImage = imresize(grayImage,0.2);
binaryImage = grayImage(:,:,1) < 168;
binaryImage = imfill(binaryImage, 'holes');
binaryImage = bwareafilt(binaryImage, 1);
subplot (2,2,2);
imshow(binaryImage);
axis('on', 'image');
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);
cla reset;
imshow(grayImage);
title('Resized Gray Image with Boundaries Overlaid on It', 'FontSize', fontSize);
axis('on', 'image');
hold on;
plot(xCenter, yCenter, 'b+', 'MarkerSize', 10, 'LineWidth', 3);
hold off;
boundaries = bwboundaries(binaryImage);
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
thisBoundary = boundaries{1};
xb = thisBoundary(:,2);
yb = thisBoundary(:,1);
hold on;
plot(xb, yb, '-', 'LineWidth', 2);
grid on;
distances = sqrt((xb-xCenter).^2+(yb-yCenter).^2);
angles = atan2d((yb-yCenter), (xb-xCenter)) + 180;
[uniqueAngles, ia, ic] = unique(angles);
uniqueDistances = distances(ia);
uniqueAngles = [uniqueAngles(end)-360; uniqueAngles; uniqueAngles(1) + 360];
uniqueDistances = [uniqueDistances(end); uniqueDistances; uniqueDistances(1)];
subplot(2, 2, 4);
plot(uniqueAngles, uniqueDistances, 'b.-', 'MarkerSize', 20, 'LineWidth', 2);
grid on;
desiredDistances = interp1(uniqueAngles, uniqueDistances, desiredAngles);
hold on
plot(desiredAngles, desiredDistances, 'ro', 'LineWidth', 2);
title('Centroid-to-Boundary Distance vs. Angle', 'FontSize', fontSize);
xlabel('Angle [degrees]', 'FontSize', fontSize);
ylabel('Distance [pixels]', 'FontSize', fontSize);
hold off;
drawnow;
allDistances(k, :) = desiredDistances;
promptMessage = sprintf('Do you want to Continue processing,\nor Quit processing?');
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Continue', 'Quit', 'Continue');
if contains(buttonText, 'Quit')
allDistances = allDistances(1:k, :);
break;
end
end
save('Latih.mat', 'desiredAngles', 'allDistances');
Best Answer