clc;
close all;
imtool close all;
workspace;
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\Mark\Documents\Temporary';
baseFileName = 'octagon.png';
fullFileName = fullfile(folder, baseFileName);
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
grayImage = imread(fullFileName);
[rows columns numberOfColorBands] = size(grayImage);
subplot(3, 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')
[pixelCount grayLevels] = imhist(grayImage);
subplot(3, 3, 2);
bar(pixelCount);
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]);
grid on;
octagon = grayImage < 84;
octagon = imdilate(octagon, true(5));
subplot(3, 3, 3);
imshow(octagon, []);
title('Octagon Image', 'FontSize', fontSize);
particles = grayImage > 155;
subplot(3, 3, 4);
imshow(particles, []);
title('Particles Image', 'FontSize', fontSize);
binaryImage = octagon | particles;
subplot(3, 3, 5);
imshow(binaryImage, []);
title('Combined Image', 'FontSize', fontSize);
binaryImage2 = imclearborder(binaryImage);
subplot(3, 3, 6);
imshow(binaryImage2, []);
title('Border cleared', 'FontSize', fontSize);
borderPixels = logical(binaryImage - binaryImage2);
subplot(3, 3, 7);
imshow(borderPixels, []);
title('Border Only', 'FontSize', fontSize);
outputImage = grayImage;
outputImage(borderPixels) = 0;
subplot(3, 3, 8);
imshow(outputImage, []);
title('Final Output Image', 'FontSize', fontSize);
msgbox('Done with demo by ImageAnalyst!');
Best Answer