MATLAB: Clear connected pixel touching a non square border

image processingimclearborder

hi, i am trying to remove the border objects from an image of an octogonal sample. plese see the image
i would like to know if there is a way to create a mask that is octogonal and then perhaps apply imclearborder.
other suggestions are very welcome.
thank, S

Best Answer

Sharif: It's not really that hard. Just follow my instructions and do it step by step like I said in my other answer. Here's some code I banged out in about 5 minutes. I took a screenshot of your image and saved it as octagon.PNG, then wrote this code:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
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')
% User said No, so exit.
return;
end
end
% Read in a standard MATLAB gray scale demo image.
folder = 'C:\Users\Mark\Documents\Temporary';
baseFileName = 'octagon.png';
fullFileName = fullfile(folder, baseFileName);
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(3, 3, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize figure.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Let's compute and display the histogram.
[pixelCount grayLevels] = imhist(grayImage);
subplot(3, 3, 2);
bar(pixelCount);
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
grid on;
% Get the octagon background
octagon = grayImage < 84;
% Shrink the octagon a bit by dilating it.
octagon = imdilate(octagon, true(5));
% Display the image.





subplot(3, 3, 3);
imshow(octagon, []);
title('Octagon Image', 'FontSize', fontSize);
% Get the particles
particles = grayImage > 155;
% Display the image.
subplot(3, 3, 4);
imshow(particles, []);
title('Particles Image', 'FontSize', fontSize);
% Combine the two.
binaryImage = octagon | particles;
% Display the image.
subplot(3, 3, 5);
imshow(binaryImage, []);
title('Combined Image', 'FontSize', fontSize);
% Remove white stuff touching the border
binaryImage2 = imclearborder(binaryImage);
% Display the image.
subplot(3, 3, 6);
imshow(binaryImage2, []);
title('Border cleared', 'FontSize', fontSize);
% Find the stuff that we removed.
borderPixels = logical(binaryImage - binaryImage2);
% Display the image.
subplot(3, 3, 7);
imshow(borderPixels, []);
title('Border Only', 'FontSize', fontSize);
% Now use borderPixels to zero out the original image
outputImage = grayImage; % Initialize.
% Zero out border pixels.
outputImage(borderPixels) = 0;
% Display the image.
subplot(3, 3, 8);
imshow(outputImage, []);
title('Final Output Image', 'FontSize', fontSize);
msgbox('Done with demo by ImageAnalyst!');