clc;
close all;
imtool 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 = 'C:\Users\jhilam\Documents\Temporary';
baseFileName = 'CT14.jpg';
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
fullFileName = baseFileName;
if ~exist(fullFileName, 'file')
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
grayImage = grayImage(:, :, 2);
end
subplot(2, 3, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
grayImage(:, 1:5) = 0;
[pixelCount, grayLevels] = imhist(grayImage);
subplot(2, 3, 2);
bar(grayLevels, pixelCount);
grid on;
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]);
binaryImage = grayImage > 150;
subplot(2, 3, 3);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);
binaryImage2 = imfill(binaryImage, 'holes');
minAcceptableSize = round(rows*columns*0.05);
binaryImage2 = bwareaopen(binaryImage2, minAcceptableSize);
subplot(2, 3, 4);
imshow(binaryImage2, []);
title('Binary Image 2', 'FontSize', fontSize);
binaryImage2 = ~(~binaryImage2 | binaryImage);
subplot(2, 3, 5);
imshow(binaryImage2, []);
axis on;
title('Binary Image 2', 'FontSize', fontSize);
binaryImage3 = imfill(binaryImage2, 'holes');
binaryImage3 = bwareaopen(binaryImage3, minAcceptableSize);
binaryImage3 = binaryImage3 & binaryImage2;
subplot(2, 3, 6);
imshow(binaryImage3, []);
title('Binary Image 3', 'FontSize', fontSize);
uiwait(helpdlg('Done with Demo by Image Analyst!'));
Best Answer