clc;
close all;
imtool close all;
clear;
workspace;
format long g;
format compact;
fontSize = 22;
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
return;
end
end
folder = pwd;
baseFileName = 'iso.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, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
grayImage = grayImage(:, :, 2);
end
subplot(2, 3, 1);
imshow(grayImage, []);
axis on;
title('Original Grayscale Image', 'FontSize', fontSize);
drawnow;
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
[pixelCount, grayLevels] = imhist(grayImage);
pixelCount(1) = 0;
pixelCount(end) = 0;
subplot(2, 3, 2);
bar(grayLevels, pixelCount, 'BarWidth', 1, 'FaceColor', 'b');
grid on;
title('Histogram of Original Image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]);
thresholdValue = 25;
line([thresholdValue, thresholdValue], ylim, 'LineWidth', 2, 'Color', 'r');
text(28, 1500, 'Threshold = 25', 'Color', 'r', 'FontSize', 20, 'FontWeight', 'bold');
binaryImage = grayImage > thresholdValue;
subplot(2, 3, 3);
imshow(binaryImage, []);
axis on;
title('Binary Image', 'FontSize', fontSize);
drawnow;
binaryImage = imclearborder(binaryImage);
binaryImage = bwareafilt(binaryImage, 1);
binaryImage = imfill(binaryImage, 'holes');
subplot(2, 3, 4);
imshow(binaryImage, []);
axis on;
title('Circle-Only Binary Image', 'FontSize', fontSize);
drawnow;
maskedImage = grayImage;
maskedImage(~binaryImage) = 0;
subplot(2, 3, 5);
imshow(maskedImage, []);
axis on;
title('Masked Circle-Only Image', 'FontSize', fontSize);
labeledImage = bwlabel(binaryImage);
props = regionprops(labeledImage, 'Centroid', 'Area');
hold on;
plot(props.Centroid(1), props.Centroid(2), 'r+', 'MarkerSize', 50);
message = sprintf('The Area = %f.\nThe Centroid is at (x,y) = (%f, %f)', ...
props.Area, props.Centroid);
uiwait(helpdlg(message));