clear connected pixel touching a non square border

7 views (last 30 days)
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

Accepted Answer

Image Analyst
Image Analyst on 18 Feb 2012
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!');

More Answers (1)

Image Analyst
Image Analyst on 17 Feb 2012
Threshold to get the dark outer octagon. You might need to dilate this a layer or two. Then threshold to get the bright particles. Then AND those two together. This gets you a combined binary image that has both the dark surround and the bright inner particles. Then call imclearborder() and subtract from your original combined binary image to get a binary image of just the dark surround and the particles that touch it. Then use that to mask out (zero out, erase) the bright particles that touch the border from your original image.
Your flickr web site says "The owner has disabled downloading of their photos" so I can't get a copy unless you want me to.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!