clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 20;
folder = pwd;
baseFileName = 'contour.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);
if numberOfColorChannels > 1
grayImage = rgbImage(:, :, 2);
else
grayImage = rgbImage;
end
subplot(2, 3, 1);
imshow(grayImage, []);
axis on;
title('Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
binaryImage = grayImage > 128;
subplot(2, 3, 2);
imshow(binaryImage, []);
axis on;
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
binaryImage = imclearborder(binaryImage);
subplot(2, 3, 3);
imshow(binaryImage, []);
axis on;
title('Border-cleared Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
se = strel('disk', 2, 0);
binaryImage = imclose(binaryImage, se);
subplot(2, 3, 4);
imshow(binaryImage, []);
axis on;
title('Closed Image', 'FontSize', fontSize, 'Interpreter', 'None');
binaryImage = imfill(binaryImage, 'holes');
subplot(2, 3, 5);
imshow(binaryImage, []);
axis on;
title('Final Image', 'FontSize', fontSize, 'Interpreter', 'None');
Best Answer