MATLAB: How to fill the region inside a contour

contour fillingfilling region of interestImage Processing Toolboxsegmentation

I have a segmented image that contains a closed contour.I want to fill the region enclosed by the boundary as well.I tried imfill but it didnt work. Does anyone know how to do it? The image is of 'logical' datatype.

Best Answer

Alright, try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
%===============================================================================
% Read in good demo image.
folder = pwd;
baseFileName = 'contour.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorChannels] = size(rgbImage);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = rgbImage(:, :, 2); % Take green channel.
else
grayImage = rgbImage; % It's already grayscale.
end
% Display the image.




subplot(2, 3, 1);
imshow(grayImage, []);
axis on;
title('Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Threshold the image
binaryImage = grayImage > 128;
% Display the image.
subplot(2, 3, 2);
imshow(binaryImage, []);
axis on;
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Get rid of huge white surround
binaryImage = imclearborder(binaryImage);
% Display the image.
subplot(2, 3, 3);
imshow(binaryImage, []);
axis on;
title('Border-cleared Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Close the image
se = strel('disk', 2, 0);
binaryImage = imclose(binaryImage, se);
% Display the image.
subplot(2, 3, 4);
imshow(binaryImage, []);
axis on;
title('Closed Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Fill holes
binaryImage = imfill(binaryImage, 'holes');
% Display the image.
subplot(2, 3, 5);
imshow(binaryImage, []);
axis on;
title('Final Image', 'FontSize', fontSize, 'Interpreter', 'None');