clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 20;
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
return;
end
end
folder = pwd
baseFileName = 'batik22.PNG';
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, 2, 1);
imshow(rgbImage);
title('Original Color Image', 'FontSize', fontSize, 'Interpreter', 'None');
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0, 1, 1]);
grayImage = rgbImage(:,:, 1);
subplot(2, 2, 2);
imshow(grayImage);
axis on;
title('Red Channel Image', 'FontSize', fontSize, 'Interpreter', 'None');
binaryImage = grayImage < 127;
subplot(2, 2, 3);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
binaryImage = ~bwareafilt(binaryImage, 8);
subplot(2, 2, 4);
imshow(binaryImage, []);
caption = sprintf('Final Binary Image with 8 largest blobs');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
Best Answer