clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 20;
folder = pwd;
baseFileName = 'PAR1_1.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
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage);
subplot(2, 2, 1);
imshow(rgbImage, []);
title('Original Color Image', 'FontSize', fontSize, 'Interpreter', 'None');
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0, 1, 1]);
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
grayImage = rgb2gray(rgbImage);
binaryImage = imfill(grayImage ~= 255, 'holes');
binaryImage = bwareafilt(binaryImage, 1);
subplot(2, 2, 2);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
labeledImage = bwlabel(binaryImage);
props = regionprops(labeledImage, 'BoundingBox');
croppedImage = imcrop(grayImage, props.BoundingBox);
subplot(2, 2, 3:4);
imshow(croppedImage, []);
title('Cropped Image', 'FontSize', fontSize, 'Interpreter', 'None');
Best Answer