clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 15;
folder = pwd;
baseFileName = '64.jpg';
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, storedColorMap] = imread(fullFileName);
if ~isempty(storedColorMap)
grayImage = ind2rgb(grayImage, storedColorMap);
end
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
grayImage = rgb2gray(grayImage);
end
hFig = figure;
subplot(2, 3, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
hFig.WindowState = 'maximized';
drawnow;
subplot(2, 3, 2);
imhist(grayImage);
grid on;
binaryImage = imbinarize(grayImage);
subplot(2, 3, 3);
imshow(binaryImage, []);
title('Initial Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
binaryImage = imfill(binaryImage, 'holes');
binaryImage = bwareafilt(binaryImage, 1);
subplot(2, 3, 4);
imshow(binaryImage, []);
title('Filled Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
props = regionprops(binaryImage, 'BoundingBox');
croppedImage = imcrop(grayImage, props.BoundingBox);
subplot(2, 3, 5);
imshow(croppedImage, []);
impixelinfo
caption = sprintf('Cropped Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
pctH1 = 18;
pctH2 = 86;
pctV1 = 10;
pctV2 = 62;
[rows, columns, numberOfColorChannels] = size(croppedImage)
row1 = round(pctV1 * rows / 100)
row2 = round(pctV2 * rows / 100)
col1 = round(pctH1 * columns / 100)
col2 = round(pctH2 * columns / 100)
finalImage = croppedImage(row1:row2, col1:col2);
subplot(2, 3, 6);
imshow(finalImage, []);
impixelinfo
axis('on', 'image');
caption = sprintf('Final Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hold on;