clc;
close all;
imtool close all;
clear;
workspace;
format long g;
format compact;
fontSize = 20;
folder = pwd;
baseFileName = 'm2.bmp';
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
fullFileName = baseFileName;
if ~exist(fullFileName, 'file')
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorBands] = size(rgbImage);
subplot(2, 3, 1);
imshow(rgbImage, []);
axis on;
title('Original Color Image', 'FontSize', fontSize);
drawnow;
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
subplot(2, 3, 2);
grayImage = rgb2gray(rgbImage);
imshow(grayImage, []);
title('Gray Scale version', 'FontSize', fontSize);
subplot(2, 3, 3);
histogram(grayImage(grayImage<255))
grid on;
title('Histogram of Gray Scale Image', 'FontSize', fontSize);
someThresholdValue = 150;
binaryImage = grayImage > someThresholdValue;
binaryImage = bwareafilt(binaryImage, 1);
subplot(2, 3, 4);
imshow(binaryImage);
grid on;
title('Binary Image', 'FontSize', fontSize);
labeledImage = bwlabel(binaryImage);
props = regionprops(labeledImage, 'BoundingBox');
bbox = props.BoundingBox
croppedImageRGB = imcrop(rgbImage, bbox);
subplot(2, 3, 5);
imshow(croppedImageRGB);
grid on;
title('Cropped RGB Image', 'FontSize', fontSize);
croppedImageGray = imcrop(grayImage, bbox);
subplot(2, 3, 6);
imshow(croppedImageGray);
grid on;
title('Cropped Gray Scale Image', 'FontSize', fontSize);
Best Answer