clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 20;
folder = fileparts(which('cameraman.tif'));
baseFileName = 'eight.tif';
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, []);
axis on;
hold on;
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
[pixelCount, grayLevels] = imhist(grayImage);
subplot(2, 2, 2);
bar(grayLevels, pixelCount);
grid on;
title('Histogram of original image', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Gray Level', 'FontSize', fontSize);
ylabel('Pixel Count', 'FontSize', fontSize);
xlim([0 grayLevels(end)]);
drawnow;
binaryImage = grayImage < 210;
binaryImage = bwareafilt(binaryImage, 4);
binaryImage = imfill(binaryImage, 'holes');
subplot(2, 2, 3);
imshow(binaryImage);
axis on;
title('Binary Image', 'FontSize', fontSize);
drawnow;
[labeledImage, numRegions] = bwlabel(binaryImage);
props = regionprops(labeledImage, 'Image', 'Centroid')
enlargementFactor = 1.5;
for k = 1 : numRegions
subplot(2, 2, 1);
plot(props(k).Centroid(1), props(k).Centroid(2), 'r+', 'LineWidth', 3, 'MarkerSize', 50);
thisImage = props(k).Image;
thisImage = imresize(thisImage, enlargementFactor);
subplot(2, 2, 4);
imshow(thisImage);
axis on;
caption = sprintf('This is region #%d, enlarged by a factor of %.2f', k);
title(caption, 'FontSize', fontSize);
promptMessage = sprintf('%s\nDo you want to Continue processing,\nor Quit processing?', caption);
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Continue', 'Quit', 'Continue');
if strcmpi(buttonText, 'Quit')
return;
end
end