MATLAB: How to fill the region of interest by white color

activecontourImage Processing Toolboxregion of interest filling

Hi All,
I have this Black and white mask
I want to fill the region of interest to be like this
approximatlly by white color to use it for segmentation later,please any could help by a Matlab code to do this job,thanks in advance.

Best Answer

See this demo which uses activecountour to get the outer envelope:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
% Read in a demo image.
folder = 'D:\Temporary stuff';
baseFileName = '4_400_before.png';
% 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.
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.', 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
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
axis on;
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')
drawnow;
subplot(2, 2, 2);
mask = false(rows, columns);
mask(50:300,20:300) = true;
imshow(mask);
axis on;
title('Initial mask', 'FontSize', fontSize);
drawnow;
% Now find the outer boundary.
bw = activecontour(grayImage, mask, 400, 'edge');
subplot(2, 2, 3);
imshow(bw);
axis on;
title('Outer Boundary Mask', 'FontSize', fontSize);
drawnow;
% Display the original gray scale image in the lower left
% So we can display boundaries over it.
subplot(2, 2, 4);
imshow(grayImage, []);
axis on;
hold on;
% Display the initial contour on the original image in blue.
% Display the final contour on the original image in red.
contour(mask,[0 0],'b', 'LineWidth', 2);
contour(bw,[0 0],'r', 'LineWidth', 4);
title('Image with initial and final contours overlaid', 'FontSize', fontSize);
msgbox('Done with activecontour demo');