MATLAB: What is the best way to determine the boundary of a porous object in an 2-D image

edge detectionImage Processing Toolboximage segmentation

edge detection returns many internal edges which is not desired sample image:

Best Answer

According to your definition of interior, here is code that calculates it. If you want the "reference" area to be the convex hull rather than the entire image, you can use bwconvhull().
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
% http://imageshack.us/photo/my-images/836/36543017.png/
% Read in a standard MATLAB gray scale demo image.
folder = 'C:\Users\mark\Documents\Temporary';
baseFileName = '36543017.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
errorMessage = sprintf('Error: %s does not exist in the %s folder.', baseFileName, folder);
uiwait(warndlg(errorMessage));
% File doesn't exist -- 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 in the search path folders either.', 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);
if numberOfColorBands > 1
grayImage = grayImage(:,:,1);
end
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Let's compute and display the histogram.
% [pixelCount grayLevels] = imhist(grayImage);
% subplot(2, 2, 2);
% bar(pixelCount);
% grid on;
% title('Histogram of original image', 'FontSize', fontSize);
% xlim([0 grayLevels(end)]); % Scale x axis manually.
% Threshold image.
binaryImage = grayImage < 65000;
% binaryImage = imfill(binaryImage, 'holes');
% Display the image.


subplot(2, 2, 2);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);
% Close gaps
binaryImage = imclose(binaryImage, true(3));
% Display the image.
subplot(2, 2, 3);
imshow(binaryImage, []);
title('After morphological closing', 'FontSize', fontSize);
% Get the interiors
interiors = imclearborder(~binaryImage);
% Get rid of blobs smaller than 60 pixels.
interiors = bwareaopen(interiors, 60);
% Display the image.
subplot(2, 2, 4);
imshow(interiors, []);
title('Interiors', 'FontSize', fontSize);
% Calculate the porosity
porosity = sum(interiors(:))/numel(interiors);
message = sprintf('The porosity (area fraction) of the interiors is %.4f', porosity);
uiwait(helpdlg(message));