clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 20;
baseFileName = 'segmented.jpg';
folder = pwd;
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
originalImage = imread(fullFileName);
[rows, columns, numberOfColorBands] = size(originalImage);
if numberOfColorBands > 1
grayImage = min(originalImage, [], 3);
else
grayImage = originalImage;
end
subplot(2, 3, 1);
imshow(originalImage);
axis on;
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
subplot(2, 3, 2);
grayImage = grayImage(74:465, 430:851);
imshow(grayImage, []);
axis on;
title('Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
subplot(2, 3, 3);
histogram(grayImage);
xlim([0, 255]);
grid on;
title('Intensity Histogram of Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Gray Level', 'FontSize', fontSize);
ylabel('Pixel Count', 'FontSize', fontSize);
binaryImage = grayImage > 80 & grayImage < 160;
binaryImage = bwareafilt(binaryImage, 1);
binaryImage = ~binaryImage;
binaryImage = bwareafilt(binaryImage, 2);
subplot(2, 3, 4);
imshow(binaryImage, []);
axis on;
title('Binary Background Image', 'FontSize', fontSize);
subplot(2, 3, 5);
imshow(~binaryImage, []);
binaryImage = imclearborder(~binaryImage, 4);
binaryImage = bwareafilt(binaryImage, 1);
subplot(2, 3, 5);
imshow(binaryImage, []);
axis on;
title('Binary Background Image', 'FontSize', fontSize);
Best Answer